You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 56618b9
Browse filesBrowse the repository at this point in the historyBrowse files
Support union type syntax in runtime contexts (#10770)
Support the X | Y syntax (PEP 604) in type aliases, casts, type applications
and base classes.
These are only available in Python 3.10 mode and probably won't work in
stubs when targeting earlier Python versions yet.
Work on #9880.
reveal_type(g) # N: Revealed type is "def (x: Union[builtins.int, builtins.str, None]) -> Union[builtins.int, None]"
89
81
90
-
91
82
[case testUnionOrSyntaxWithQuotedVariableTypes]
92
83
# flags: --python-version 3.6
93
84
y: "int | str" = 42
94
85
reveal_type(y) # N: Revealed type is "Union[builtins.int, builtins.str]"
95
86
96
-
97
87
[case testUnionOrSyntaxWithTypeAliasWorking]
98
88
# flags: --python-version 3.10
99
-
from typing import Union
100
-
T = Union[int, str]
89
+
T = int | str
101
90
x: T
102
91
reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]"
92
+
S = list[int] | str | None
93
+
y: S
94
+
reveal_type(y) # N: Revealed type is "Union[builtins.list[builtins.int], builtins.str, None]"
95
+
U = str | None
96
+
z: U
97
+
reveal_type(z) # N: Revealed type is "Union[builtins.str, None]"
98
+
99
+
def f(): pass
103
100
101
+
X = int | str | f()
102
+
b: X # E: Variable "__main__.X" is not valid as a type \
103
+
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
0 commit comments