1
1
-- Type checking of union types with '|' syntax
2
2
3
3
[case testUnionOrSyntaxWithTwoBuiltinsTypes]
4
- # flags: --python-version 3.9
4
+ # flags: --python-version 3.10
5
+ from __future__ import annotations
5
6
def f(x: int | str) -> int | str:
6
7
reveal_type(x) # N: Revealed type is 'Union[builtins.int, builtins.str]'
7
8
z: int | str = 0
8
9
reveal_type(z) # N: Revealed type is 'Union[builtins.int, builtins.str]'
9
10
return x
10
11
reveal_type(f) # N: Revealed type is 'def (x: Union[builtins.int, builtins.str]) -> Union[builtins.int, builtins.str]'
12
+ [builtins fixtures/tuple.pyi]
11
13
12
14
[case testUnionOrSyntaxWithThreeBuiltinsTypes]
13
- # flags: --python-version 3.9
15
+ # flags: --python-version 3.10
14
16
def f(x: int | str | float) -> int | str | float:
15
17
reveal_type(x) # N: Revealed type is 'Union[builtins.int, builtins.str, builtins.float]'
16
18
z: int | str | float = 0
@@ -20,7 +22,7 @@ def f(x: int | str | float) -> int | str | float:
20
22
reveal_type(f) # N: Revealed type is 'def (x: Union[builtins.int, builtins.str, builtins.float]) -> Union[builtins.int, builtins.str, builtins.float]'
21
23
22
24
[case testUnionOrSyntaxWithTwoTypes]
23
- # flags: --python-version 3.9
25
+ # flags: --python-version 3.10
24
26
class A: pass
25
27
class B: pass
26
28
def f(x: A | B) -> A | B:
@@ -31,7 +33,7 @@ def f(x: A | B) -> A | B:
31
33
reveal_type(f) # N: Revealed type is 'def (x: Union[__main__.A, __main__.B]) -> Union[__main__.A, __main__.B]'
32
34
33
35
[case testUnionOrSyntaxWithThreeTypes]
34
- # flags: --python-version 3.9
36
+ # flags: --python-version 3.10
35
37
class A: pass
36
38
class B: pass
37
39
class C: pass
@@ -43,21 +45,37 @@ def f(x: A | B | C) -> A | B | C:
43
45
reveal_type(f) # N: Revealed type is 'def (x: Union[__main__.A, __main__.B, __main__.C]) -> Union[__main__.A, __main__.B, __main__.C]'
44
46
45
47
[case testUnionOrSyntaxWithLiteral]
46
- # flags: --python-version 3.9
48
+ # flags: --python-version 3.10
47
49
from typing_extensions import Literal
48
50
reveal_type(Literal[4] | str) # N: Revealed type is 'Any'
49
51
[builtins fixtures/tuple.pyi]
50
52
51
53
[case testUnionOrSyntaxWithBadOperator]
52
- # flags: --python-version 3.9
54
+ # flags: --python-version 3.10
53
55
x: 1 + 2 # E: Invalid type comment or annotation
54
56
55
57
[case testUnionOrSyntaxWithBadOperands]
56
- # flags: --python-version 3.9
58
+ # flags: --python-version 3.10
57
59
x: int | 42 # E: Invalid type: try using Literal[42] instead?
58
60
y: 42 | int # E: Invalid type: try using Literal[42] instead?
59
61
z: str | 42 | int # E: Invalid type: try using Literal[42] instead?
60
62
61
63
[case testUnionOrSyntaxInComment]
62
- # flags: --python-version 3.9
64
+ # flags: --python-version 3.10
63
65
x = 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