matchcomponents:
case [int(), *_] ascomponent_ids:
print("Dispatch: Setting components to %s", components)
case [ComponentCategory.BATTERY, *_]:
print("Dispatch: Using all battery components")
caseunsupported:
print(
"Dispatch: Requested an unsupported selector %r, ""but only component IDs or category BATTERY are supported.",
unsupported,
)
But it doesn't work when trying to assign component_ids to a variable hinted with type list[int], mypy gives this error:
Incompatible types in assignment (expression has type "list[int] | list[ComponentCategory]", variable has type "list[int] | None") [assignment]
What did you expect instead?
Code examples to be robust and work for most use cases.
Affected part(s)
Documentation (part:docs)
Extra information
This code works:
matchdispatch.selector:
case []:
print("Dispatch: Using all components")
caselist() asidsifisinstance(ids[0], int):
component_ids=idscase [ComponentCategory.BATTERY, *_]:
component_category=ComponentCategorySDK.BATTERYcaseunsupported:
print(
"Dispatch: Requested an unsupported selector %r, ""but only component IDs or category BATTERY are supported.",
unsupported,
)
But ideally we should probably add some way to do these checks more easily for the users.
Activity
Update component selector example
Update component selector example