I read PEP 604 (Complementary syntax for Union []).

When I heard that Microsoft's checker for Python pyright supports the latest specifications and scanned the README, I hadn't watched it before. PEP 604 (Complementary syntax for Union []) was listed, so I took a look. By the way, I will write about PEP-612 in the next article.

Overview

approach

that's all.

Example

The sample in PEP is self-explanatory, so I won't explain it.

# in place of
# def f(list: List[Union[int, str]], param: Optional[int]) -> Union[float, str]
def f(list: List[int | str], param: int | None) -> float | str:
    pass

f([1, "abc"], None)

assert str | int == Union[str,int]
assert str | int | float == Union[str, int, float]

assert isinstance("", int | str)
assert issubclass(bool, int | float)

Impressions

Recommended Posts

I read PEP 604 (Complementary syntax for Union []).
I read PEP 584 (Add Union Operators To dict)
I read PEP 613 (Explicit Type Aliases)
I read PEP 612 (Parameter Specification Variables)
I read PEP 560 (Core support for typing module and generic types)
I read PEP-362 (Function Signature Object) Memo
I read PEP 614 (Relaxing Grammar Restrictions On Decorators)
I read PEP-593 (Flexible function and variable annotations)
I read PEP-544 (Protocols: Structural subtyping (static duck typing)).
I read PEP 585 (Type Hinting Generics In Standard Collections)