I read PEP 585 (Type Hinting Generics In Standard Collections)

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.

Overview

the term

approach

Use list and dict instead of the previous typing.List and typing.Dict. that's all.

def find(haystack: dict[str, list[int]]) -> int:
    ...

What not to do

Note

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,)

Impressions

Postscript

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

I read PEP 585 (Type Hinting Generics In Standard Collections)
I read PEP 613 (Explicit Type Aliases)
I read "Linux standard textbook"!
Mastering type hinting in PyCharm
I can't enter standard in Subprocess ...
I read PEP 612 (Parameter Specification Variables)
I read PEP 604 (Complementary syntax for Union []).
I read PEP-362 (Function Signature Object) Memo
I read PEP 618 (Add Optional Length-Checking To zip)
I read PEP 584 (Add Union Operators To dict)
I read PEP 614 (Relaxing Grammar Restrictions On Decorators)
I read PEP-593 (Flexible function and variable annotations)