11-- Type checking of union types with '|' syntax
22
33[case testUnionOrSyntaxWithTwoBuiltinsTypes]
4- # flags: --python-version 3.9
4+ # flags: --python-version 3.10
5+ from __future__ import annotations
56def f(x: int | str) -> int | str:
67 reveal_type(x) # N: Revealed type is 'Union[builtins.int, builtins.str]'
78 z: int | str = 0
89 reveal_type(z) # N: Revealed type is 'Union[builtins.int, builtins.str]'
910 return x
1011reveal_type(f) # N: Revealed type is 'def (x: Union[builtins.int, builtins.str]) -> Union[builtins.int, builtins.str]'
12+ [builtins fixtures/tuple.pyi]
1113
1214[case testUnionOrSyntaxWithThreeBuiltinsTypes]
13- # flags: --python-version 3.9
15+ # flags: --python-version 3.10
1416def f(x: int | str | float) -> int | str | float:
1517 reveal_type(x) # N: Revealed type is 'Union[builtins.int, builtins.str, builtins.float]'
1618 z: int | str | float = 0
@@ -20,7 +22,7 @@ def f(x: int | str | float) -> int | str | float:
2022reveal_type(f) # N: Revealed type is 'def (x: Union[builtins.int, builtins.str, builtins.float]) -> Union[builtins.int, builtins.str, builtins.float]'
2123
2224[case testUnionOrSyntaxWithTwoTypes]
23- # flags: --python-version 3.9
25+ # flags: --python-version 3.10
2426class A: pass
2527class B: pass
2628def f(x: A | B) -> A | B:
@@ -31,7 +33,7 @@ def f(x: A | B) -> A | B:
3133reveal_type(f) # N: Revealed type is 'def (x: Union[__main__.A, __main__.B]) -> Union[__main__.A, __main__.B]'
3234
3335[case testUnionOrSyntaxWithThreeTypes]
34- # flags: --python-version 3.9
36+ # flags: --python-version 3.10
3537class A: pass
3638class B: pass
3739class C: pass
@@ -43,21 +45,37 @@ def f(x: A | B | C) -> A | B | C:
4345reveal_type(f) # N: Revealed type is 'def (x: Union[__main__.A, __main__.B, __main__.C]) -> Union[__main__.A, __main__.B, __main__.C]'
4446
4547[case testUnionOrSyntaxWithLiteral]
46- # flags: --python-version 3.9
48+ # flags: --python-version 3.10
4749from typing_extensions import Literal
4850reveal_type(Literal[4] | str) # N: Revealed type is 'Any'
4951[builtins fixtures/tuple.pyi]
5052
5153[case testUnionOrSyntaxWithBadOperator]
52- # flags: --python-version 3.9
54+ # flags: --python-version 3.10
5355x: 1 + 2 # E: Invalid type comment or annotation
5456
5557[case testUnionOrSyntaxWithBadOperands]
56- # flags: --python-version 3.9
58+ # flags: --python-version 3.10
5759x: int | 42 # E: Invalid type: try using Literal[42] instead?
5860y: 42 | int # E: Invalid type: try using Literal[42] instead?
5961z: str | 42 | int # E: Invalid type: try using Literal[42] instead?
6062
6163[case testUnionOrSyntaxInComment]
62- # flags: --python-version 3.9
64+ # flags: --python-version 3.10
6365x = 1 # type: int | str
66+
67+ [case testUnionOrSyntaxFutureImport]
68+ # flags: --python-version 3.7
69+ from __future__ import annotations
70+ x: int | None
71+ [builtins fixtures/tuple.pyi]
72+
73+ [case testUnionOrSyntaxMissingFutureImport]
74+ # flags: --python-version 3.9
75+ x: int | None # E: Alternative syntax for unions requires Python 3.10 or newer
76+
77+ [case testUnionOrSyntaxInStubFile]
78+ # flags: --python-version 3.6
79+ from lib import x
80+ [file lib.pyi]
81+ x: int | None
0 commit comments