typing_inspect icon indicating copy to clipboard operation
typing_inspect copied to clipboard

`get_origin()` doesn't support PEP 585 generic types?

Open rouge8 opened this issue 3 years ago • 2 comments
trafficstars

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

rouge8 avatar Jul 25 '22 20:07 rouge8

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:

rtbs-dev avatar Aug 02 '22 18:08 rtbs-dev

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.

ilevkivskyi avatar Aug 16 '22 18:08 ilevkivskyi