Skip to content

Incorrect type of key in PreviewParam for representing a key that may not be present #608

@yousefmoazzam

Description

@yousefmoazzam

PreviewParam is intended to represent the python dict that is the value of the preview parameter in a loader's config (if the preview parameter value is given at all, it can also be None):

class PreviewParam(TypedDict):
"""
Preview configuration dict.
"""
angles: Optional[StartStopEntry]
detector_y: Optional[PreviewParamEntry]
detector_x: Optional[PreviewParamEntry]

The intention in the definition of the type was that any of the keys could be omitted. However, due to the use of the Optional type, in its current state, the PreviewParam type states that:

  • all three keys must be present in the dict
  • the value of the keys can be either None or some other type

which isn't what was intended.

The Optional type alone is not the correct type for what is desired.

The NotRequired type provides a piece of the puzzle.

However, NotRequired[T] isn't quite correct either. This is because NotRequired[T] disallows setting the key's value to None, which is allowed.

The correct type should allow:

  • omitting of a key
  • but also providing the key as either None or the T

which I think is accomplished with NotRequired[Optional[T]].

Metadata

Metadata

Assignees

No one assigned

    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