ilevkivskyi / typing_inspect

Runtime inspection utilities for Python typing module
MIT License
350 stars 35 forks source link

Python 3.9 support breaks List #73

Closed martinjm97 closed 3 years ago

martinjm97 commented 3 years ago

There was a regression in release 0.7.0 from 0.6.0.

Currently,

>>> from typing import List, Set
>>> from typing_inspect import get_args
>>> get_args(list[str])  # Yay this works! Great to see support for Python 3.9!
(<class 'str'>,)
>>> get_args(List)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jessemichel/Dependencies/miniconda3/envs/py39/lib/python3.9/site-packages/typing_inspect.py", line 471, in get_args
    res = tp.__args__
  File "/Users/jessemichel/Dependencies/miniconda3/envs/py39/lib/python3.9/typing.py", line 694, in __getattr__
    raise AttributeError(attr)
AttributeError: __args__

Previously,

>>> get_args(List)
(~T,)

I believe the fix would be changing this line: https://github.com/ilevkivskyi/typing_inspect/blob/d1d46f4c2429dd0a60f902fa7b728d67bb2d1582/typing_inspect.py#L471 to

res = tp.__args__ if hasattr(tp, '__args__') else ()
ilevkivskyi commented 3 years ago

Thanks! I applied a similar fix.

ilevkivskyi commented 3 years ago

Also published this as 0.7.1.