-
Notifications
You must be signed in to change notification settings - Fork 37
Closed
Description
We need to support a syntax like this for parameters that are optional, but if they exist should have a given type:
class MyDevice(Device):
maybe_attr: SignalR[int] | NoneThe behaviour should be:
- at init then
device.maybe_attris a disconnectedSignalR[int] - at connect then
device.maybe_attrshould become either become disconnected or becomeNonedepending on if the signal exists in the PVI structure
Suggested strategy:
- In
DeviceFiller._scan_for_annotationsthen look for| Noneand add it toself._optional_devices: set[str]. E.g. go from
ophyd-async/src/ophyd_async/core/_device_filler.py
Lines 122 to 123 in d381c3b
name = UniqueName(attr_name) origin = get_origin_class(annotation)
to
name = UniqueName(attr_name)
origin = get_origin_class(annotation)
args = get_args(annotation)
if origin is type.UnionType and None in args and len(args) == 2:
# We got `T | None`, so register it as optional, and continue with `T`
self._optional_device.add(name)
annotation, = [x for x in args if x is not None]
origin = get_origin_class(annotation)- In
DeviceFiller.check_filledthen check those that are not filled but optional to None. E.g. go from
ophyd-async/src/ophyd_async/core/_device_filler.py
Lines 244 to 248 in d381c3b
unfilled = sorted(set(self._unfilled_connectors).union(self._unfilled_backends)) if unfilled: raise RuntimeError( f"{self._device.name}: cannot provision {unfilled} from {source}" )
to
unfilled = set(self._unfilled_connectors).union(self._unfilled_backends)
unfilled_optional = unfilled.intersection(self._optional_device)
for name in unfilled_optional:
setattr(self._device, name, None)
unfilled_mandatory = sorted(unfilled.difference(unfilled_optional))
if unfilled_mandatory:
raise RuntimeError(
f"{self._device.name}: cannot provision {unfilled_mandatory} from {source}"
)Acceptance Criteria
- Code changed and tests written for PVI devices
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels