| 1 |
-- Type checking of union types with '|' syntax |
| 2 |
|
| 3 |
[case testUnionOrSyntaxWithTwoBuiltinsTypes] |
| 4 |
from __future__ import annotations |
| 5 |
def f(x: int | str) -> int | str: |
| 6 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 7 |
z: int | str = 0 |
| 8 |
reveal_type(z) # N: Revealed type is "builtins.int | builtins.str" |
| 9 |
return x |
| 10 |
reveal_type(f) # N: Revealed type is "def (x: builtins.int | builtins.str) -> builtins.int | builtins.str" |
| 11 |
[builtins fixtures/tuple.pyi] |
| 12 |
|
| 13 |
[case testUnionOrSyntaxWithThreeBuiltinsTypes] |
| 14 |
def f(x: int | str | float) -> int | str | float: |
| 15 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | builtins.float" |
| 16 |
z: int | str | float = 0 |
| 17 |
reveal_type(z) # N: Revealed type is "builtins.int | builtins.str | builtins.float" |
| 18 |
return x |
| 19 |
reveal_type(f) # N: Revealed type is "def (x: builtins.int | builtins.str | builtins.float) -> builtins.int | builtins.str | builtins.float" |
| 20 |
|
| 21 |
[case testUnionOrSyntaxWithTwoTypes] |
| 22 |
class A: pass |
| 23 |
class B: pass |
| 24 |
def f(x: A | B) -> A | B: |
| 25 |
reveal_type(x) # N: Revealed type is "__main__.A | __main__.B" |
| 26 |
z: A | B = A() |
| 27 |
reveal_type(z) # N: Revealed type is "__main__.A | __main__.B" |
| 28 |
return x |
| 29 |
reveal_type(f) # N: Revealed type is "def (x: __main__.A | __main__.B) -> __main__.A | __main__.B" |
| 30 |
|
| 31 |
[case testUnionOrSyntaxWithThreeTypes] |
| 32 |
class A: pass |
| 33 |
class B: pass |
| 34 |
class C: pass |
| 35 |
def f(x: A | B | C) -> A | B | C: |
| 36 |
reveal_type(x) # N: Revealed type is "__main__.A | __main__.B | __main__.C" |
| 37 |
z: A | B | C = A() |
| 38 |
reveal_type(z) # N: Revealed type is "__main__.A | __main__.B | __main__.C" |
| 39 |
return x |
| 40 |
reveal_type(f) # N: Revealed type is "def (x: __main__.A | __main__.B | __main__.C) -> __main__.A | __main__.B | __main__.C" |
| 41 |
|
| 42 |
[case testUnionOrSyntaxWithLiteral] |
| 43 |
from typing import Literal |
| 44 |
reveal_type(Literal[4] | str) # N: Revealed type is "Any" |
| 45 |
[builtins fixtures/tuple.pyi] |
| 46 |
[typing fixtures/typing-full.pyi] |
| 47 |
|
| 48 |
[case testUnionOrSyntaxWithBadOperator] |
| 49 |
x: 1 + 2 # E: Invalid type comment or annotation |
| 50 |
|
| 51 |
[case testUnionOrSyntaxWithBadOperands] |
| 52 |
x: int | 42 # E: Invalid type: try using Literal[42] instead? |
| 53 |
y: 42 | int # E: Invalid type: try using Literal[42] instead? |
| 54 |
z: str | 42 | int # E: Invalid type: try using Literal[42] instead? |
| 55 |
|
| 56 |
[case testUnionOrSyntaxWithGenerics] |
| 57 |
from typing import List |
| 58 |
x: List[int | str] |
| 59 |
reveal_type(x) # N: Revealed type is "builtins.list[builtins.int | builtins.str]" |
| 60 |
[builtins fixtures/list.pyi] |
| 61 |
|
| 62 |
[case testUnionOrSyntaxWithQuotedFunctionTypes] |
| 63 |
from typing import Union |
| 64 |
def f(x: 'Union[int, str, None]') -> 'Union[int, None]': |
| 65 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | None" |
| 66 |
return 42 |
| 67 |
reveal_type(f) # N: Revealed type is "def (x: builtins.int | builtins.str | None) -> builtins.int | None" |
| 68 |
|
| 69 |
def g(x: "int | str | None") -> "int | None": |
| 70 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | None" |
| 71 |
return 42 |
| 72 |
reveal_type(g) # N: Revealed type is "def (x: builtins.int | builtins.str | None) -> builtins.int | None" |
| 73 |
|
| 74 |
[case testUnionOrSyntaxWithQuotedVariableTypes] |
| 75 |
y: "int | str" = 42 |
| 76 |
reveal_type(y) # N: Revealed type is "builtins.int | builtins.str" |
| 77 |
|
| 78 |
[case testUnionOrSyntaxWithTypeAliasWorking] |
| 79 |
T = int | str |
| 80 |
x: T |
| 81 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 82 |
S = list[int] | str | None |
| 83 |
y: S |
| 84 |
reveal_type(y) # N: Revealed type is "builtins.list[builtins.int] | builtins.str | None" |
| 85 |
U = str | None |
| 86 |
z: U |
| 87 |
reveal_type(z) # N: Revealed type is "builtins.str | None" |
| 88 |
|
| 89 |
def f(): pass |
| 90 |
|
| 91 |
X = int | str | f() |
| 92 |
b: X # E: Variable "__main__.X" is not valid as a type \ |
| 93 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases |
| 94 |
[builtins fixtures/type.pyi] |
| 95 |
|
| 96 |
[case testUnionOrSyntaxInComment] |
| 97 |
x = 1 # type: int | str |
| 98 |
|
| 99 |
[case testUnionOrSyntaxFutureImport] |
| 100 |
from __future__ import annotations |
| 101 |
x: int | None |
| 102 |
[builtins fixtures/tuple.pyi] |
| 103 |
|
| 104 |
[case testUnionOrSyntaxInStubFile] |
| 105 |
from lib import x |
| 106 |
[file lib.pyi] |
| 107 |
x: int | None |
| 108 |
|
| 109 |
[case testUnionOrSyntaxInMiscRuntimeContexts] |
| 110 |
from typing import cast |
| 111 |
|
| 112 |
class C(list[int | None]): |
| 113 |
pass |
| 114 |
|
| 115 |
def f() -> object: pass |
| 116 |
|
| 117 |
reveal_type(cast(str | None, f())) # N: Revealed type is "builtins.str | None" |
| 118 |
reveal_type(list[str | None]()) # N: Revealed type is "builtins.list[builtins.str | None]" |
| 119 |
[builtins fixtures/type.pyi] |
| 120 |
|
| 121 |
[case testUnionOrSyntaxRuntimeContextInStubFile] |
| 122 |
import lib |
| 123 |
reveal_type(lib.x) # N: Revealed type is "builtins.int | builtins.list[builtins.str] | None" |
| 124 |
reveal_type(lib.y) # N: Revealed type is "builtins.list[builtins.int | None]" |
| 125 |
|
| 126 |
[file lib.pyi] |
| 127 |
A = int | list[str] | None |
| 128 |
x: A |
| 129 |
B = list[int | None] |
| 130 |
y: B |
| 131 |
class C(list[int | None]): |
| 132 |
pass |
| 133 |
[builtins fixtures/type.pyi] |
| 134 |
|
| 135 |
[case testUnionOrSyntaxInIsinstance] |
| 136 |
class C: pass |
| 137 |
|
| 138 |
def f(x: int | str | C) -> None: |
| 139 |
if isinstance(x, int | str): |
| 140 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 141 |
else: |
| 142 |
reveal_type(x) # N: Revealed type is "__main__.C" |
| 143 |
|
| 144 |
def g(x: int | str | tuple[int, str] | C) -> None: |
| 145 |
if isinstance(x, int | str | tuple): |
| 146 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | tuple[builtins.int, builtins.str]" |
| 147 |
else: |
| 148 |
reveal_type(x) # N: Revealed type is "__main__.C" |
| 149 |
[builtins fixtures/isinstance_python3_10.pyi] |
| 150 |
|
| 151 |
[case testUnionOrSyntaxInIsinstanceNotSupported] |
| 152 |
from typing import Union |
| 153 |
def f(x: Union[int, str, None]) -> None: |
| 154 |
if isinstance(x, int | str): |
| 155 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 156 |
else: |
| 157 |
reveal_type(x) # N: Revealed type is "None" |
| 158 |
[builtins fixtures/isinstance.pyi] |
| 159 |
|
| 160 |
[case testImplicit604TypeAliasWithCyclicImportInStub] |
| 161 |
from was_builtins import foo |
| 162 |
reveal_type(foo) # N: Revealed type is "builtins.str | was_mmap.mmap" |
| 163 |
[file was_builtins.pyi] |
| 164 |
import was_mmap |
| 165 |
WriteableBuffer = was_mmap.mmap |
| 166 |
ReadableBuffer = str | WriteableBuffer |
| 167 |
foo: ReadableBuffer |
| 168 |
[file was_mmap.pyi] |
| 169 |
from was_builtins import * |
| 170 |
class mmap: ... |
| 171 |
[builtins fixtures/type.pyi] |
| 172 |
|
| 173 |
[case testTypeAliasWithNewUnionIsInstance] |
| 174 |
SimpleAlias = int | str |
| 175 |
|
| 176 |
def foo(x: int | str | tuple): |
| 177 |
if isinstance(x, SimpleAlias): |
| 178 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 179 |
else: |
| 180 |
reveal_type(x) # N: Revealed type is "builtins.tuple[Any, ...]" |
| 181 |
|
| 182 |
ParameterizedAlias = str | list[str] |
| 183 |
|
| 184 |
# these are false negatives: |
| 185 |
isinstance(5, str | list[str]) |
| 186 |
isinstance(5, ParameterizedAlias) |
| 187 |
[builtins fixtures/type.pyi] |
| 188 |
|
| 189 |
[case testIsInstanceUnionNone] |
| 190 |
def foo(value: str | bool | None): |
| 191 |
assert not isinstance(value, str | None) |
| 192 |
reveal_type(value) # N: Revealed type is "builtins.bool" |
| 193 |
|
| 194 |
def bar(value: object): |
| 195 |
assert isinstance(value, str | None) |
| 196 |
reveal_type(value) # N: Revealed type is "builtins.str | None" |
| 197 |
[builtins fixtures/type.pyi] |
| 198 |
|
| 199 |
|
| 200 |
# TODO: Get this test to pass |
| 201 |
[case testImplicit604TypeAliasWithCyclicImportNotInStub-xfail] |
| 202 |
from was_builtins import foo |
| 203 |
reveal_type(foo) # N: Revealed type is "Union[builtins.str, was_mmap.mmap]" |
| 204 |
[file was_builtins.py] |
| 205 |
import was_mmap |
| 206 |
WriteableBuffer = was_mmap.mmap |
| 207 |
ReadableBuffer = str | WriteableBuffer |
| 208 |
foo: ReadableBuffer |
| 209 |
[file was_mmap.py] |
| 210 |
from was_builtins import * |
| 211 |
class mmap: ... |