Skip to content

Support Signal[T] | None syntax in DeviceFiller #1163

@coretl

Description

@coretl

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] | None

The behaviour should be:

  • at init then device.maybe_attr is a disconnected SignalR[int]
  • at connect then device.maybe_attr should become either become disconnected or become None depending on if the signal exists in the PVI structure

Suggested strategy:

            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_filled then check those that are not filled but optional to None. E.g. go from
    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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions