|
| 1 | +-- Type checking of union types with '|' syntax |
| 2 | + |
| 3 | +[case testUnionOrSyntaxWithTwoBuiltinsTypes] |
| 4 | +# flags: --python-version 3.10 |
| 5 | +from __future__ import annotations |
| 6 | +def f(x: int | str) -> int | str: |
| 7 | + reveal_type(x) # N: Revealed type is 'Union[builtins.int, builtins.str]' |
| 8 | + z: int | str = 0 |
| 9 | + reveal_type(z) # N: Revealed type is 'Union[builtins.int, builtins.str]' |
| 10 | + return x |
| 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] |
| 13 | + |
| 14 | + |
| 15 | +[case testUnionOrSyntaxWithThreeBuiltinsTypes] |
| 16 | +# flags: --python-version 3.10 |
| 17 | +def f(x: int | str | float) -> int | str | float: |
| 18 | + reveal_type(x) # N: Revealed type is 'Union[builtins.int, builtins.str, builtins.float]' |
| 19 | + z: int | str | float = 0 |
| 20 | + reveal_type(z) # N: Revealed type is 'Union[builtins.int, builtins.str, builtins.float]' |
| 21 | + return x |
| 22 | +reveal_type(f) # N: Revealed type is 'def (x: Union[builtins.int, builtins.str, builtins.float]) -> Union[builtins.int, builtins.str, builtins.float]' |
| 23 | + |
| 24 | + |
| 25 | +[case testUnionOrSyntaxWithTwoTypes] |
| 26 | +# flags: --python-version 3.10 |
| 27 | +class A: pass |
| 28 | +class B: pass |
| 29 | +def f(x: A | B) -> A | B: |
| 30 | + reveal_type(x) # N: Revealed type is 'Union[__main__.A, __main__.B]' |
| 31 | + z: A | B = A() |
| 32 | + reveal_type(z) # N: Revealed type is 'Union[__main__.A, __main__.B]' |
| 33 | + return x |
| 34 | +reveal_type(f) # N: Revealed type is 'def (x: Union[__main__.A, __main__.B]) -> Union[__main__.A, __main__.B]' |
| 35 | + |
| 36 | + |
| 37 | +[case testUnionOrSyntaxWithThreeTypes] |
| 38 | +# flags: --python-version 3.10 |
| 39 | +class A: pass |
| 40 | +class B: pass |
| 41 | +class C: pass |
| 42 | +def f(x: A | B | C) -> A | B | C: |
| 43 | + reveal_type(x) # N: Revealed type is 'Union[__main__.A, __main__.B, __main__.C]' |
| 44 | + z: A | B | C = A() |
| 45 | + reveal_type(z) # N: Revealed type is 'Union[__main__.A, __main__.B, __main__.C]' |
| 46 | + return x |
| 47 | +reveal_type(f) # N: Revealed type is 'def (x: Union[__main__.A, __main__.B, __main__.C]) -> Union[__main__.A, __main__.B, __main__.C]' |
| 48 | + |
| 49 | + |
| 50 | +[case testUnionOrSyntaxWithLiteral] |
| 51 | +# flags: --python-version 3.10 |
| 52 | +from typing_extensions import Literal |
| 53 | +reveal_type(Literal[4] | str) # N: Revealed type is 'Any' |
| 54 | +[builtins fixtures/tuple.pyi] |
| 55 | + |
| 56 | + |
| 57 | +[case testUnionOrSyntaxWithBadOperator] |
| 58 | +# flags: --python-version 3.10 |
| 59 | +x: 1 + 2 # E: Invalid type comment or annotation |
| 60 | + |
| 61 | + |
| 62 | +[case testUnionOrSyntaxWithBadOperands] |
| 63 | +# flags: --python-version 3.10 |
| 64 | +x: int | 42 # E: Invalid type: try using Literal[42] instead? |
| 65 | +y: 42 | int # E: Invalid type: try using Literal[42] instead? |
| 66 | +z: str | 42 | int # E: Invalid type: try using Literal[42] instead? |
| 67 | + |
| 68 | + |
| 69 | +[case testUnionOrSyntaxWithGenerics] |
| 70 | +# flags: --python-version 3.10 |
| 71 | +from typing import List |
| 72 | +x: List[int | str] |
| 73 | +reveal_type(x) # N: Revealed type is 'builtins.list[Union[builtins.int, builtins.str]]' |
| 74 | +[builtins fixtures/list.pyi] |
| 75 | + |
| 76 | + |
| 77 | +[case testUnionOrSyntaxWithQuotedFunctionTypes] |
| 78 | +# flags: --python-version 3.4 |
| 79 | +from typing import Union |
| 80 | +def f(x: 'Union[int, str, None]') -> 'Union[int, None]': |
| 81 | + reveal_type(x) # N: Revealed type is 'Union[builtins.int, builtins.str, None]' |
| 82 | + return 42 |
| 83 | +reveal_type(f) # N: Revealed type is 'def (x: Union[builtins.int, builtins.str, None]) -> Union[builtins.int, None]' |
| 84 | + |
| 85 | +def g(x: "int | str | None") -> "int | None": |
| 86 | + reveal_type(x) # N: Revealed type is 'Union[builtins.int, builtins.str, None]' |
| 87 | + return 42 |
| 88 | +reveal_type(g) # N: Revealed type is 'def (x: Union[builtins.int, builtins.str, None]) -> Union[builtins.int, None]' |
| 89 | + |
| 90 | + |
| 91 | +[case testUnionOrSyntaxWithQuotedVariableTypes] |
| 92 | +# flags: --python-version 3.6 |
| 93 | +y: "int | str" = 42 |
| 94 | +reveal_type(y) # N: Revealed type is 'Union[builtins.int, builtins.str]' |
| 95 | + |
| 96 | + |
| 97 | +[case testUnionOrSyntaxWithTypeAliasWorking] |
| 98 | +# flags: --python-version 3.10 |
| 99 | +from typing import Union |
| 100 | +T = Union[int, str] |
| 101 | +x: T |
| 102 | +reveal_type(x) # N: Revealed type is 'Union[builtins.int, builtins.str]' |
| 103 | + |
| 104 | + |
| 105 | +[case testUnionOrSyntaxWithTypeAliasNotAllowed] |
| 106 | +# flags: --python-version 3.9 |
| 107 | +from __future__ import annotations |
| 108 | +T = int | str # E: Unsupported left operand type for | ("Type[int]") |
| 109 | +[builtins fixtures/tuple.pyi] |
| 110 | + |
| 111 | + |
| 112 | +[case testUnionOrSyntaxInComment] |
| 113 | +# flags: --python-version 3.6 |
| 114 | +x = 1 # type: int | str |
| 115 | + |
| 116 | + |
| 117 | +[case testUnionOrSyntaxFutureImport] |
| 118 | +# flags: --python-version 3.7 |
| 119 | +from __future__ import annotations |
| 120 | +x: int | None |
| 121 | +[builtins fixtures/tuple.pyi] |
| 122 | + |
| 123 | + |
| 124 | +[case testUnionOrSyntaxMissingFutureImport] |
| 125 | +# flags: --python-version 3.9 |
| 126 | +x: int | None # E: X | Y syntax for unions requires Python 3.10 |
| 127 | + |
| 128 | + |
| 129 | +[case testUnionOrSyntaxInStubFile] |
| 130 | +# flags: --python-version 3.6 |
| 131 | +from lib import x |
| 132 | +[file lib.pyi] |
| 133 | +x: int | None |
0 commit comments