oxan / djangorestframework-dataclasses

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

Fix handling of nested Literal types #9

Closed intgr closed 4 years ago

intgr commented 4 years ago

Turns out there was a bug in #5. Whilst working on #8 it occurred to me that I left this case unhandled.

The set of allowed types in Literal[] is quite limited, but the case of other nested Literal[]s was not properly handled.

For more details: https://www.python.org/dev/peps/pep-0586/#legal-parameters-for-literal-at-type-check-time

oxan commented 4 years ago

I'm surprised Python doesn't automatically flatten the tree, like it does with nested Unions.

intgr commented 4 years ago

Yep, doesn't work like that

In [1]: from typing_extensions import Literal                                                                                                                                                                                            
In [2]: Literal[Literal['a'], 'b'].__args__                                                                                                                                                                                                   
Out[2]: (typing_extensions.Literal['a'], 'b')
In [6]: Literal[Literal['a']]                                                                                                                                                                                                            
Out[6]: typing_extensions.Literal[typing_extensions.Literal['a']]

PS: Any rough timeline for merging this and the next release? (#8 is not that important)

oxan commented 4 years ago

I've merged it and will create a new release in the next hour or so. I'll take a look at #8 when I've a bit more time somewhere later this week.