Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Discriminator Support for Polymorphic Types (Unions) #72

Closed
seandstewart opened this issue Apr 30, 2020 · 2 comments · Fixed by #133
Closed

Add Discriminator Support for Polymorphic Types (Unions) #72

seandstewart opened this issue Apr 30, 2020 · 2 comments · Fixed by #133
Labels
enhancement New feature or request
Projects
Milestone

Comments

@seandstewart
Copy link
Owner

seandstewart commented Apr 30, 2020

Description

A common pattern for polymorphic data in statically typed languages is the use of a discriminator field on the object which can be used to determine which type to deserialize said data into. This is main use-case for users registering custom type deserializers in typical. A better route would be to expose a method at time of declaration by which the user my declare a discriminator.

Potential Implementation

from typing import ClassVar, Union
import typic

@typic.klass
class Something:
    type: ClassVar[str] = "something"


@typic.klass
class Else:
    type: ClassVar[str] = "else"


discriminator = typic.discriminator(field="type", mapping={"something": Something, "else": Else})


@typic.klass
class Poly:
    morphic: Union[Something, Else] = typic.field(discriminator=discriminator)

This relates to #56

@seandstewart seandstewart added this to To do in v2.1 via automation Apr 30, 2020
@seandstewart seandstewart added this to the Typic 2.1 milestone Apr 30, 2020
@seandstewart seandstewart added the enhancement New feature or request label Jun 17, 2020
@syastrov
Copy link
Contributor

Another way would be to use literal types for the discriminator, as otherwise the variable could be mutated.

mypy just got support for narrowing tagged-unions: https://mypy.readthedocs.io/en/latest/literal_types.html#tagged-unions

I know explicit is better than implicit, but it would be cool if typic automatically could handle deserializing in this case: a union of objects each having exactly one field with the same name, which is annotated using a distinct literal type. I think that's pretty unambiguous what the behavior should be. :)

@seandstewart
Copy link
Owner Author

Now that mypy has defined the expected behavior, I think that's the way to go for sure.

v2.1 automation moved this from To do to Done Aug 10, 2020
@seandstewart seandstewart reopened this Aug 10, 2020
v2.1 automation moved this from Done to In progress Aug 10, 2020
@seandstewart seandstewart linked a pull request Oct 11, 2020 that will close this issue
seandstewart added a commit that referenced this issue Oct 11, 2020
@seandstewart seandstewart mentioned this issue Dec 8, 2020
@seandstewart seandstewart moved this from In progress to Done in v2.1 Jan 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
No open projects
v2.1
  
Done
Development

Successfully merging a pull request may close this issue.

2 participants