| 1 |
-- Type checking of union types |
| 2 |
|
| 3 |
[case testUnion1] |
| 4 |
from typing import Union |
| 5 |
def f(x: Union[int, str]) -> None: |
| 6 |
if isinstance(x, int): |
| 7 |
y = 1 |
| 8 |
y = x |
| 9 |
elif isinstance(x, str): |
| 10 |
z = 'a' |
| 11 |
z = x |
| 12 |
[builtins fixtures/isinstance.pyi] |
| 13 |
|
| 14 |
[case testUnion2] |
| 15 |
from typing import Union |
| 16 |
def f(x: Union[int, str]) -> None: |
| 17 |
if isinstance(x, int): |
| 18 |
y = 1 |
| 19 |
y = x |
| 20 |
else: |
| 21 |
z = 'a' |
| 22 |
z = x |
| 23 |
[builtins fixtures/isinstance.pyi] |
| 24 |
|
| 25 |
[case testUnion3] |
| 26 |
from typing import Union |
| 27 |
def f(x: Union[int, str]) -> None: |
| 28 |
if isinstance(x, int): |
| 29 |
y = 1 |
| 30 |
if int(): |
| 31 |
y = x |
| 32 |
else: |
| 33 |
z = 2 |
| 34 |
if int(): |
| 35 |
z = x # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 36 |
[builtins fixtures/isinstance.pyi] |
| 37 |
[out] |
| 38 |
|
| 39 |
[case testUnionAnyIsInstance] |
| 40 |
from typing import Any, Union |
| 41 |
|
| 42 |
def func(v: Union[int, Any]) -> None: |
| 43 |
if isinstance(v, int): |
| 44 |
reveal_type(v) # N: Revealed type is "builtins.int" |
| 45 |
else: |
| 46 |
reveal_type(v) # N: Revealed type is "Any" |
| 47 |
[builtins fixtures/isinstance.pyi] |
| 48 |
[out] |
| 49 |
|
| 50 |
[case testUnionAttributeAccess] |
| 51 |
from typing import Union |
| 52 |
|
| 53 |
class A: y = 1 |
| 54 |
class B: y = 2 |
| 55 |
class C: pass |
| 56 |
class D: pass |
| 57 |
|
| 58 |
u: Union[A, C, D] |
| 59 |
v: Union[C, D] |
| 60 |
w: Union[A, B] |
| 61 |
x: Union[A, C] |
| 62 |
y: int |
| 63 |
z: str |
| 64 |
|
| 65 |
if int(): |
| 66 |
y = w.y |
| 67 |
v.y # E: Item "C" of "C | D" has no attribute "y" \ |
| 68 |
# E: Item "D" of "C | D" has no attribute "y" |
| 69 |
u.y # E: Item "C" of "A | C | D" has no attribute "y" \ |
| 70 |
# E: Item "D" of "A | C | D" has no attribute "y" |
| 71 |
if int(): |
| 72 |
z = w.y # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 73 |
w.y = 'a' # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 74 |
if int(): |
| 75 |
y = x.y # E: Item "C" of "A | C" has no attribute "y" |
| 76 |
zz = x.y # E: Item "C" of "A | C" has no attribute "y" |
| 77 |
if int(): |
| 78 |
z = zz # E: Incompatible types in assignment (expression has type "int | Any", variable has type "str") |
| 79 |
|
| 80 |
[builtins fixtures/isinstance.pyi] |
| 81 |
|
| 82 |
[case testUnionMethodCalls] |
| 83 |
from typing import Union |
| 84 |
|
| 85 |
class A: |
| 86 |
def foo(self) -> int: pass |
| 87 |
class B: |
| 88 |
def foo(self) -> int: pass |
| 89 |
class C: |
| 90 |
def foo(self) -> str: pass |
| 91 |
|
| 92 |
x: Union[A, B] |
| 93 |
y: Union[A, C] |
| 94 |
i: int |
| 95 |
|
| 96 |
x.foo() |
| 97 |
y.foo() |
| 98 |
i = x.foo() |
| 99 |
if int(): |
| 100 |
i = y.foo() # E: Incompatible types in assignment (expression has type "int | str", variable has type "int") |
| 101 |
|
| 102 |
[builtins fixtures/isinstance.pyi] |
| 103 |
|
| 104 |
[case testUnionIndexing] |
| 105 |
from typing import Union, List |
| 106 |
x: Union[List[int], str] |
| 107 |
x[2] |
| 108 |
x[2] + 1 # E: Unsupported operand types for + ("str" and "int") \ |
| 109 |
# N: Left operand is of type "int | str" |
| 110 |
[builtins fixtures/isinstancelist.pyi] |
| 111 |
|
| 112 |
[case testUnionAsOverloadArg] |
| 113 |
from foo import * |
| 114 |
x = 0 |
| 115 |
if int(): |
| 116 |
x = f(1) |
| 117 |
if int(): |
| 118 |
x = f('') |
| 119 |
s = '' |
| 120 |
if int(): |
| 121 |
s = f(int) |
| 122 |
if int(): |
| 123 |
s = f(1) # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 124 |
if int(): |
| 125 |
x = f(int) # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 126 |
|
| 127 |
[file foo.pyi] |
| 128 |
from typing import Union, overload |
| 129 |
@overload |
| 130 |
def f(x: Union[int, str]) -> int: pass |
| 131 |
@overload |
| 132 |
def f(x: type) -> str: pass |
| 133 |
|
| 134 |
[case testUnionWithNoneItem] |
| 135 |
# flags: --no-strict-optional |
| 136 |
from typing import Union |
| 137 |
def f() -> Union[int, None]: pass |
| 138 |
x = 1 |
| 139 |
x = f() |
| 140 |
|
| 141 |
[case testUnionWithEllipsis] |
| 142 |
from typing import Union |
| 143 |
def f(x: Union[int, EllipsisType]) -> int: |
| 144 |
if x is Ellipsis: |
| 145 |
reveal_type(x) # N: Revealed type is "builtins.ellipsis" |
| 146 |
x = 1 |
| 147 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 148 |
return x |
| 149 |
[builtins fixtures/isinstancelist.pyi] |
| 150 |
|
| 151 |
[case testOptional] |
| 152 |
from typing import Optional |
| 153 |
def f(x: Optional[int]) -> None: pass |
| 154 |
f(1) |
| 155 |
f(None) |
| 156 |
f('') # E: Argument 1 to "f" has incompatible type "str"; expected "int | None" |
| 157 |
|
| 158 |
[case testUnionWithNoReturn] |
| 159 |
from typing import Union, NoReturn |
| 160 |
def f() -> Union[int, NoReturn]: ... |
| 161 |
reveal_type(f()) # N: Revealed type is "builtins.int" |
| 162 |
|
| 163 |
[case testUnionSimplificationGenericFunction] |
| 164 |
from typing import TypeVar, Union, List |
| 165 |
T = TypeVar('T') |
| 166 |
def f(x: List[T]) -> Union[T, int]: pass |
| 167 |
def g(y: str) -> None: pass |
| 168 |
a = f([1]) |
| 169 |
g(a) # E: Argument 1 to "g" has incompatible type "int"; expected "str" |
| 170 |
[builtins fixtures/list.pyi] |
| 171 |
|
| 172 |
[case testUnionSimplificationGenericClass] |
| 173 |
from typing import TypeVar, Union, Generic |
| 174 |
T = TypeVar('T') |
| 175 |
U = TypeVar('U') |
| 176 |
class C(Generic[T, U]): |
| 177 |
def f(self, x: str) -> Union[T, U]: pass |
| 178 |
a = C() # type: C[int, int] |
| 179 |
b = a.f('a') |
| 180 |
a.f(b) # E: Argument 1 to "f" of "C" has incompatible type "int"; expected "str" |
| 181 |
|
| 182 |
[case testUnionOrderEquivalence] |
| 183 |
from typing import Union |
| 184 |
|
| 185 |
def foo(): pass |
| 186 |
|
| 187 |
S = str |
| 188 |
T = int |
| 189 |
|
| 190 |
if foo(): |
| 191 |
def f(x: Union[int, str]) -> None: pass |
| 192 |
elif foo(): |
| 193 |
def f(x: Union[str, int]) -> None: pass |
| 194 |
elif foo(): |
| 195 |
def f(x: Union[int, str, int, int, str]) -> None: pass |
| 196 |
elif foo(): |
| 197 |
def f(x: Union[int, str, float]) -> None: pass # E: All conditional function variants must have identical signatures \ |
| 198 |
# N: Original: \ |
| 199 |
# N: def f(x: int | str) -> None \ |
| 200 |
# N: Redefinition: \ |
| 201 |
# N: def f(x: int | str | float) -> None |
| 202 |
elif foo(): |
| 203 |
def f(x: Union[S, T]) -> None: pass |
| 204 |
elif foo(): |
| 205 |
def f(x: Union[str]) -> None: pass # E: All conditional function variants must have identical signatures \ |
| 206 |
# N: Original: \ |
| 207 |
# N: def f(x: int | str) -> None \ |
| 208 |
# N: Redefinition: \ |
| 209 |
# N: def f(x: str) -> None |
| 210 |
else: |
| 211 |
def f(x: Union[Union[int, T], Union[S, T], str]) -> None: pass |
| 212 |
|
| 213 |
# Checks bidirectionality of testing. The first definition of g is consistent with |
| 214 |
# the second, but not vice-versa. |
| 215 |
if foo(): |
| 216 |
def g(x: Union[int, str, bytes]) -> None: pass |
| 217 |
else: |
| 218 |
def g(x: Union[int, str]) -> None: pass # E: All conditional function variants must have identical signatures \ |
| 219 |
# N: Original: \ |
| 220 |
# N: def g(x: int | str | bytes) -> None \ |
| 221 |
# N: Redefinition: \ |
| 222 |
# N: def g(x: int | str) -> None |
| 223 |
|
| 224 |
[case testUnionSimplificationSpecialCases] |
| 225 |
# flags: --no-strict-optional |
| 226 |
from typing import Any, TypeVar, Union |
| 227 |
|
| 228 |
class C(Any): pass |
| 229 |
|
| 230 |
T = TypeVar('T') |
| 231 |
S = TypeVar('S') |
| 232 |
def u(x: T, y: S) -> Union[S, T]: pass |
| 233 |
|
| 234 |
a = None # type: Any |
| 235 |
|
| 236 |
reveal_type(u(C(), None)) # N: Revealed type is "__main__.C" |
| 237 |
reveal_type(u(None, C())) # N: Revealed type is "__main__.C" |
| 238 |
|
| 239 |
reveal_type(u(C(), a)) # N: Revealed type is "Any | __main__.C" |
| 240 |
reveal_type(u(a, C())) # N: Revealed type is "__main__.C | Any" |
| 241 |
|
| 242 |
reveal_type(u(C(), C())) # N: Revealed type is "__main__.C" |
| 243 |
reveal_type(u(a, a)) # N: Revealed type is "Any" |
| 244 |
|
| 245 |
[case testUnionSimplificationSpecialCase2] |
| 246 |
from typing import Any, TypeVar, Union |
| 247 |
|
| 248 |
class C(Any): pass |
| 249 |
|
| 250 |
T = TypeVar('T') |
| 251 |
S = TypeVar('S') |
| 252 |
def u(x: T, y: S) -> Union[S, T]: pass |
| 253 |
|
| 254 |
def f(x: T) -> None: |
| 255 |
reveal_type(u(C(), x)) # N: Revealed type is "T`-1 | __main__.C" |
| 256 |
reveal_type(u(x, C())) # N: Revealed type is "__main__.C | T`-1" |
| 257 |
|
| 258 |
[case testUnionSimplificationSpecialCase3] |
| 259 |
from typing import Any, TypeVar, Generic, Union |
| 260 |
|
| 261 |
class C(Any): pass |
| 262 |
|
| 263 |
V = TypeVar('V') |
| 264 |
T = TypeVar('T') |
| 265 |
|
| 266 |
class M(Generic[V]): |
| 267 |
def get(self, default: T) -> Union[V, T]: ... |
| 268 |
|
| 269 |
def f(x: M[C]) -> None: |
| 270 |
y = x.get(None) |
| 271 |
reveal_type(y) # N: Revealed type is "__main__.C | None" |
| 272 |
|
| 273 |
[case testUnionSimplificationSpecialCases2] |
| 274 |
# flags: --no-strict-optional |
| 275 |
from typing import Any, TypeVar, Union |
| 276 |
|
| 277 |
class C(Any): pass |
| 278 |
|
| 279 |
T = TypeVar('T') |
| 280 |
S = TypeVar('S') |
| 281 |
def u(x: T, y: S) -> Union[S, T]: pass |
| 282 |
|
| 283 |
a = None # type: Any |
| 284 |
|
| 285 |
# Base-class-Any and None, simplify |
| 286 |
reveal_type(u(C(), None)) # N: Revealed type is "__main__.C" |
| 287 |
reveal_type(u(None, C())) # N: Revealed type is "__main__.C" |
| 288 |
|
| 289 |
# Normal instance type and None, simplify |
| 290 |
reveal_type(u(1, None)) # N: Revealed type is "builtins.int" |
| 291 |
reveal_type(u(None, 1)) # N: Revealed type is "builtins.int" |
| 292 |
|
| 293 |
# Normal instance type and base-class-Any, no simplification |
| 294 |
reveal_type(u(C(), 1)) # N: Revealed type is "builtins.int | __main__.C" |
| 295 |
reveal_type(u(1, C())) # N: Revealed type is "__main__.C | builtins.int" |
| 296 |
|
| 297 |
# Normal instance type and Any, no simplification |
| 298 |
reveal_type(u(1, a)) # N: Revealed type is "Any | builtins.int" |
| 299 |
reveal_type(u(a, 1)) # N: Revealed type is "builtins.int | Any" |
| 300 |
|
| 301 |
# Any and base-class-Any, no simplification |
| 302 |
reveal_type(u(C(), a)) # N: Revealed type is "Any | __main__.C" |
| 303 |
reveal_type(u(a, C())) # N: Revealed type is "__main__.C | Any" |
| 304 |
|
| 305 |
# Two normal instance types, simplify |
| 306 |
reveal_type(u(1, object())) # N: Revealed type is "builtins.object" |
| 307 |
reveal_type(u(object(), 1)) # N: Revealed type is "builtins.object" |
| 308 |
|
| 309 |
# Two normal instance types, no simplification |
| 310 |
reveal_type(u(1, '')) # N: Revealed type is "builtins.str | builtins.int" |
| 311 |
reveal_type(u('', 1)) # N: Revealed type is "builtins.int | builtins.str" |
| 312 |
|
| 313 |
[case testUnionSimplificationWithDuplicateItems] |
| 314 |
from typing import Any, TypeVar, Union |
| 315 |
|
| 316 |
class C(Any): pass |
| 317 |
|
| 318 |
T = TypeVar('T') |
| 319 |
S = TypeVar('S') |
| 320 |
R = TypeVar('R') |
| 321 |
def u(x: T, y: S, z: R) -> Union[R, S, T]: pass |
| 322 |
|
| 323 |
a: Any |
| 324 |
|
| 325 |
reveal_type(u(1, 1, 1)) # N: Revealed type is "builtins.int" |
| 326 |
reveal_type(u(C(), C(), None)) # N: Revealed type is "None | __main__.C" |
| 327 |
reveal_type(u(a, a, 1)) # N: Revealed type is "builtins.int | Any" |
| 328 |
reveal_type(u(a, C(), a)) # N: Revealed type is "Any | __main__.C" |
| 329 |
reveal_type(u('', 1, 1)) # N: Revealed type is "builtins.int | builtins.str" |
| 330 |
|
| 331 |
[case testUnionAndBinaryOperation] |
| 332 |
from typing import Union |
| 333 |
class A: pass |
| 334 |
def f(x: Union[int, str, A]): |
| 335 |
x + object() # E: Unsupported left operand type for + ("A") \ |
| 336 |
# N: Left operand is of type "int | str | A" \ |
| 337 |
# E: Unsupported operand types for + ("int" and "object") \ |
| 338 |
# E: Unsupported operand types for + ("str" and "object") |
| 339 |
[builtins fixtures/primitives.pyi] |
| 340 |
|
| 341 |
[case testNarrowingDownNamedTupleUnion] |
| 342 |
from typing import NamedTuple, Union |
| 343 |
|
| 344 |
A = NamedTuple('A', [('y', int)]) |
| 345 |
B = NamedTuple('B', [('x', int)]) |
| 346 |
C = NamedTuple('C', [('x', int)]) |
| 347 |
|
| 348 |
def foo(a: Union[A, B, C]): |
| 349 |
if isinstance(a, (B, C)): |
| 350 |
reveal_type(a) # N: Revealed type is "tuple[builtins.int, fallback=__main__.B] | tuple[builtins.int, fallback=__main__.C]" |
| 351 |
a.x |
| 352 |
a.y # E: Item "B" of "B | C" has no attribute "y" \ |
| 353 |
# E: Item "C" of "B | C" has no attribute "y" |
| 354 |
b = a # type: Union[B, C] |
| 355 |
[builtins fixtures/isinstance.pyi] |
| 356 |
|
| 357 |
[case testSimplifyingUnionAndTypePromotions] |
| 358 |
from typing import TypeVar, Union |
| 359 |
T = TypeVar('T') |
| 360 |
S = TypeVar('S') |
| 361 |
def u(x: T, y: S) -> Union[T, S]: pass |
| 362 |
|
| 363 |
reveal_type(u(1, 2.3)) # N: Revealed type is "builtins.int | builtins.float" |
| 364 |
reveal_type(u(2.3, 1)) # N: Revealed type is "builtins.float | builtins.int" |
| 365 |
reveal_type(u(False, 2.2)) # N: Revealed type is "builtins.bool | builtins.float" |
| 366 |
reveal_type(u(2.2, False)) # N: Revealed type is "builtins.float | builtins.bool" |
| 367 |
[builtins fixtures/primitives.pyi] |
| 368 |
|
| 369 |
[case testSimplifyingUnionWithTypeTypes1] |
| 370 |
from typing import TypeVar, Union, Type, Any |
| 371 |
|
| 372 |
T = TypeVar('T') |
| 373 |
S = TypeVar('S') |
| 374 |
def u(x: T, y: S) -> Union[S, T]: pass |
| 375 |
|
| 376 |
t_o: Type[object] |
| 377 |
t_s: Type[str] |
| 378 |
t_a: Type[Any] |
| 379 |
|
| 380 |
# Two identical items |
| 381 |
reveal_type(u(t_o, t_o)) # N: Revealed type is "type[builtins.object]" |
| 382 |
reveal_type(u(t_s, t_s)) # N: Revealed type is "type[builtins.str]" |
| 383 |
reveal_type(u(t_a, t_a)) # N: Revealed type is "type[Any]" |
| 384 |
reveal_type(u(type, type)) # N: Revealed type is "def (x: builtins.object) -> builtins.type" |
| 385 |
|
| 386 |
# One type, other non-type |
| 387 |
reveal_type(u(t_s, 1)) # N: Revealed type is "builtins.int | type[builtins.str]" |
| 388 |
reveal_type(u(1, t_s)) # N: Revealed type is "type[builtins.str] | builtins.int" |
| 389 |
reveal_type(u(type, 1)) # N: Revealed type is "builtins.int | (def (x: builtins.object) -> builtins.type)" |
| 390 |
reveal_type(u(1, type)) # N: Revealed type is "(def (x: builtins.object) -> builtins.type) | builtins.int" |
| 391 |
reveal_type(u(t_a, 1)) # N: Revealed type is "builtins.int | type[Any]" |
| 392 |
reveal_type(u(1, t_a)) # N: Revealed type is "type[Any] | builtins.int" |
| 393 |
reveal_type(u(t_o, 1)) # N: Revealed type is "builtins.int | type[builtins.object]" |
| 394 |
reveal_type(u(1, t_o)) # N: Revealed type is "type[builtins.object] | builtins.int" |
| 395 |
|
| 396 |
[case testSimplifyingUnionWithTypeTypes2] |
| 397 |
from typing import TypeVar, Union, Type, Any |
| 398 |
|
| 399 |
T = TypeVar('T') |
| 400 |
S = TypeVar('S') |
| 401 |
def u(x: T, y: S) -> Union[S, T]: pass |
| 402 |
|
| 403 |
t_o: Type[object] |
| 404 |
t_s: Type[str] |
| 405 |
t_a: Type[Any] |
| 406 |
t: type |
| 407 |
|
| 408 |
# Union with object |
| 409 |
reveal_type(u(t_o, object())) # N: Revealed type is "builtins.object" |
| 410 |
reveal_type(u(object(), t_o)) # N: Revealed type is "builtins.object" |
| 411 |
reveal_type(u(t_s, object())) # N: Revealed type is "builtins.object" |
| 412 |
reveal_type(u(object(), t_s)) # N: Revealed type is "builtins.object" |
| 413 |
reveal_type(u(t_a, object())) # N: Revealed type is "builtins.object" |
| 414 |
reveal_type(u(object(), t_a)) # N: Revealed type is "builtins.object" |
| 415 |
|
| 416 |
# Union between type objects |
| 417 |
reveal_type(u(t_o, t_a)) # N: Revealed type is "type[Any] | type[builtins.object]" |
| 418 |
reveal_type(u(t_a, t_o)) # N: Revealed type is "type[builtins.object] | type[Any]" |
| 419 |
reveal_type(u(t_s, t_o)) # N: Revealed type is "type[builtins.object]" |
| 420 |
reveal_type(u(t_o, t_s)) # N: Revealed type is "type[builtins.object]" |
| 421 |
reveal_type(u(t_o, type)) # N: Revealed type is "type[builtins.object]" |
| 422 |
reveal_type(u(type, t_o)) # N: Revealed type is "type[builtins.object]" |
| 423 |
reveal_type(u(t_a, t)) # N: Revealed type is "builtins.type" |
| 424 |
reveal_type(u(t, t_a)) # N: Revealed type is "builtins.type" |
| 425 |
# The following should arguably not be simplified, but it's unclear how to fix then |
| 426 |
# without causing regressions elsewhere. |
| 427 |
reveal_type(u(t_o, t)) # N: Revealed type is "builtins.type" |
| 428 |
reveal_type(u(t, t_o)) # N: Revealed type is "builtins.type" |
| 429 |
|
| 430 |
[case testNotSimplifyingUnionWithMetaclass] |
| 431 |
from typing import TypeVar, Union, Type, Any |
| 432 |
|
| 433 |
class M(type): pass |
| 434 |
class M2(M): pass |
| 435 |
class A(metaclass=M): pass |
| 436 |
|
| 437 |
T = TypeVar('T') |
| 438 |
S = TypeVar('S') |
| 439 |
def u(x: T, y: S) -> Union[S, T]: pass |
| 440 |
|
| 441 |
a: Any |
| 442 |
t_a: Type[A] |
| 443 |
|
| 444 |
reveal_type(u(M(*a), t_a)) # N: Revealed type is "__main__.M" |
| 445 |
reveal_type(u(t_a, M(*a))) # N: Revealed type is "__main__.M" |
| 446 |
|
| 447 |
reveal_type(u(M2(*a), t_a)) # N: Revealed type is "type[__main__.A] | __main__.M2" |
| 448 |
reveal_type(u(t_a, M2(*a))) # N: Revealed type is "__main__.M2 | type[__main__.A]" |
| 449 |
|
| 450 |
[case testSimplifyUnionWithCallable] |
| 451 |
from typing import TypeVar, Union, Any, Callable |
| 452 |
|
| 453 |
T = TypeVar('T') |
| 454 |
S = TypeVar('S') |
| 455 |
def u(x: T, y: S) -> Union[S, T]: pass |
| 456 |
|
| 457 |
class C: pass |
| 458 |
class D(C): pass |
| 459 |
|
| 460 |
D_C: Callable[[D], C] |
| 461 |
A_C: Callable[[Any], C] |
| 462 |
D_A: Callable[[D], Any] |
| 463 |
C_C: Callable[[C], C] |
| 464 |
D_D: Callable[[D], D] |
| 465 |
i_C: Callable[[int], C] |
| 466 |
|
| 467 |
# TODO: Test argument names and kinds once we have flexible callable types. |
| 468 |
|
| 469 |
reveal_type(u(D_C, D_C)) # N: Revealed type is "def (__main__.D) -> __main__.C" |
| 470 |
|
| 471 |
reveal_type(u(A_C, D_C)) # N: Revealed type is "(def (__main__.D) -> __main__.C) | (def (Any) -> __main__.C)" |
| 472 |
reveal_type(u(D_C, A_C)) # N: Revealed type is "(def (Any) -> __main__.C) | (def (__main__.D) -> __main__.C)" |
| 473 |
|
| 474 |
reveal_type(u(D_A, D_C)) # N: Revealed type is "(def (__main__.D) -> __main__.C) | (def (__main__.D) -> Any)" |
| 475 |
reveal_type(u(D_C, D_A)) # N: Revealed type is "(def (__main__.D) -> Any) | (def (__main__.D) -> __main__.C)" |
| 476 |
|
| 477 |
reveal_type(u(D_C, C_C)) # N: Revealed type is "def (__main__.D) -> __main__.C" |
| 478 |
reveal_type(u(C_C, D_C)) # N: Revealed type is "def (__main__.D) -> __main__.C" |
| 479 |
|
| 480 |
reveal_type(u(D_C, D_D)) # N: Revealed type is "def (__main__.D) -> __main__.C" |
| 481 |
reveal_type(u(D_D, D_C)) # N: Revealed type is "def (__main__.D) -> __main__.C" |
| 482 |
|
| 483 |
reveal_type(u(D_C, i_C)) # N: Revealed type is "(def (builtins.int) -> __main__.C) | (def (__main__.D) -> __main__.C)" |
| 484 |
|
| 485 |
[case testUnionOperatorMethodSpecialCase] |
| 486 |
from typing import Union |
| 487 |
class C: |
| 488 |
def __le__(self, x: 'C') -> int: ... |
| 489 |
class D: |
| 490 |
def __le__(self, other) -> int: ... |
| 491 |
class E: |
| 492 |
def __ge__(self, other: Union[C, D]) -> int: ... |
| 493 |
|
| 494 |
[case testUnionSimplificationWithBoolIntAndFloat] |
| 495 |
from typing import List, Union |
| 496 |
l = reveal_type([]) # type: List[Union[bool, int, float]] \ |
| 497 |
# N: Revealed type is "builtins.list[builtins.int | builtins.float]" |
| 498 |
reveal_type(l) \ |
| 499 |
# N: Revealed type is "builtins.list[builtins.bool | builtins.int | builtins.float]" |
| 500 |
[builtins fixtures/list.pyi] |
| 501 |
|
| 502 |
[case testUnionSimplificationWithBoolIntAndFloat2] |
| 503 |
from typing import List, Union |
| 504 |
l = reveal_type([]) # type: List[Union[bool, int, float, str]] \ |
| 505 |
# N: Revealed type is "builtins.list[builtins.int | builtins.float | builtins.str]" |
| 506 |
reveal_type(l) \ |
| 507 |
# N: Revealed type is "builtins.list[builtins.bool | builtins.int | builtins.float | builtins.str]" |
| 508 |
[builtins fixtures/list.pyi] |
| 509 |
|
| 510 |
[case testNestedUnionsProcessedCorrectly] |
| 511 |
from typing import Union |
| 512 |
|
| 513 |
class A: pass |
| 514 |
class B: pass |
| 515 |
class C: pass |
| 516 |
|
| 517 |
def foo(bar: Union[Union[A, B], C]) -> None: |
| 518 |
if isinstance(bar, A): |
| 519 |
reveal_type(bar) # N: Revealed type is "__main__.A" |
| 520 |
else: |
| 521 |
reveal_type(bar) # N: Revealed type is "__main__.B | __main__.C" |
| 522 |
[builtins fixtures/isinstance.pyi] |
| 523 |
[out] |
| 524 |
|
| 525 |
[case testAssignAnyToUnion] |
| 526 |
from typing import Union, Any |
| 527 |
x: Union[int, str] |
| 528 |
a: Any |
| 529 |
if bool(): |
| 530 |
x = a |
| 531 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 532 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 533 |
[builtins fixtures/bool.pyi] |
| 534 |
|
| 535 |
[case testAssignAnyToUnionWithAny] |
| 536 |
from typing import Union, Any |
| 537 |
x: Union[int, Any] |
| 538 |
a: Any |
| 539 |
if bool(): |
| 540 |
x = a |
| 541 |
reveal_type(x) # N: Revealed type is "Any" |
| 542 |
reveal_type(x) # N: Revealed type is "builtins.int | Any" |
| 543 |
[builtins fixtures/bool.pyi] |
| 544 |
|
| 545 |
[case testUnionMultiassignSingle] |
| 546 |
from typing import Union, Tuple, Any |
| 547 |
|
| 548 |
a: Union[Tuple[int], Tuple[float]] |
| 549 |
(a1,) = a |
| 550 |
reveal_type(a1) # N: Revealed type is "builtins.int | builtins.float" |
| 551 |
|
| 552 |
b: Union[Tuple[int], Tuple[str]] |
| 553 |
(b1,) = b |
| 554 |
reveal_type(b1) # N: Revealed type is "builtins.int | builtins.str" |
| 555 |
[builtins fixtures/tuple.pyi] |
| 556 |
|
| 557 |
[case testUnionMultiassignDouble] |
| 558 |
from typing import Union, Tuple |
| 559 |
|
| 560 |
c: Union[Tuple[int, int], Tuple[int, float]] |
| 561 |
(c1, c2) = c |
| 562 |
reveal_type(c1) # N: Revealed type is "builtins.int" |
| 563 |
reveal_type(c2) # N: Revealed type is "builtins.int | builtins.float" |
| 564 |
[builtins fixtures/tuple.pyi] |
| 565 |
|
| 566 |
[case testUnionMultiassignGeneric] |
| 567 |
from typing import Union, Tuple, TypeVar |
| 568 |
T = TypeVar('T') |
| 569 |
S = TypeVar('S') |
| 570 |
|
| 571 |
def pack_two(x: T, y: S) -> Union[Tuple[T, T], Tuple[S, S]]: |
| 572 |
pass |
| 573 |
|
| 574 |
(x, y) = pack_two(1, 'a') |
| 575 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 576 |
reveal_type(y) # N: Revealed type is "builtins.int | builtins.str" |
| 577 |
[builtins fixtures/tuple.pyi] |
| 578 |
|
| 579 |
[case testUnionMultiassignAny] |
| 580 |
from typing import Union, Tuple, Any |
| 581 |
|
| 582 |
d: Union[Any, Tuple[float, float]] |
| 583 |
(d1, d2) = d |
| 584 |
reveal_type(d1) # N: Revealed type is "Any | builtins.float" |
| 585 |
reveal_type(d2) # N: Revealed type is "Any | builtins.float" |
| 586 |
|
| 587 |
e: Union[Any, Tuple[float, float], int] |
| 588 |
(e1, e2) = e # E: "int" object is not iterable |
| 589 |
[builtins fixtures/tuple.pyi] |
| 590 |
|
| 591 |
[case testUnionMultiassignNotJoin] |
| 592 |
from typing import Union, List |
| 593 |
|
| 594 |
class A: pass |
| 595 |
class B(A): pass |
| 596 |
class C(A): pass |
| 597 |
a: Union[List[B], List[C]] |
| 598 |
x, y = a |
| 599 |
reveal_type(x) # N: Revealed type is "__main__.B | __main__.C" |
| 600 |
[builtins fixtures/list.pyi] |
| 601 |
|
| 602 |
[case testUnionMultiassignRebind] |
| 603 |
from typing import Union, List |
| 604 |
|
| 605 |
class A: pass |
| 606 |
class B(A): pass |
| 607 |
class C(A): pass |
| 608 |
obj: object |
| 609 |
a: Union[List[B], List[C]] |
| 610 |
obj, new = a |
| 611 |
reveal_type(obj) # N: Revealed type is "__main__.B | __main__.C" |
| 612 |
reveal_type(new) # N: Revealed type is "__main__.B | __main__.C" |
| 613 |
|
| 614 |
obj = 1 |
| 615 |
reveal_type(obj) # N: Revealed type is "builtins.int" |
| 616 |
[builtins fixtures/list.pyi] |
| 617 |
|
| 618 |
[case testUnionMultiassignAlreadyDeclared] |
| 619 |
from typing import Union, Tuple |
| 620 |
|
| 621 |
a: Union[Tuple[int, int], Tuple[int, float]] |
| 622 |
a1: object |
| 623 |
a2: int |
| 624 |
(a1, a2) = a # E: Incompatible types in assignment (expression has type "float", variable has type "int") |
| 625 |
|
| 626 |
b: Union[Tuple[float, int], Tuple[int, int]] |
| 627 |
b1: object |
| 628 |
b2: int |
| 629 |
(b1, b2) = b |
| 630 |
reveal_type(b1) # N: Revealed type is "builtins.float | builtins.int" |
| 631 |
reveal_type(b2) # N: Revealed type is "builtins.int" |
| 632 |
|
| 633 |
c: Union[Tuple[int, int], Tuple[int, int]] |
| 634 |
c1: object |
| 635 |
c2: int |
| 636 |
(c1, c2) = c |
| 637 |
reveal_type(c1) # N: Revealed type is "builtins.int" |
| 638 |
reveal_type(c2) # N: Revealed type is "builtins.int" |
| 639 |
|
| 640 |
d: Union[Tuple[int, int], Tuple[int, float]] |
| 641 |
d1: object |
| 642 |
(d1, d2) = d |
| 643 |
reveal_type(d1) # N: Revealed type is "builtins.int" |
| 644 |
reveal_type(d2) # N: Revealed type is "builtins.int | builtins.float" |
| 645 |
[builtins fixtures/tuple.pyi] |
| 646 |
|
| 647 |
[case testUnionMultiassignIndexed] |
| 648 |
from typing import Union, Tuple, List |
| 649 |
|
| 650 |
class B: |
| 651 |
x: object |
| 652 |
|
| 653 |
x: List[int] |
| 654 |
b: B |
| 655 |
|
| 656 |
a: Union[Tuple[int, int], Tuple[int, object]] |
| 657 |
(x[0], b.x) = a |
| 658 |
reveal_type(x[0]) # N: Revealed type is "builtins.int" |
| 659 |
reveal_type(b.x) # N: Revealed type is "builtins.object" |
| 660 |
[builtins fixtures/list.pyi] |
| 661 |
|
| 662 |
[case testUnionMultiassignIndexedWithError] |
| 663 |
from typing import Union, Tuple, List |
| 664 |
|
| 665 |
class A: pass |
| 666 |
class B: |
| 667 |
x: int |
| 668 |
|
| 669 |
x: List[A] |
| 670 |
b: B |
| 671 |
|
| 672 |
a: Union[Tuple[int, int], Tuple[int, object]] |
| 673 |
(x[0], b.x) = a # E: Incompatible types in assignment (expression has type "int", target has type "A") \ |
| 674 |
# E: Incompatible types in assignment (expression has type "object", variable has type "int") |
| 675 |
reveal_type(x[0]) # N: Revealed type is "__main__.A" |
| 676 |
reveal_type(b.x) # N: Revealed type is "builtins.int" |
| 677 |
[builtins fixtures/list.pyi] |
| 678 |
|
| 679 |
[case testUnionMultiassignPacked] |
| 680 |
from typing import Union, Tuple, List |
| 681 |
|
| 682 |
a: Union[Tuple[int, int, int], Tuple[int, int, str]] |
| 683 |
a1: int |
| 684 |
a2: object |
| 685 |
(a1, *xs, a2) = a |
| 686 |
|
| 687 |
reveal_type(a1) # N: Revealed type is "builtins.int" |
| 688 |
reveal_type(xs) # N: Revealed type is "builtins.list[builtins.int]" |
| 689 |
reveal_type(a2) # N: Revealed type is "builtins.int | builtins.str" |
| 690 |
[builtins fixtures/list.pyi] |
| 691 |
|
| 692 |
[case testUnpackingUnionOfListsInFunction] |
| 693 |
from typing import Union, List |
| 694 |
|
| 695 |
def f(x: bool) -> Union[List[int], List[str]]: |
| 696 |
if x: |
| 697 |
return [1, 1] |
| 698 |
else: |
| 699 |
return ['a', 'a'] |
| 700 |
|
| 701 |
def g(x: bool) -> None: |
| 702 |
a, b = f(x) |
| 703 |
reveal_type(a) # N: Revealed type is "builtins.int | builtins.str" |
| 704 |
reveal_type(b) # N: Revealed type is "builtins.int | builtins.str" |
| 705 |
[builtins fixtures/list.pyi] |
| 706 |
|
| 707 |
[case testUnionOfVariableLengthTupleUnpacking] |
| 708 |
from typing import Tuple, Union |
| 709 |
VarTuple = Union[Tuple[int, int], Tuple[int, int, int]] |
| 710 |
|
| 711 |
def make_tuple() -> VarTuple: |
| 712 |
pass |
| 713 |
x = make_tuple() |
| 714 |
|
| 715 |
a, b = x # E: Too many values to unpack (2 expected, 3 provided) |
| 716 |
a, b, c = x # E: Need more than 2 values to unpack (3 expected) |
| 717 |
c, *d = x |
| 718 |
reveal_type(c) # N: Revealed type is "builtins.int" |
| 719 |
reveal_type(d) # N: Revealed type is "builtins.list[builtins.int]" |
| 720 |
[builtins fixtures/tuple.pyi] |
| 721 |
|
| 722 |
[case testUnionOfNonIterableUnpacking] |
| 723 |
from typing import Union |
| 724 |
bad: Union[int, str] |
| 725 |
|
| 726 |
x, y = bad # E: "int" object is not iterable \ |
| 727 |
# E: Unpacking a string is disallowed |
| 728 |
reveal_type(x) # N: Revealed type is "Any | builtins.str" |
| 729 |
reveal_type(y) # N: Revealed type is "Any | builtins.str" |
| 730 |
[builtins fixtures/primitives.pyi] |
| 731 |
|
| 732 |
|
| 733 |
[case testUnionAlwaysTooMany] |
| 734 |
from typing import Union, Tuple |
| 735 |
bad: Union[Tuple[int, int, int], Tuple[str, str, str]] |
| 736 |
|
| 737 |
x, y = bad # E: Too many values to unpack (2 expected, 3 provided) |
| 738 |
reveal_type(x) # N: Revealed type is "Any" |
| 739 |
reveal_type(y) # N: Revealed type is "Any" |
| 740 |
[builtins fixtures/tuple.pyi] |
| 741 |
[out] |
| 742 |
|
| 743 |
[case testUnionAlwaysTooFew] |
| 744 |
from typing import Union, Tuple |
| 745 |
bad: Union[Tuple[int, int, int], Tuple[str, str, str]] |
| 746 |
|
| 747 |
x, y, z, w = bad # E: Need more than 3 values to unpack (4 expected) |
| 748 |
reveal_type(x) # N: Revealed type is "Any" |
| 749 |
reveal_type(y) # N: Revealed type is "Any" |
| 750 |
reveal_type(z) # N: Revealed type is "Any" |
| 751 |
reveal_type(w) # N: Revealed type is "Any" |
| 752 |
[builtins fixtures/tuple.pyi] |
| 753 |
[out] |
| 754 |
|
| 755 |
[case testUnionUnpackingChainedTuple] |
| 756 |
from typing import Union, Tuple |
| 757 |
good: Union[Tuple[int, int], Tuple[str, str]] |
| 758 |
|
| 759 |
x, y = t = good |
| 760 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 761 |
reveal_type(y) # N: Revealed type is "builtins.int | builtins.str" |
| 762 |
reveal_type(t) # N: Revealed type is "tuple[builtins.int, builtins.int] | tuple[builtins.str, builtins.str]" |
| 763 |
[builtins fixtures/tuple.pyi] |
| 764 |
[out] |
| 765 |
|
| 766 |
[case testUnionUnpackingChainedTuple2] |
| 767 |
from typing import Union, Tuple |
| 768 |
good: Union[Tuple[int, int], Tuple[str, str]] |
| 769 |
|
| 770 |
t = x, y = good |
| 771 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 772 |
reveal_type(y) # N: Revealed type is "builtins.int | builtins.str" |
| 773 |
reveal_type(t) # N: Revealed type is "tuple[builtins.int, builtins.int] | tuple[builtins.str, builtins.str]" |
| 774 |
[builtins fixtures/tuple.pyi] |
| 775 |
[out] |
| 776 |
|
| 777 |
[case testUnionUnpackingChainedTuple3] |
| 778 |
from typing import Union, Tuple |
| 779 |
good: Union[Tuple[int, int], Tuple[str, str]] |
| 780 |
|
| 781 |
x, y = a, b = good |
| 782 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 783 |
reveal_type(y) # N: Revealed type is "builtins.int | builtins.str" |
| 784 |
reveal_type(a) # N: Revealed type is "builtins.int | builtins.str" |
| 785 |
reveal_type(b) # N: Revealed type is "builtins.int | builtins.str" |
| 786 |
[builtins fixtures/tuple.pyi] |
| 787 |
[out] |
| 788 |
|
| 789 |
[case testUnionUnpackingChainedList] |
| 790 |
from typing import Union, List |
| 791 |
good: Union[List[int], List[str]] |
| 792 |
|
| 793 |
lst = x, y = good |
| 794 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 795 |
reveal_type(y) # N: Revealed type is "builtins.int | builtins.str" |
| 796 |
reveal_type(lst) # N: Revealed type is "builtins.list[builtins.int] | builtins.list[builtins.str]" |
| 797 |
[builtins fixtures/list.pyi] |
| 798 |
[out] |
| 799 |
|
| 800 |
[case testUnionUnpackingChainedList2] |
| 801 |
from typing import Union, List |
| 802 |
good: Union[List[int], List[str]] |
| 803 |
|
| 804 |
x, *y, z = lst = good |
| 805 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 806 |
reveal_type(y) # N: Revealed type is "builtins.list[builtins.int] | builtins.list[builtins.str]" |
| 807 |
reveal_type(z) # N: Revealed type is "builtins.int | builtins.str" |
| 808 |
reveal_type(lst) # N: Revealed type is "builtins.list[builtins.int] | builtins.list[builtins.str]" |
| 809 |
[builtins fixtures/list.pyi] |
| 810 |
[out] |
| 811 |
|
| 812 |
[case testUnionUnpackingInForTuple] |
| 813 |
from typing import Union, Tuple, NamedTuple |
| 814 |
class NTInt(NamedTuple): |
| 815 |
x: int |
| 816 |
y: int |
| 817 |
class NTStr(NamedTuple): |
| 818 |
x: str |
| 819 |
y: str |
| 820 |
|
| 821 |
t1: NTInt |
| 822 |
reveal_type(t1.__iter__) # N: Revealed type is "def () -> typing.Iterator[builtins.int]" |
| 823 |
nt: Union[NTInt, NTStr] |
| 824 |
reveal_type(nt.__iter__) # N: Revealed type is "(def () -> typing.Iterator[builtins.int]) | (def () -> typing.Iterator[builtins.str])" |
| 825 |
for nx in nt: |
| 826 |
reveal_type(nx) # N: Revealed type is "builtins.int | builtins.str" |
| 827 |
|
| 828 |
t: Union[Tuple[int, int], Tuple[str, str]] |
| 829 |
for x in t: |
| 830 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 831 |
[builtins fixtures/for.pyi] |
| 832 |
[out] |
| 833 |
|
| 834 |
[case testUnionUnpackingInForList] |
| 835 |
from typing import Union, List, Tuple |
| 836 |
|
| 837 |
t: Union[List[Tuple[int, int]], List[Tuple[str, str]]] |
| 838 |
for x, y in t: |
| 839 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 840 |
reveal_type(y) # N: Revealed type is "builtins.int | builtins.str" |
| 841 |
|
| 842 |
t2: List[Union[Tuple[int, int], Tuple[str, str]]] |
| 843 |
for x2, y2 in t2: |
| 844 |
reveal_type(x2) # N: Revealed type is "builtins.int | builtins.str" |
| 845 |
reveal_type(y2) # N: Revealed type is "builtins.int | builtins.str" |
| 846 |
[builtins fixtures/for.pyi] |
| 847 |
[out] |
| 848 |
|
| 849 |
[case testUnionUnpackingDoubleBinder] |
| 850 |
from typing import Union, Tuple |
| 851 |
|
| 852 |
x: object |
| 853 |
y: object |
| 854 |
class A: pass |
| 855 |
class B: pass |
| 856 |
|
| 857 |
t1: Union[Tuple[A, A], Tuple[B, B]] |
| 858 |
t2: Union[Tuple[int, int], Tuple[str, str]] |
| 859 |
|
| 860 |
x, y = t1 |
| 861 |
reveal_type(x) # N: Revealed type is "__main__.A | __main__.B" |
| 862 |
reveal_type(y) # N: Revealed type is "__main__.A | __main__.B" |
| 863 |
|
| 864 |
x, y = t2 |
| 865 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 866 |
reveal_type(y) # N: Revealed type is "builtins.int | builtins.str" |
| 867 |
|
| 868 |
x, y = object(), object() |
| 869 |
reveal_type(x) # N: Revealed type is "builtins.object" |
| 870 |
reveal_type(y) # N: Revealed type is "builtins.object" |
| 871 |
[builtins fixtures/tuple.pyi] |
| 872 |
[out] |
| 873 |
|
| 874 |
[case testUnionUnpackingFromNestedTuples] |
| 875 |
from typing import Union, Tuple |
| 876 |
|
| 877 |
t: Union[Tuple[int, Tuple[int, int]], Tuple[str, Tuple[str, str]]] |
| 878 |
x, (y, z) = t |
| 879 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 880 |
reveal_type(y) # N: Revealed type is "builtins.int | builtins.str" |
| 881 |
reveal_type(z) # N: Revealed type is "builtins.int | builtins.str" |
| 882 |
[builtins fixtures/tuple.pyi] |
| 883 |
[out] |
| 884 |
|
| 885 |
[case testNestedUnionUnpackingFromNestedTuples] |
| 886 |
from typing import Union, Tuple |
| 887 |
|
| 888 |
class A: pass |
| 889 |
class B: pass |
| 890 |
|
| 891 |
t: Union[Tuple[int, Union[Tuple[int, int], Tuple[A, A]]], Tuple[str, Union[Tuple[str, str], Tuple[B, B]]]] |
| 892 |
x, (y, z) = t |
| 893 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 894 |
reveal_type(y) # N: Revealed type is "builtins.int | __main__.A | builtins.str | __main__.B" |
| 895 |
reveal_type(z) # N: Revealed type is "builtins.int | __main__.A | builtins.str | __main__.B" |
| 896 |
[builtins fixtures/tuple.pyi] |
| 897 |
[out] |
| 898 |
|
| 899 |
[case testNestedUnionUnpackingFromNestedTuplesBinder] |
| 900 |
from typing import Union, Tuple |
| 901 |
|
| 902 |
class A: pass |
| 903 |
class B: pass |
| 904 |
|
| 905 |
x: object |
| 906 |
y: object |
| 907 |
z: object |
| 908 |
|
| 909 |
t: Union[Tuple[int, Union[Tuple[int, int], Tuple[A, A]]], Tuple[str, Union[Tuple[str, str], Tuple[B, B]]]] |
| 910 |
x, (y, z) = t |
| 911 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 912 |
reveal_type(y) # N: Revealed type is "builtins.int | __main__.A | builtins.str | __main__.B" |
| 913 |
reveal_type(z) # N: Revealed type is "builtins.int | __main__.A | builtins.str | __main__.B" |
| 914 |
[builtins fixtures/tuple.pyi] |
| 915 |
[out] |
| 916 |
|
| 917 |
[case testUnpackUnionNoCrashOnPartialNone] |
| 918 |
from typing import Dict, Tuple, List, Any |
| 919 |
|
| 920 |
a: Any |
| 921 |
d: Dict[str, Tuple[List[Tuple[str, str]], str]] |
| 922 |
x, _ = d.get(a, (None, None)) |
| 923 |
|
| 924 |
for y in x: pass # E: Item "None" of "list[tuple[str, str]] | None" has no attribute "__iter__" (not iterable) |
| 925 |
if x: |
| 926 |
for s, t in x: |
| 927 |
reveal_type(s) # N: Revealed type is "builtins.str" |
| 928 |
[builtins fixtures/dict.pyi] |
| 929 |
[out] |
| 930 |
|
| 931 |
[case testUnpackUnionNoCrashOnPartialNone2] |
| 932 |
from typing import Dict, Tuple, List, Any |
| 933 |
|
| 934 |
a: Any |
| 935 |
x = None |
| 936 |
d: Dict[str, Tuple[List[Tuple[str, str]], str]] |
| 937 |
x, _ = d.get(a, (None, None)) |
| 938 |
|
| 939 |
for y in x: pass # E: Item "None" of "list[tuple[str, str]] | None" has no attribute "__iter__" (not iterable) |
| 940 |
if x: |
| 941 |
for s, t in x: |
| 942 |
reveal_type(s) # N: Revealed type is "builtins.str" |
| 943 |
[builtins fixtures/dict.pyi] |
| 944 |
[out] |
| 945 |
|
| 946 |
[case testUnpackUnionNoCrashOnPartialNoneBinder] |
| 947 |
from typing import Dict, Tuple, List, Any |
| 948 |
|
| 949 |
x: object |
| 950 |
a: Any |
| 951 |
d: Dict[str, Tuple[List[Tuple[str, str]], str]] |
| 952 |
x, _ = d.get(a, (None, None)) |
| 953 |
reveal_type(x) # N: Revealed type is "builtins.list[tuple[builtins.str, builtins.str]] | None" |
| 954 |
|
| 955 |
if x: |
| 956 |
for y in x: pass |
| 957 |
[builtins fixtures/dict.pyi] |
| 958 |
[out] |
| 959 |
|
| 960 |
[case testUnpackUnionNoCrashOnPartialList] |
| 961 |
from typing import Dict, Tuple, List, Any |
| 962 |
|
| 963 |
a: Any |
| 964 |
d: Dict[str, Tuple[List[Tuple[str, str]], str]] |
| 965 |
x, _ = d.get(a, ([], "")) |
| 966 |
reveal_type(x) # N: Revealed type is "builtins.list[tuple[builtins.str, builtins.str]]" |
| 967 |
|
| 968 |
for y in x: pass |
| 969 |
[builtins fixtures/dict.pyi] |
| 970 |
[out] |
| 971 |
|
| 972 |
[case testLongUnionFormatting] |
| 973 |
from typing import Any, Generic, TypeVar, Union |
| 974 |
|
| 975 |
T = TypeVar('T') |
| 976 |
|
| 977 |
class ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes(Generic[T]): |
| 978 |
pass |
| 979 |
|
| 980 |
x: Union[ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes[int], |
| 981 |
ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes[object], |
| 982 |
ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes[float], |
| 983 |
ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes[str], |
| 984 |
ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes[Any], |
| 985 |
ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes[bytes]] |
| 986 |
|
| 987 |
def takes_int(arg: int) -> None: pass |
| 988 |
|
| 989 |
takes_int(x) # E: Argument 1 to "takes_int" has incompatible type "ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes[int] | ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes[object] | ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes[float] | ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes[str] | ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes[Any] | ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes[bytes]"; expected "int" |
| 990 |
|
| 991 |
[case testRecursiveForwardReferenceInUnion] |
| 992 |
from typing import List, Union |
| 993 |
|
| 994 |
def test() -> None: |
| 995 |
MYTYPE = List[Union[str, "MYTYPE"]] # E: Cannot resolve name "MYTYPE" (possible cyclic definition) \ |
| 996 |
# N: Recursive types are not allowed at function scope |
| 997 |
[builtins fixtures/list.pyi] |
| 998 |
|
| 999 |
[case testNonStrictOptional] |
| 1000 |
# flags: --no-strict-optional |
| 1001 |
from typing import Optional, List |
| 1002 |
|
| 1003 |
def union_test1(x): |
| 1004 |
# type: (Optional[List[int]]) -> Optional[int] |
| 1005 |
if x is None: |
| 1006 |
return x |
| 1007 |
else: |
| 1008 |
return x[0] |
| 1009 |
|
| 1010 |
def union_test2(x): |
| 1011 |
# type: (Optional[List[int]]) -> Optional[int] |
| 1012 |
if isinstance(x, type(None)): |
| 1013 |
return x |
| 1014 |
else: |
| 1015 |
return x[0] |
| 1016 |
|
| 1017 |
def f(): return 0 |
| 1018 |
|
| 1019 |
def union_test3(): |
| 1020 |
# type: () -> int |
| 1021 |
x = f() |
| 1022 |
assert x is None |
| 1023 |
x = f() |
| 1024 |
return x + 1 |
| 1025 |
|
| 1026 |
[builtins fixtures/isinstancelist.pyi] |
| 1027 |
|
| 1028 |
[case testInvariance] |
| 1029 |
from typing import List, Union |
| 1030 |
from enum import Enum |
| 1031 |
|
| 1032 |
class Boop(Enum): |
| 1033 |
FOO = 1 |
| 1034 |
|
| 1035 |
def do_thing_with_enums(enums: Union[List[Enum], Enum]) -> None: ... |
| 1036 |
|
| 1037 |
boop: List[Boop] = [] |
| 1038 |
do_thing_with_enums(boop) # E: Argument 1 to "do_thing_with_enums" has incompatible type "list[Boop]"; expected "list[Enum] | Enum" \ |
| 1039 |
# N: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \ |
| 1040 |
# N: Consider using "Sequence" instead, which is covariant |
| 1041 |
[builtins fixtures/isinstancelist.pyi] |
| 1042 |
|
| 1043 |
[case testAssignUnionWithTenaryExprWithEmptyCollection] |
| 1044 |
from typing import Dict, List, Union |
| 1045 |
x: Union[int, List[int]] = 1 if bool() else [] |
| 1046 |
y: Union[int, Dict[int, int]] = 1 if bool() else {} |
| 1047 |
|
| 1048 |
u: Union[int, List[int]] = [] if bool() else 1 |
| 1049 |
v: Union[int, Dict[int, int]] = {} if bool() else 1 |
| 1050 |
[builtins fixtures/isinstancelist.pyi] |
| 1051 |
|
| 1052 |
[case testFlattenTypeAliasWhenAliasedAsUnion] |
| 1053 |
from typing import Union |
| 1054 |
|
| 1055 |
T1 = int |
| 1056 |
T2 = Union[T1, float] |
| 1057 |
T3 = Union[T2, complex] |
| 1058 |
T4 = Union[T3, int] |
| 1059 |
|
| 1060 |
def foo(a: T2, b: T2) -> T2: |
| 1061 |
return a + b |
| 1062 |
|
| 1063 |
def bar(a: T4, b: T4) -> T4: # test multi-level alias |
| 1064 |
return a + b |
| 1065 |
[builtins fixtures/ops.pyi] |
| 1066 |
|
| 1067 |
[case testJoinUnionWithUnionAndAny] |
| 1068 |
from typing import TypeVar, Union, Any |
| 1069 |
T = TypeVar("T") |
| 1070 |
def f(x: T, y: T) -> T: |
| 1071 |
return x |
| 1072 |
x: Union[None, Any] |
| 1073 |
y: Union[int, None] |
| 1074 |
reveal_type(f(x, y)) # N: Revealed type is "None | Any | builtins.int" |
| 1075 |
reveal_type(f(y, x)) # N: Revealed type is "builtins.int | None | Any" |
| 1076 |
|
| 1077 |
[case testNestedProtocolUnions] |
| 1078 |
from typing import Union, Iterator, Iterable |
| 1079 |
def foo( |
| 1080 |
values: Union[ |
| 1081 |
Iterator[Union[ |
| 1082 |
Iterator[Union[Iterator[int], Iterable[int]]], |
| 1083 |
Iterable[Union[Iterator[int], Iterable[int]]], |
| 1084 |
]], |
| 1085 |
Iterable[Union[ |
| 1086 |
Iterator[Union[Iterator[int], Iterable[int]]], |
| 1087 |
Iterable[Union[Iterator[int], Iterable[int]]], |
| 1088 |
]], |
| 1089 |
] |
| 1090 |
) -> Iterator[int]: |
| 1091 |
for i in values: |
| 1092 |
for j in i: |
| 1093 |
for k in j: |
| 1094 |
yield k |
| 1095 |
foo([[[1]]]) |
| 1096 |
[builtins fixtures/list.pyi] |
| 1097 |
|
| 1098 |
[case testNestedProtocolGenericUnions] |
| 1099 |
from typing import Union, Iterator, List |
| 1100 |
def foo( |
| 1101 |
values: Union[ |
| 1102 |
Iterator[Union[ |
| 1103 |
Iterator[Union[Iterator[int], List[int]]], |
| 1104 |
List[Union[Iterator[int], List[int]]], |
| 1105 |
]], |
| 1106 |
List[Union[ |
| 1107 |
Iterator[Union[Iterator[int], List[int]]], |
| 1108 |
List[Union[Iterator[int], List[int]]], |
| 1109 |
]], |
| 1110 |
] |
| 1111 |
) -> Iterator[int]: |
| 1112 |
for i in values: |
| 1113 |
for j in i: |
| 1114 |
for k in j: |
| 1115 |
yield k |
| 1116 |
foo([[[1]]]) |
| 1117 |
[builtins fixtures/list.pyi] |
| 1118 |
|
| 1119 |
[case testNestedProtocolGenericUnionsDeep] |
| 1120 |
from typing import TypeVar, Union, Iterator, List |
| 1121 |
T = TypeVar("T") |
| 1122 |
Iter = Union[Iterator[T], List[T]] |
| 1123 |
def foo( |
| 1124 |
values: Iter[Iter[Iter[Iter[Iter[int]]]]], |
| 1125 |
) -> Iterator[int]: |
| 1126 |
for i in values: |
| 1127 |
for j in i: |
| 1128 |
for k in j: |
| 1129 |
for l in k: |
| 1130 |
for m in l: |
| 1131 |
yield m |
| 1132 |
foo([[[[[1]]]]]) |
| 1133 |
[builtins fixtures/list.pyi] |
| 1134 |
|
| 1135 |
[case testNestedInstanceUnsimplifiedUnion] |
| 1136 |
from typing import TypeVar, Union, Iterator, List, Any |
| 1137 |
T = TypeVar("T") |
| 1138 |
|
| 1139 |
Iter = Union[Iterator[T], List[T]] |
| 1140 |
def foo( |
| 1141 |
values: Iter[Union[Any, Any]], |
| 1142 |
) -> Iterator[Any]: |
| 1143 |
for i in values: |
| 1144 |
yield i |
| 1145 |
foo([1]) |
| 1146 |
[builtins fixtures/list.pyi] |
| 1147 |
|
| 1148 |
[case testNestedInstanceTypeAlias] |
| 1149 |
from typing import TypeVar, Union, Iterator, List, Any |
| 1150 |
T = TypeVar("T") |
| 1151 |
|
| 1152 |
Iter = Union[Iterator[T], List[T]] |
| 1153 |
def foo( |
| 1154 |
values: Iter["Any"], |
| 1155 |
) -> Iterator[Any]: |
| 1156 |
for i in values: |
| 1157 |
yield i |
| 1158 |
foo([1]) |
| 1159 |
[builtins fixtures/list.pyi] |
| 1160 |
|
| 1161 |
[case testGenericUnionMemberWithTypeVarConstraints] |
| 1162 |
|
| 1163 |
from typing import Generic, TypeVar, Union |
| 1164 |
|
| 1165 |
T = TypeVar('T', str, int) |
| 1166 |
|
| 1167 |
class C(Generic[T]): ... |
| 1168 |
|
| 1169 |
def f(s: Union[T, C[T]]) -> T: ... |
| 1170 |
|
| 1171 |
ci: C[int] |
| 1172 |
cs: C[str] |
| 1173 |
|
| 1174 |
reveal_type(f(1)) # N: Revealed type is "builtins.int" |
| 1175 |
reveal_type(f('')) # N: Revealed type is "builtins.str" |
| 1176 |
reveal_type(f(ci)) # N: Revealed type is "builtins.int" |
| 1177 |
reveal_type(f(cs)) # N: Revealed type is "builtins.str" |
| 1178 |
|
| 1179 |
|
| 1180 |
[case testNestedInstanceTypeAliasUnsimplifiedUnion] |
| 1181 |
from typing import TypeVar, Union, Iterator, List, Any |
| 1182 |
T = TypeVar("T") |
| 1183 |
|
| 1184 |
Iter = Union[Iterator[T], List[T]] |
| 1185 |
def foo( |
| 1186 |
values: Iter["Union[Any, Any]"], |
| 1187 |
) -> Iterator[Any]: |
| 1188 |
for i in values: |
| 1189 |
yield i |
| 1190 |
foo([1]) |
| 1191 |
[builtins fixtures/list.pyi] |
| 1192 |
|
| 1193 |
[case testUnionIterableContainer] |
| 1194 |
from typing import Iterable, Container, Union |
| 1195 |
|
| 1196 |
i: Iterable[str] |
| 1197 |
c: Container[str] |
| 1198 |
u: Union[Iterable[str], Container[str]] |
| 1199 |
ni: Union[Iterable[str], int] |
| 1200 |
nc: Union[Container[str], int] |
| 1201 |
|
| 1202 |
'x' in i |
| 1203 |
'x' in c |
| 1204 |
'x' in u |
| 1205 |
'x' in ni # E: Unsupported right operand type for in ("Iterable[str] | int") |
| 1206 |
'x' in nc # E: Unsupported right operand type for in ("Container[str] | int") |
| 1207 |
[builtins fixtures/tuple.pyi] |
| 1208 |
[typing fixtures/typing-full.pyi] |
| 1209 |
|
| 1210 |
[case testDescriptorAccessForUnionOfTypes] |
| 1211 |
from typing import overload, Generic, Any, TypeVar, List, Optional, Union, Type |
| 1212 |
|
| 1213 |
_T_co = TypeVar("_T_co", bound=Any, covariant=True) |
| 1214 |
|
| 1215 |
class Mapped(Generic[_T_co]): |
| 1216 |
def __init__(self, value: _T_co): |
| 1217 |
self.value = value |
| 1218 |
|
| 1219 |
@overload |
| 1220 |
def __get__( |
| 1221 |
self, instance: None, owner: Any |
| 1222 |
) -> List[_T_co]: |
| 1223 |
... |
| 1224 |
|
| 1225 |
@overload |
| 1226 |
def __get__(self, instance: object, owner: Any) -> _T_co: |
| 1227 |
... |
| 1228 |
|
| 1229 |
def __get__( |
| 1230 |
self, instance: Optional[object], owner: Any |
| 1231 |
) -> Union[List[_T_co], _T_co]: |
| 1232 |
return self.value |
| 1233 |
|
| 1234 |
class A: |
| 1235 |
field_1: Mapped[int] = Mapped(1) |
| 1236 |
field_2: Mapped[str] = Mapped('1') |
| 1237 |
|
| 1238 |
class B: |
| 1239 |
field_1: Mapped[int] = Mapped(2) |
| 1240 |
field_2: Mapped[str] = Mapped('2') |
| 1241 |
|
| 1242 |
mix: Union[Type[A], Type[B]] = A |
| 1243 |
reveal_type(mix) # N: Revealed type is "type[__main__.A] | type[__main__.B]" |
| 1244 |
reveal_type(mix.field_1) # N: Revealed type is "builtins.list[builtins.int]" |
| 1245 |
reveal_type(mix().field_1) # N: Revealed type is "builtins.int" |
| 1246 |
[builtins fixtures/list.pyi] |
| 1247 |
|
| 1248 |
|
| 1249 |
[case testDescriptorAccessForUnionOfTypesWithNoStrictOptional] |
| 1250 |
# mypy: no-strict-optional |
| 1251 |
from typing import overload, Generic, Any, TypeVar, List, Optional, Union, Type |
| 1252 |
|
| 1253 |
class Descriptor: |
| 1254 |
@overload |
| 1255 |
def __get__( |
| 1256 |
self, instance: None, owner: type |
| 1257 |
) -> str: |
| 1258 |
... |
| 1259 |
|
| 1260 |
@overload |
| 1261 |
def __get__(self, instance: object, owner: type) -> int: |
| 1262 |
... |
| 1263 |
|
| 1264 |
def __get__( |
| 1265 |
self, instance: Optional[object], owner: type |
| 1266 |
) -> Union[str, int]: |
| 1267 |
... |
| 1268 |
|
| 1269 |
class A: |
| 1270 |
field = Descriptor() |
| 1271 |
|
| 1272 |
a_class_or_none: Optional[Type[A]] |
| 1273 |
x: str = a_class_or_none.field |
| 1274 |
|
| 1275 |
a_or_none: Optional[A] |
| 1276 |
y: int = a_or_none.field |
| 1277 |
[builtins fixtures/list.pyi] |
| 1278 |
|
| 1279 |
[case testLargeUnionsShort] |
| 1280 |
from typing import Union |
| 1281 |
|
| 1282 |
class C1: ... |
| 1283 |
class C2: ... |
| 1284 |
class C3: ... |
| 1285 |
class C4: ... |
| 1286 |
class C5: ... |
| 1287 |
class C6: ... |
| 1288 |
class C7: ... |
| 1289 |
class C8: ... |
| 1290 |
class C9: ... |
| 1291 |
class C10: ... |
| 1292 |
class C11: ... |
| 1293 |
|
| 1294 |
u: Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11] |
| 1295 |
x: int = u # E: Incompatible types in assignment (expression has type "C1 | C2 | C3 | C4 | C5 | <6 more items>", variable has type "int") |
| 1296 |
|
| 1297 |
[case testLargeUnionsLongIfNeeded] |
| 1298 |
from typing import Union |
| 1299 |
|
| 1300 |
class C1: ... |
| 1301 |
class C2: ... |
| 1302 |
class C3: ... |
| 1303 |
class C4: ... |
| 1304 |
class C5: ... |
| 1305 |
class C6: ... |
| 1306 |
class C7: ... |
| 1307 |
class C8: ... |
| 1308 |
class C9: ... |
| 1309 |
class C10: ... |
| 1310 |
|
| 1311 |
x: Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, int] |
| 1312 |
y: Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, str] |
| 1313 |
x = y # E: Incompatible types in assignment (expression has type "C1 | C2 | C3 | C4 | C5 | C6 | C7 | C8 | C9 | C10 | str", variable has type "C1 | C2 | C3 | C4 | C5 | C6 | C7 | C8 | C9 | C10 | int") \ |
| 1314 |
# N: Item in the first union not in the second: "str" |
| 1315 |
|
| 1316 |
[case testLargeUnionsNoneShown] |
| 1317 |
from typing import Union |
| 1318 |
|
| 1319 |
class C1: ... |
| 1320 |
class C2: ... |
| 1321 |
class C3: ... |
| 1322 |
class C4: ... |
| 1323 |
class C5: ... |
| 1324 |
class C6: ... |
| 1325 |
class C7: ... |
| 1326 |
class C8: ... |
| 1327 |
class C9: ... |
| 1328 |
class C10: ... |
| 1329 |
class C11: ... |
| 1330 |
|
| 1331 |
x: Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11] |
| 1332 |
y: Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11, None] |
| 1333 |
x = y # E: Incompatible types in assignment (expression has type "C1 | C2 | C3 | C4 | C5 | <6 more items> | None", variable has type "C1 | C2 | C3 | C4 | C5 | <6 more items>") \ |
| 1334 |
# N: Item in the first union not in the second: "None" |
| 1335 |
|
| 1336 |
[case testTypeAliasWithOldUnionIsInstance] |
| 1337 |
from typing import Union |
| 1338 |
SimpleAlias = Union[int, str] |
| 1339 |
|
| 1340 |
def foo(x: Union[int, str, tuple]): |
| 1341 |
# TODO: fix the typeshed stub for isinstance |
| 1342 |
if isinstance(x, SimpleAlias): # E: Argument 2 to "isinstance" has incompatible type "<typing special form>"; expected "type" |
| 1343 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | builtins.tuple[Any, ...]" |
| 1344 |
else: |
| 1345 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | builtins.tuple[Any, ...]" |
| 1346 |
[builtins fixtures/tuple.pyi] |