typing_inspect
typing_inspect copied to clipboard
`get_origin()` doesn't support PEP 585 generic types?
Hi,
I'm seeing different behavior with get_origin(typing.List) and get_origin(list):
[ins] In [1]: from typing_extensions import get_origin
[ins] In [2]: import typing
[ins] In [3]: get_origin(list[int])
Out[3]: list
[ins] In [4]: get_origin(list)
[ins] In [5]: get_origin(typing.List)
Out[5]: list
This is what I'm running into as well. I would like to be able to work around the fact that e.g. typing.Sequence.mro() will give me a sane list of the type hierarchy, but list is jumping directly to object (after pep 585), even though issubclass(list, typing.Sequence) is True.
I realize why this is happening, but I am here because I thought typing_inspect gave a workaround to access the actual type hierarchy here. I don't want to pairwise check whether or not type H is a subclass of G, Y, and A, but would much prefer to get a list of everything in the base library list is known a priori to pass a typecheck on! :sweat_smile:
IIUC this behavior matches get_origin() from typing itself, right (for versions where it is available)? Also changing this would be backwards incompatible, so I am not sure we should do this.
In any case, a simple workaround is to keep a map list -> List, dict -> Dict etc. (there are very few builtin collections that are actually generic), and then call get_origin() again on the typing version if the first call returned something in this map.