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

is_generic_type(List) is False in 3.9 #60

Closed
JelleZijlstra opened this issue May 29, 2020 · 6 comments · Fixed by #69
Closed

is_generic_type(List) is False in 3.9 #60

JelleZijlstra opened this issue May 29, 2020 · 6 comments · Fixed by #69

Comments

@JelleZijlstra
Copy link

In [7]: sys.version                                                                                                                                                                  
Out[7]: '3.9.0b1 (v3.9.0b1:97fe9cfd9f, May 18 2020, 20:39:28) \n[Clang 6.0 (clang-600.0.57)]'

In [8]: from typing import List                                                                                                                                                      

In [9]: typing_inspect.is_generic_type(List)                                                                                                                                         
Out[9]: False

Apparently in 3.9 List became an instance of _SpecialGenericAlias instead of _GenericAlias, which is what typing_inspect checks for.

@thatch
Copy link

thatch commented Jun 1, 2020

This same bug has far-reaching effects and is the cause of Instagram/LibCST#305 as well.

thatch added a commit to thatch/typing_inspect that referenced this issue Jun 1, 2020
The `_GenericAlias` still exists, but they now subclass
`_SpecialGenericAlias`.  This affects many things, so just use a tuple
up top.

Fixes ilevkivskyi#60
@thatch
Copy link

thatch commented Jun 1, 2020

This is a little more interesting, and mainly appears to affect unsubscripted types that ought to be considered generic. In 3.8, these have __args__ with TypeVar; in 3.9 there's only _nargs.

If we apply the trivial fix to consider unsubscripted types to be generic as well, we get a predictable AttributeError. If we return empty-tuple, that's a behavior change, and I'd like to have others weigh in on whether this is a bug in upstream 3.9, and whether get_args returning empty-tuple is good enough. That would work for the current implementation in LibCST, since it just skips further validation.

@thatch
Copy link

thatch commented Aug 28, 2020

Jelle, I had a conversation with Łukasz and I don't feel like we have buy-in on this being a cpython bug. I don't think there's a way to fix this in typing_inspect, can you help me with considering fixes and escalating?

@ilevkivskyi
Copy link
Owner

FWIW, a potential fix can be hard-coding some special cases. Technically, typing_inspect uses a bunch of private APIs to provide a bit deeper level of introspection than the one provided by typing, so I can see why this is not an upstream bug.

I may not have time to work on this soon, but if someone has ideas for a robust fix, I will be glad to merge a PR.

@JelleZijlstra
Copy link
Author

Similar to what Ivan said, I'm not sure why this can't be fixed in typing-inspect, for example by looking for the _SpecialGenericAlias class inside the implementation of is_generic_type().

I ran into this doing some runtime type inspection in pyanalyze and worked around it with some 3.9-specific code: https://github.com/quora/pyanalyze/blob/master/pyanalyze/annotations.py#L225.

Is there a bugs.python.org issue about this?

@baldurmen
Copy link

FWIW, this means I can't build typing_inspect in Debian anymore :(

https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=972803

Hopefully someone can implement a fix soonish, since it won't move from unstable to testing until this is fixed.

archlinux-github pushed a commit to archlinux/svntogit-community that referenced this issue Nov 19, 2020
…evkivskyi/typing_inspect#60 (comment))

git-svn-id: file:///srv/repos/svn-community/svn@757689 9fca08f4-af9d-4005-b8df-a31f2cc04f65
archlinux-github pushed a commit to archlinux/svntogit-community that referenced this issue Nov 19, 2020
…evkivskyi/typing_inspect#60 (comment))

git-svn-id: file:///srv/repos/svn-community/svn@757689 9fca08f4-af9d-4005-b8df-a31f2cc04f65
Apteryks added a commit to Apteryks/typing_inspect that referenced this issue Jan 15, 2021
ilevkivskyi pushed a commit that referenced this issue Mar 21, 2021
Another take at #66 which keeps compatibility with older Python versions.

Fixes #60
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants