aind-data-schema icon indicating copy to clipboard operation
aind-data-schema copied to clipboard

Current Enum-like Union pattern does not provide type hinting

Open bruno-f-cruz opened this issue 11 months ago • 0 comments

Is your feature request related to a problem? Please describe. Type hinting for sets of valid inputs that are made up of classes (e.g. Organization) lack a proper typing hinting. As an example, the following line:

https://github.com/AllenNeuralDynamics/aind-data-schema/blob/0328cbed69d5da849660557be162a24c2cd02df4/src/aind_data_schema/models/organizations.py#L774

results in the following type hint in vscode:

(variable) source: Any using the following line:

non_viral = NonViralMaterial(name="example", source=Organization.ABCAM, rrid=None, lot_number="foo", expiration_date=None)

Describe the solution you'd like Change the way these unions are defined as I previously suggested in #731 to:

    _ALL = tuple(_Organization.__subclasses__())

    class ONE_OF(RootModel):
        root: Annotated[Union[Organization._ALL], Field(discriminator="name")]

which results in the following type hint (variable) source: ONE_OF

while not perfect for this example since ONE_OF is not descriptive, i think it is a problem that can be solved by renaming the class if needed, and it will definitely be infinitely more useful than Any.

Describe alternatives you've considered Do away with this pattern altogether and simply use value-type Enums. I don't think we can move in this direction anymore since there is a lot of information already packed into these enum-like patterns.

Additional context Add any other context or screenshots about the feature request here.

bruno-f-cruz avatar Mar 16 '24 04:03 bruno-f-cruz