oxan / djangorestframework-dataclasses

Dataclasses serializer for Django REST framework
BSD 3-Clause "New" or "Revised" License
429 stars 28 forks source link

Add Literal["values"] field support, convert to a ChoiceField #5

Closed intgr closed 4 years ago

intgr commented 4 years ago

Since the Literal type is not available before Python 3.8, this introduces a new dependency typing_extensions, which backports it.

intgr commented 4 years ago

Sure, making typing_extensions dependency optional conditional sounds reasonable.

intgr commented 4 years ago

By the way: Does it bother you that Literal['bla', None] is considered is_optional=False in TypeInfo but it's actually equivalent to Optional[Literal['bla']], which would have is_optional=True?

Should I change is_optional_type as well?

oxan commented 4 years ago

Meh, it doesn't really bother me, as it still does the right thing atm. There's more strange constructs that don't really do the right thing in TypeInfo (consider Union[None, str, int] or Union[Literal[..], Literal[..]]). If it becomes a problem I'd want to refactor that whole thing to be a bit more robust, or probably just vendor it out to an external implementation to handle all the subtleties.