When I was investigating where the attribute __origin__ came from, a PEP called PEP 585 --Type Hinting Generics In Standard Collections I found it, so I forgot the original purpose and skimmed it. I'm still in Draft status, so I'm not sure if it will be adopted in the future, but I'll make a note of my understanding.
typing.List and typing.Dict).list and dict) for type annotationtypingdictdict [str, int]Use list and dict instead of the previous typing.List and typing.Dict. that's all.
def find(haystack: dict[str, list[int]]) -> int:
...
Internally, it is supposed to create an instance of types.GenericAlias. Therefore, you can use __origin__ or __args__ to retrieve type information.
StrList = list[str]
assert isinstance(StrList, types.GenericAlias)
assert StrList.__origin__ is list
assert StrList.__args__ is (str,)
Before I knew it, it was decided to adopt PEP 585 (Accepted), and it was available in 3.9.0a6. Below is the state of 3.9-dev installed a while ago.
$ python3.9
Python 3.9.0a6+ (heads/master:7f7e706, May 9 2020, 13:35:20)
[Clang 11.0.3 (clang-1103.0.32.59)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> list[int]
list[int]
>>> dict[str, int]
dict[str, int]
>>> tuple[int, ...]
tuple[int, ...]
Recommended Posts