| 1 |
[case testForcedAssignment] |
| 2 |
# flags: --warn-unreachable |
| 3 |
x = 1 # type: object |
| 4 |
y = 1 |
| 5 |
def f(): x, y # Prevent independent redefinition |
| 6 |
y = x # E: Incompatible types in assignment (expression has type "object", variable has type "int") |
| 7 |
x = 2 |
| 8 |
y = x |
| 9 |
[builtins fixtures/tuple.pyi] |
| 10 |
|
| 11 |
[case testJoinAny] |
| 12 |
# flags: --warn-unreachable |
| 13 |
from typing import List, Any |
| 14 |
|
| 15 |
x: List[Any] |
| 16 |
|
| 17 |
def foo() -> List[int]: pass |
| 18 |
def bar() -> List[str]: pass |
| 19 |
|
| 20 |
if bool(): |
| 21 |
x = foo() |
| 22 |
else: |
| 23 |
x = bar() |
| 24 |
|
| 25 |
x * 2 |
| 26 |
[builtins fixtures/list.pyi] |
| 27 |
|
| 28 |
[case testGeneratorExpressionTypes] |
| 29 |
# flags: --warn-unreachable |
| 30 |
class A: y = 1 |
| 31 |
x = [A()] |
| 32 |
y = [x] |
| 33 |
|
| 34 |
z = [1,2] |
| 35 |
z = [a.y for b in y for a in b] |
| 36 |
[builtins fixtures/list.pyi] |
| 37 |
|
| 38 |
[case testIsinstanceNestedTuple] |
| 39 |
# flags: --warn-unreachable |
| 40 |
from typing import Union, List, Tuple, Dict |
| 41 |
|
| 42 |
def f(x: Union[int, str, List]) -> None: |
| 43 |
if isinstance(x, (str, (int,))): |
| 44 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 45 |
x[1] # E: Value of type "int | str" is not indexable |
| 46 |
else: |
| 47 |
reveal_type(x) # N: Revealed type is "builtins.list[Any]" |
| 48 |
x[1] |
| 49 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | builtins.list[Any]" |
| 50 |
if isinstance(x, (str, (list,))): |
| 51 |
reveal_type(x) # N: Revealed type is "builtins.str | builtins.list[Any]" |
| 52 |
x[1] |
| 53 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | builtins.list[Any]" |
| 54 |
[builtins fixtures/isinstancelist.pyi] |
| 55 |
|
| 56 |
[case testIsinstanceNarrowsSequenceParentFromElement] |
| 57 |
# flags: --warn-unreachable |
| 58 |
from __future__ import annotations |
| 59 |
|
| 60 |
def f1(x: list[str] | list[int]) -> None: |
| 61 |
if isinstance(x[0], str): |
| 62 |
reveal_type(x) # N: Revealed type is "builtins.list[builtins.str]" |
| 63 |
reveal_type(x[0]) # N: Revealed type is "builtins.str" |
| 64 |
else: |
| 65 |
reveal_type(x) # N: Revealed type is "builtins.list[builtins.int]" |
| 66 |
reveal_type(x[0]) # N: Revealed type is "builtins.int" |
| 67 |
|
| 68 |
def f2(x: tuple[str, ...] | tuple[int, ...]) -> None: |
| 69 |
if isinstance(x[0], str): |
| 70 |
reveal_type(x) # N: Revealed type is "builtins.tuple[builtins.str, ...]" |
| 71 |
reveal_type(x[0]) # N: Revealed type is "builtins.str" |
| 72 |
else: |
| 73 |
reveal_type(x) # N: Revealed type is "builtins.tuple[builtins.int, ...]" |
| 74 |
reveal_type(x[0]) # N: Revealed type is "builtins.int" |
| 75 |
|
| 76 |
def f3(x: tuple[str, str] | tuple[int, int]) -> None: |
| 77 |
if isinstance(x[0], str): |
| 78 |
reveal_type(x) # N: Revealed type is "tuple[builtins.str, builtins.str]" |
| 79 |
else: |
| 80 |
reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int]" |
| 81 |
|
| 82 |
class A: ... |
| 83 |
class B: ... |
| 84 |
class C(A, B): ... |
| 85 |
|
| 86 |
def f4(x: list[A] | list[B]): |
| 87 |
# Technically this is unsound in the presence of multiple inheritance, |
| 88 |
# but we already do this kind of narrowing for tuples |
| 89 |
if isinstance(x[0], A): |
| 90 |
reveal_type(x) # N: Revealed type is "builtins.list[__main__.A]" |
| 91 |
else: |
| 92 |
reveal_type(x) # N: Revealed type is "builtins.list[__main__.B]" |
| 93 |
|
| 94 |
def f5(x: tuple[A, ...] | tuple[B, ...]): |
| 95 |
if isinstance(x[0], A): |
| 96 |
reveal_type(x) # N: Revealed type is "builtins.tuple[__main__.A, ...]" |
| 97 |
else: |
| 98 |
reveal_type(x) # N: Revealed type is "builtins.tuple[__main__.B, ...]" |
| 99 |
|
| 100 |
def f6(x: tuple[A, A] | tuple[B, B]): |
| 101 |
if isinstance(x[0], A): |
| 102 |
reveal_type(x) # N: Revealed type is "tuple[__main__.A, __main__.A]" |
| 103 |
else: |
| 104 |
reveal_type(x) # N: Revealed type is "tuple[__main__.B, __main__.B]" |
| 105 |
|
| 106 |
class AA(A): ... |
| 107 |
|
| 108 |
def f7(x: list[A] | list[AA]): |
| 109 |
if isinstance(x[0], AA): |
| 110 |
reveal_type(x) # N: Revealed type is "builtins.list[__main__.A] | builtins.list[__main__.AA]" |
| 111 |
else: |
| 112 |
reveal_type(x) # N: Revealed type is "builtins.list[__main__.A] | builtins.list[__main__.AA]" |
| 113 |
|
| 114 |
def f8(x: list[str] | list[str | None]): |
| 115 |
if x[0] is not None: |
| 116 |
reveal_type(x) # N: Revealed type is "builtins.list[builtins.str] | builtins.list[builtins.str | None]" |
| 117 |
else: |
| 118 |
reveal_type(x) # N: Revealed type is "builtins.list[builtins.str | None]" |
| 119 |
[builtins fixtures/tuple.pyi] |
| 120 |
|
| 121 |
[case testClassAttributeInitialization] |
| 122 |
# flags: --warn-unreachable |
| 123 |
class A: |
| 124 |
x: int |
| 125 |
def __init__(self) -> None: |
| 126 |
self.y: int |
| 127 |
z = self.x |
| 128 |
w = self.y |
| 129 |
|
| 130 |
[case testAssignmentSubtypes] |
| 131 |
# flags: --warn-unreachable |
| 132 |
from typing import Union |
| 133 |
|
| 134 |
def foo(x: Union[str, int]): |
| 135 |
if isinstance(x, int): |
| 136 |
x = 'a' |
| 137 |
x + 'a' |
| 138 |
z = x |
| 139 |
y = [x] |
| 140 |
y[0] + 'a' |
| 141 |
# TODO: should we allow these two lines? |
| 142 |
y + [1] # E: List item 0 has incompatible type "int"; expected "str" |
| 143 |
z = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 144 |
|
| 145 |
x: int |
| 146 |
y = [x] |
| 147 |
[builtins fixtures/isinstancelist.pyi] |
| 148 |
|
| 149 |
[case testFunctionDefaultArgs] |
| 150 |
# flags: --warn-unreachable |
| 151 |
class A: pass |
| 152 |
class B(A): y = 1 |
| 153 |
|
| 154 |
x = A() |
| 155 |
|
| 156 |
def foo(x: A = B()): |
| 157 |
x.y # E: "A" has no attribute "y" |
| 158 |
[builtins fixtures/isinstance.pyi] |
| 159 |
|
| 160 |
[case testIsinstanceFancyConditionals] |
| 161 |
# flags: --warn-unreachable |
| 162 |
class A: pass |
| 163 |
|
| 164 |
class B(A): |
| 165 |
y = 1 |
| 166 |
|
| 167 |
x = A() |
| 168 |
if isinstance(x, B): |
| 169 |
x.y |
| 170 |
while isinstance(x, B): |
| 171 |
x.y |
| 172 |
while isinstance(x, B): |
| 173 |
x.y |
| 174 |
x = B() |
| 175 |
[builtins fixtures/isinstance.pyi] |
| 176 |
|
| 177 |
[case testSubtypingWithAny] |
| 178 |
# flags: --warn-unreachable |
| 179 |
class A: |
| 180 |
y = 1 |
| 181 |
|
| 182 |
class B(A): |
| 183 |
z = 1 |
| 184 |
|
| 185 |
def foo(): pass |
| 186 |
|
| 187 |
x = A() |
| 188 |
if int(): |
| 189 |
x = B() |
| 190 |
x.z |
| 191 |
x = foo() |
| 192 |
reveal_type(x) # N: Revealed type is "Any" |
| 193 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 194 |
|
| 195 |
[case testSingleMultiAssignment] |
| 196 |
# flags: --warn-unreachable |
| 197 |
x = 'a' |
| 198 |
(x,) = ('a',) |
| 199 |
|
| 200 |
[case testUnionMultiAssignment] |
| 201 |
# flags: --warn-unreachable |
| 202 |
from typing import Union |
| 203 |
x: Union[int, str] |
| 204 |
if int(): |
| 205 |
x = 1 |
| 206 |
x = 'a' |
| 207 |
x + 1 # E: Unsupported operand types for + ("str" and "int") |
| 208 |
x = 1 |
| 209 |
(x, y) = ('a', 1) |
| 210 |
x + 1 # E: Unsupported operand types for + ("str" and "int") |
| 211 |
[builtins fixtures/isinstancelist.pyi] |
| 212 |
|
| 213 |
[case testUnionIfZigzag] |
| 214 |
# flags: --warn-unreachable |
| 215 |
from typing import Union |
| 216 |
|
| 217 |
def f(x: Union[int, str]) -> None: |
| 218 |
if 1: # Without this, the assignment below could create a new variable "x" of type "int" |
| 219 |
x = 1 |
| 220 |
if x: |
| 221 |
x = 'a' |
| 222 |
x = 1 |
| 223 |
x + 1 |
| 224 |
x + 1 |
| 225 |
[builtins fixtures/isinstancelist.pyi] |
| 226 |
|
| 227 |
[case testTwoLoopsUnion] |
| 228 |
# flags: --warn-unreachable |
| 229 |
from typing import Union |
| 230 |
|
| 231 |
def foo() -> Union[int, str]: pass |
| 232 |
|
| 233 |
def bar() -> None: |
| 234 |
x = foo() |
| 235 |
if isinstance(x, int): |
| 236 |
return |
| 237 |
while bool(): |
| 238 |
x + 'a' |
| 239 |
while bool(): |
| 240 |
x = foo() |
| 241 |
if bool(): |
| 242 |
return |
| 243 |
x = 'a' |
| 244 |
x + 'a' |
| 245 |
[builtins fixtures/isinstancelist.pyi] |
| 246 |
|
| 247 |
[case testComplicatedBlocks] |
| 248 |
# flags: --warn-unreachable |
| 249 |
from typing import Union |
| 250 |
|
| 251 |
def foo() -> Union[int, str]: pass |
| 252 |
|
| 253 |
def bar() -> None: |
| 254 |
x = foo() |
| 255 |
if isinstance(x, int): |
| 256 |
return |
| 257 |
while bool(): |
| 258 |
x + 'a' |
| 259 |
while bool(): |
| 260 |
x = foo() |
| 261 |
if bool(): |
| 262 |
return |
| 263 |
x = 'a' |
| 264 |
x + 'a' |
| 265 |
|
| 266 |
x = foo() |
| 267 |
if isinstance(x, int): |
| 268 |
return |
| 269 |
while bool(): |
| 270 |
x + 'a' |
| 271 |
while bool(): |
| 272 |
x + 'a' # E: Unsupported operand types for + ("int" and "str") \ |
| 273 |
# N: Left operand is of type "int | str" |
| 274 |
x = foo() |
| 275 |
if bool(): |
| 276 |
continue |
| 277 |
x = 'a' |
| 278 |
x = 'a' |
| 279 |
x + 'a' |
| 280 |
[builtins fixtures/isinstancelist.pyi] |
| 281 |
|
| 282 |
[case testUnionTryExcept] |
| 283 |
# flags: --warn-unreachable |
| 284 |
class A: |
| 285 |
y = A() |
| 286 |
|
| 287 |
class B(A): |
| 288 |
z = 1 |
| 289 |
|
| 290 |
x = A() |
| 291 |
def f(): x # Prevent redefinition of x |
| 292 |
x = B() |
| 293 |
x.z |
| 294 |
try: |
| 295 |
x.z |
| 296 |
x = A() |
| 297 |
x = B() |
| 298 |
x.z |
| 299 |
except: |
| 300 |
pass |
| 301 |
x.z # E: "A" has no attribute "z" |
| 302 |
|
| 303 |
[case testUnionTryExcept2] |
| 304 |
# flags: --warn-unreachable |
| 305 |
class A: |
| 306 |
y = A() |
| 307 |
|
| 308 |
class B(A): |
| 309 |
z = 1 |
| 310 |
|
| 311 |
x = A() |
| 312 |
try: |
| 313 |
x.z # E: "A" has no attribute "z" |
| 314 |
x = A() |
| 315 |
x = B() |
| 316 |
x.z |
| 317 |
except: |
| 318 |
x.z # E: "A" has no attribute "z" |
| 319 |
x = B() |
| 320 |
x.z |
| 321 |
else: |
| 322 |
x = B() |
| 323 |
x.z |
| 324 |
|
| 325 |
[case testUnionTryExcept3] |
| 326 |
# flags: --warn-unreachable |
| 327 |
class A: |
| 328 |
y = A() |
| 329 |
|
| 330 |
class B(A): |
| 331 |
z = 1 |
| 332 |
|
| 333 |
x = A() |
| 334 |
def f(): x # Prevent redefinition of x |
| 335 |
x = B() |
| 336 |
try: |
| 337 |
raise BaseException() |
| 338 |
x = A() # E: Statement is unreachable |
| 339 |
except: |
| 340 |
pass |
| 341 |
x.z |
| 342 |
x = B() |
| 343 |
try: |
| 344 |
x = A() |
| 345 |
raise BaseException() |
| 346 |
except: |
| 347 |
pass |
| 348 |
x.z # E: "A" has no attribute "z" |
| 349 |
x = B() |
| 350 |
try: |
| 351 |
pass |
| 352 |
except: |
| 353 |
x = A() |
| 354 |
raise BaseException() |
| 355 |
x.z |
| 356 |
try: |
| 357 |
x = A() |
| 358 |
except: |
| 359 |
pass |
| 360 |
x.z # E: "A" has no attribute "z" |
| 361 |
x = B() |
| 362 |
try: |
| 363 |
pass |
| 364 |
except: |
| 365 |
x = A() |
| 366 |
x.z # E: "A" has no attribute "z" |
| 367 |
[builtins fixtures/exception.pyi] |
| 368 |
|
| 369 |
[case testUnionTryExcept4] |
| 370 |
# flags: --warn-unreachable |
| 371 |
class A: pass |
| 372 |
|
| 373 |
class B(A): |
| 374 |
z = 1 |
| 375 |
|
| 376 |
x = A() |
| 377 |
while bool(): |
| 378 |
try: |
| 379 |
x.z # E: "A" has no attribute "z" |
| 380 |
x = A() |
| 381 |
except: |
| 382 |
x = B() |
| 383 |
else: |
| 384 |
x = B() |
| 385 |
x.z |
| 386 |
[builtins fixtures/exception.pyi] |
| 387 |
|
| 388 |
[case testUnionTryFinally] |
| 389 |
# flags: --warn-unreachable |
| 390 |
class A: pass |
| 391 |
|
| 392 |
class B(A): |
| 393 |
b = 1 |
| 394 |
|
| 395 |
x = A() |
| 396 |
def f(): x # Prevent redefinition |
| 397 |
x = B() |
| 398 |
try: |
| 399 |
x = A() |
| 400 |
x.b # E: "A" has no attribute "b" |
| 401 |
x = B() |
| 402 |
finally: |
| 403 |
x.b # E: "A" has no attribute "b" |
| 404 |
x.b |
| 405 |
|
| 406 |
[case testUnionTryFinally2] |
| 407 |
# flags: --warn-unreachable |
| 408 |
class A: pass |
| 409 |
|
| 410 |
class B(A): |
| 411 |
b = 1 |
| 412 |
|
| 413 |
x = A() |
| 414 |
def f(): x # Prevent redefinition |
| 415 |
x = B() |
| 416 |
try: |
| 417 |
x = A() |
| 418 |
x = B() |
| 419 |
except: |
| 420 |
pass |
| 421 |
finally: |
| 422 |
pass |
| 423 |
x.b # E: "A" has no attribute "b" |
| 424 |
|
| 425 |
[case testUnionTryFinally3] |
| 426 |
# flags: --warn-unreachable |
| 427 |
class A: pass |
| 428 |
|
| 429 |
class B(A): |
| 430 |
b = 1 |
| 431 |
|
| 432 |
x = A() |
| 433 |
def f(): x # Prevent redefinition |
| 434 |
x = B() |
| 435 |
try: |
| 436 |
x = A() |
| 437 |
x = B() |
| 438 |
except: |
| 439 |
pass |
| 440 |
finally: |
| 441 |
x = B() |
| 442 |
x.b |
| 443 |
|
| 444 |
[case testUnionTryFinally4] |
| 445 |
# flags: --warn-unreachable |
| 446 |
class A: pass |
| 447 |
|
| 448 |
class B(A): |
| 449 |
b = 1 |
| 450 |
|
| 451 |
while 2: |
| 452 |
x = A() |
| 453 |
def f(): x # Prevents redefinition |
| 454 |
x = B() |
| 455 |
try: |
| 456 |
x = A() |
| 457 |
x = B() |
| 458 |
except: |
| 459 |
pass |
| 460 |
finally: |
| 461 |
x.b # E: "A" has no attribute "b" |
| 462 |
if not isinstance(x, B): |
| 463 |
break |
| 464 |
x.b |
| 465 |
[builtins fixtures/isinstancelist.pyi] |
| 466 |
|
| 467 |
[case testUnionTryFinally5] |
| 468 |
# flags: --warn-unreachable |
| 469 |
class A: pass |
| 470 |
|
| 471 |
class B(A): |
| 472 |
b = 1 |
| 473 |
|
| 474 |
while 2: |
| 475 |
x = A() |
| 476 |
try: |
| 477 |
x = A() |
| 478 |
x = B() |
| 479 |
finally: |
| 480 |
x.b # E: "A" has no attribute "b" |
| 481 |
break |
| 482 |
x.b # E: Statement is unreachable |
| 483 |
x.b # E: Statement is unreachable |
| 484 |
|
| 485 |
[case testUnionTryFinally6] |
| 486 |
# flags: --warn-unreachable |
| 487 |
class A: pass |
| 488 |
|
| 489 |
class B(A): |
| 490 |
b = 1 |
| 491 |
|
| 492 |
def f() -> int: |
| 493 |
x = B() # type: A |
| 494 |
try: |
| 495 |
x = B() |
| 496 |
except: |
| 497 |
x = A() |
| 498 |
# An exception could occur here |
| 499 |
x = B() |
| 500 |
finally: |
| 501 |
return x.b # E: "A" has no attribute "b" |
| 502 |
|
| 503 |
[case testUnionListIsinstance] |
| 504 |
# flags: --warn-unreachable |
| 505 |
from typing import Union, List |
| 506 |
|
| 507 |
def f(x: Union[List[int], List[str], int]) -> None: |
| 508 |
if isinstance(x, list): |
| 509 |
a = x[0] |
| 510 |
if isinstance(a, int): |
| 511 |
a + 1 |
| 512 |
a + 'x' # E: Unsupported operand types for + ("int" and "str") |
| 513 |
|
| 514 |
# type of a? |
| 515 |
reveal_type(x) # N: Revealed type is "builtins.list[builtins.int] | builtins.list[builtins.str]" |
| 516 |
x + 1 # E: Unsupported operand types for + ("list[int]" and "int") \ |
| 517 |
# E: Unsupported operand types for + ("list[str]" and "int") \ |
| 518 |
# N: Left operand is of type "list[int] | list[str]" |
| 519 |
else: |
| 520 |
x[0] # E: Value of type "int" is not indexable |
| 521 |
x + 1 |
| 522 |
x[0] # E: Value of type "list[int] | list[str] | int" is not indexable |
| 523 |
x + 1 # E: Unsupported operand types for + ("list[int]" and "int") \ |
| 524 |
# E: Unsupported operand types for + ("list[str]" and "int") \ |
| 525 |
# N: Left operand is of type "list[int] | list[str] | int" |
| 526 |
[builtins fixtures/isinstancelist.pyi] |
| 527 |
|
| 528 |
[case testUnionListIsinstance2] |
| 529 |
# flags: --warn-unreachable |
| 530 |
from typing import Union, List |
| 531 |
|
| 532 |
class A: |
| 533 |
a = 1 |
| 534 |
|
| 535 |
class B: pass |
| 536 |
class C: pass |
| 537 |
|
| 538 |
def g(x: Union[A, B]) -> A: pass |
| 539 |
def h(x: C) -> A: pass |
| 540 |
def f(x: Union[A, B, C]) -> None: |
| 541 |
if isinstance(x, C): |
| 542 |
x = h(x) |
| 543 |
else: |
| 544 |
x = g(x) |
| 545 |
x.a |
| 546 |
[builtins fixtures/isinstancelist.pyi] |
| 547 |
|
| 548 |
[case testUnionStrictDefnBasic] |
| 549 |
# flags: --warn-unreachable |
| 550 |
from typing import Union |
| 551 |
|
| 552 |
def foo() -> Union[int, str]: pass |
| 553 |
|
| 554 |
x = foo() |
| 555 |
if int(): |
| 556 |
x = 1 |
| 557 |
x = x + 1 |
| 558 |
x = foo() |
| 559 |
x = x + 1 # E: Unsupported operand types for + ("str" and "int") \ |
| 560 |
# N: Left operand is of type "int | str" |
| 561 |
if isinstance(x, str): |
| 562 |
x = x + 1 # E: Unsupported operand types for + ("str" and "int") |
| 563 |
x = 1 |
| 564 |
x = x + 1 |
| 565 |
[builtins fixtures/isinstancelist.pyi] |
| 566 |
|
| 567 |
[case testSubtypeRedefinitionBasic] |
| 568 |
# flags: --warn-unreachable |
| 569 |
from typing import Union |
| 570 |
|
| 571 |
class A: pass |
| 572 |
|
| 573 |
class B(A): |
| 574 |
y = 1 |
| 575 |
|
| 576 |
x = A() |
| 577 |
x.y # E: "A" has no attribute "y" |
| 578 |
x = B() |
| 579 |
x.y # OK: x is known to be a B |
| 580 |
[builtins fixtures/isinstancelist.pyi] |
| 581 |
|
| 582 |
[case testIsInstanceBasic] |
| 583 |
# flags: --warn-unreachable |
| 584 |
from typing import Union |
| 585 |
x: Union[int, str] |
| 586 |
if isinstance(x, str): |
| 587 |
x = x + 1 # E: Unsupported operand types for + ("str" and "int") |
| 588 |
x = x + 'a' |
| 589 |
else: |
| 590 |
x = x + 'a' # E: Unsupported operand types for + ("int" and "str") |
| 591 |
x = x + 1 |
| 592 |
[builtins fixtures/isinstancelist.pyi] |
| 593 |
|
| 594 |
[case testIsInstanceIndexing] |
| 595 |
# flags: --warn-unreachable |
| 596 |
from typing import Union |
| 597 |
x: Union[int, str] |
| 598 |
j = [x] |
| 599 |
if isinstance(j[0], str): |
| 600 |
j[0] = j[0] + 'a' |
| 601 |
j[0] = j[0] + 1 # E: Unsupported operand types for + ("str" and "int") |
| 602 |
else: |
| 603 |
j[0] = j[0] + 'a' # E: Unsupported operand types for + ("int" and "str") |
| 604 |
j[0] = j[0] + 1 |
| 605 |
[builtins fixtures/isinstancelist.pyi] |
| 606 |
|
| 607 |
[case testIsInstanceSubClassMember] |
| 608 |
# flags: --warn-unreachable |
| 609 |
from typing import Union |
| 610 |
|
| 611 |
class Animal: pass |
| 612 |
|
| 613 |
class Dog(Animal): |
| 614 |
paws = 4 # type: Union[int, str] |
| 615 |
|
| 616 |
def bark(self): pass |
| 617 |
|
| 618 |
class House: |
| 619 |
pet: Animal |
| 620 |
|
| 621 |
h = House() |
| 622 |
h.pet = Dog() |
| 623 |
|
| 624 |
while bool(): |
| 625 |
if isinstance(h.pet, Dog): |
| 626 |
if isinstance(h.pet.paws, str): |
| 627 |
x = h.pet.paws + 'a' |
| 628 |
y = h.pet.paws + 1 # E: Unsupported operand types for + ("str" and "int") \ |
| 629 |
# N: Left operand is of type "int | str" |
| 630 |
z = h.pet.paws + 'a' # E: Unsupported operand types for + ("int" and "str") \ |
| 631 |
# N: Left operand is of type "int | str" |
| 632 |
if isinstance(h.pet.paws, str): |
| 633 |
x = h.pet.paws + 'a' |
| 634 |
break |
| 635 |
y = h.pet.paws + 1 |
| 636 |
z = h.pet.paws + 'a' # E: Unsupported operand types for + ("int" and "str") |
| 637 |
[builtins fixtures/isinstancelist.pyi] |
| 638 |
|
| 639 |
[case testIsInstanceSubClassReset] |
| 640 |
# flags: --warn-unreachable |
| 641 |
class A: |
| 642 |
pass |
| 643 |
|
| 644 |
class B(A): |
| 645 |
b = 1 |
| 646 |
|
| 647 |
class C: |
| 648 |
a = A() |
| 649 |
|
| 650 |
x = C() |
| 651 |
x.a.b # E: "A" has no attribute "b" |
| 652 |
if isinstance(x.a, B): |
| 653 |
x.a.b |
| 654 |
x = C() |
| 655 |
x.a.b # E: "A" has no attribute "b" |
| 656 |
[builtins fixtures/isinstance.pyi] |
| 657 |
|
| 658 |
[case testIsinstanceTuple] |
| 659 |
# flags: --warn-unreachable |
| 660 |
from typing import Union |
| 661 |
|
| 662 |
class A: pass |
| 663 |
|
| 664 |
class B: |
| 665 |
def method2(self, arg: int): |
| 666 |
return 123 |
| 667 |
|
| 668 |
class C: |
| 669 |
def method2(self, arg: int): |
| 670 |
return 456 |
| 671 |
|
| 672 |
def method3(self, arg: str): |
| 673 |
return 'abc' |
| 674 |
|
| 675 |
v = A() # type: Union[A, B, C] |
| 676 |
|
| 677 |
if isinstance(v, (B, C)): |
| 678 |
v.method2(123) |
| 679 |
v.method3('xyz') # E: Item "B" of "B | C" has no attribute "method3" |
| 680 |
[builtins fixtures/isinstance.pyi] |
| 681 |
|
| 682 |
[case testIsinstanceNeverWidens] |
| 683 |
# flags: --warn-unreachable |
| 684 |
from typing import Union |
| 685 |
|
| 686 |
class A: pass |
| 687 |
class B: pass |
| 688 |
class C: pass |
| 689 |
|
| 690 |
a = A() # type: A |
| 691 |
assert isinstance(a, (A, B)) |
| 692 |
reveal_type(a) # N: Revealed type is "__main__.A" |
| 693 |
|
| 694 |
b = A() # type: Union[A, B] |
| 695 |
assert isinstance(b, (A, B, C)) |
| 696 |
reveal_type(b) # N: Revealed type is "__main__.A | __main__.B" |
| 697 |
[builtins fixtures/isinstance.pyi] |
| 698 |
|
| 699 |
[case testMemberAssignmentChanges] |
| 700 |
# flags: --warn-unreachable |
| 701 |
from typing import Union |
| 702 |
|
| 703 |
class Dog: |
| 704 |
paws = 1 # type: Union[int, str] |
| 705 |
|
| 706 |
pet = Dog() |
| 707 |
|
| 708 |
pet.paws + 'a' # E: Unsupported operand types for + ("int" and "str") \ |
| 709 |
# N: Left operand is of type "int | str" |
| 710 |
pet.paws = 'a' |
| 711 |
pet.paws + 'a' |
| 712 |
pet.paws = 1 |
| 713 |
pet.paws + 1 |
| 714 |
[builtins fixtures/isinstancelist.pyi] |
| 715 |
|
| 716 |
[case testIsInstanceSubClassMemberHard] |
| 717 |
# flags: --warn-unreachable |
| 718 |
from typing import Union |
| 719 |
|
| 720 |
class Animal: |
| 721 |
pass |
| 722 |
|
| 723 |
class Dog(Animal): |
| 724 |
paws = 4 # type: Union[int, str] |
| 725 |
|
| 726 |
def bark(self): pass |
| 727 |
|
| 728 |
class House: |
| 729 |
pet: Animal |
| 730 |
|
| 731 |
h = House() |
| 732 |
h.pet = Dog() |
| 733 |
|
| 734 |
if isinstance(h.pet, Dog): |
| 735 |
if isinstance(h.pet.paws, str): |
| 736 |
for i in [1]: |
| 737 |
# TODO: should we allow this if iterable is of length one or zero? |
| 738 |
h.pet.paws + 'a' # E: Unsupported operand types for + ("int" and "str") \ |
| 739 |
# N: Left operand is of type "int | str" |
| 740 |
if bool(): |
| 741 |
break |
| 742 |
h.pet.paws = 1 |
| 743 |
h.pet.paws + 1 |
| 744 |
|
| 745 |
if isinstance(h.pet.paws, str): |
| 746 |
h.pet.paws + 'a' |
| 747 |
else: |
| 748 |
h.pet.paws + 1 |
| 749 |
[builtins fixtures/isinstancelist.pyi] |
| 750 |
|
| 751 |
[case testIsInstanceReturn] |
| 752 |
# flags: --warn-unreachable |
| 753 |
from typing import Union |
| 754 |
|
| 755 |
def foo() -> None: |
| 756 |
x = 1 # type: Union[int, str] |
| 757 |
if isinstance(x, int): |
| 758 |
return |
| 759 |
y = x + 'asdad' |
| 760 |
|
| 761 |
def bar() -> None: |
| 762 |
x = 1 # type: Union[int, str] |
| 763 |
if isinstance(x, int): |
| 764 |
return |
| 765 |
else: |
| 766 |
pass |
| 767 |
y = x + 'asdad' |
| 768 |
|
| 769 |
foo() |
| 770 |
[builtins fixtures/isinstancelist.pyi] |
| 771 |
|
| 772 |
[case testIsInstanceBadBreak] |
| 773 |
# flags: --warn-unreachable |
| 774 |
from typing import Union |
| 775 |
|
| 776 |
def foo() -> None: |
| 777 |
x: Union[int, str] |
| 778 |
if isinstance(x, int): |
| 779 |
for z in [1,2]: |
| 780 |
break |
| 781 |
else: |
| 782 |
pass |
| 783 |
y = x + 'asdad' # E: Unsupported operand types for + ("int" and "str") \ |
| 784 |
# N: Left operand is of type "int | str" |
| 785 |
|
| 786 |
foo() |
| 787 |
[builtins fixtures/isinstancelist.pyi] |
| 788 |
|
| 789 |
[case testIsInstanceThreeUnion] |
| 790 |
# flags: --warn-unreachable |
| 791 |
from typing import Union, List |
| 792 |
|
| 793 |
x: Union[int, str, List[int]] |
| 794 |
|
| 795 |
while bool(): |
| 796 |
if isinstance(x, int): |
| 797 |
x + 1 |
| 798 |
elif isinstance(x, str): |
| 799 |
x + 'a' |
| 800 |
else: |
| 801 |
x + [1] |
| 802 |
x + 'a' # E: Unsupported operand types for + ("int" and "str") \ |
| 803 |
# E: Unsupported operand types for + ("list[int]" and "str") \ |
| 804 |
# N: Left operand is of type "int | str | list[int]" |
| 805 |
|
| 806 |
x + [1] # E: Unsupported operand types for + ("int" and "list[int]") \ |
| 807 |
# E: Unsupported operand types for + ("str" and "list[int]") \ |
| 808 |
# N: Left operand is of type "int | str | list[int]" |
| 809 |
[builtins fixtures/isinstancelist.pyi] |
| 810 |
|
| 811 |
[case testIsInstanceThreeUnion2] |
| 812 |
# flags: --warn-unreachable |
| 813 |
from typing import Union, List |
| 814 |
x: Union[int, str, List[int]] |
| 815 |
while bool(): |
| 816 |
if isinstance(x, int): |
| 817 |
x + 1 |
| 818 |
break |
| 819 |
elif isinstance(x, str): |
| 820 |
x + 'a' |
| 821 |
break |
| 822 |
x + [1] |
| 823 |
x + 'a' # E: Unsupported operand types for + ("list[int]" and "str") |
| 824 |
x + [1] # E: Unsupported operand types for + ("int" and "list[int]") \ |
| 825 |
# E: Unsupported operand types for + ("str" and "list[int]") \ |
| 826 |
# N: Left operand is of type "int | str | list[int]" |
| 827 |
[builtins fixtures/isinstancelist.pyi] |
| 828 |
|
| 829 |
[case testIsInstanceThreeUnion3] |
| 830 |
# flags: --warn-unreachable |
| 831 |
from typing import Union, List |
| 832 |
|
| 833 |
while bool(): |
| 834 |
x: Union[int, str, List[int]] |
| 835 |
def f(): x # Prevent redefinition |
| 836 |
x = 1 |
| 837 |
if isinstance(x, int): |
| 838 |
x + 1 |
| 839 |
break |
| 840 |
elif isinstance(x, str): # E: Statement is unreachable |
| 841 |
x + 'a' |
| 842 |
break |
| 843 |
x + [1] # These lines aren't reached because x was an int # E: Statement is unreachable |
| 844 |
x + 'a' |
| 845 |
x + [1] # E: Unsupported operand types for + ("int" and "list[int]") \ |
| 846 |
# E: Unsupported operand types for + ("str" and "list[int]") \ |
| 847 |
# N: Left operand is of type "int | str | list[int]" |
| 848 |
|
| 849 |
|
| 850 |
[builtins fixtures/isinstancelist.pyi] |
| 851 |
|
| 852 |
[case testRemovingTypeRepeatedly] |
| 853 |
# flags: --warn-unreachable |
| 854 |
from typing import Union |
| 855 |
|
| 856 |
def foo() -> Union[int, str]: pass |
| 857 |
|
| 858 |
for i in [1, 2]: |
| 859 |
x = foo() |
| 860 |
x + 'a' # E: Unsupported operand types for + ("int" and "str") \ |
| 861 |
# N: Left operand is of type "int | str" |
| 862 |
if isinstance(x, int): |
| 863 |
break |
| 864 |
x + 'a' |
| 865 |
|
| 866 |
x = foo() |
| 867 |
x + 'a' # E: Unsupported operand types for + ("int" and "str") \ |
| 868 |
# N: Left operand is of type "int | str" |
| 869 |
if isinstance(x, int): |
| 870 |
break |
| 871 |
x + 'a' |
| 872 |
|
| 873 |
x = foo() |
| 874 |
x + 'a' # E: Unsupported operand types for + ("int" and "str") \ |
| 875 |
# N: Left operand is of type "int | str" |
| 876 |
if isinstance(x, int): |
| 877 |
break |
| 878 |
x + 'a' |
| 879 |
|
| 880 |
x + 'a' # E: Unsupported operand types for + ("int" and "str") \ |
| 881 |
# N: Left operand is of type "int | str" |
| 882 |
[builtins fixtures/isinstancelist.pyi] |
| 883 |
|
| 884 |
[case testModifyRepeatedly] |
| 885 |
# flags: --warn-unreachable |
| 886 |
from typing import Union |
| 887 |
|
| 888 |
def foo() -> Union[int, str]: pass |
| 889 |
|
| 890 |
x = foo() |
| 891 |
def f(): x # Prevent redefinition |
| 892 |
x + 1 # E: Unsupported operand types for + ("str" and "int") \ |
| 893 |
# N: Left operand is of type "int | str" |
| 894 |
x + 'a' # E: Unsupported operand types for + ("int" and "str") \ |
| 895 |
# N: Left operand is of type "int | str" |
| 896 |
|
| 897 |
x = 1 |
| 898 |
x + 1 |
| 899 |
x + 'a' # E: Unsupported operand types for + ("int" and "str") |
| 900 |
|
| 901 |
x = 'a' |
| 902 |
x + 1 # E: Unsupported operand types for + ("str" and "int") |
| 903 |
x + 'a' |
| 904 |
|
| 905 |
x = foo() |
| 906 |
x + 1 # E: Unsupported operand types for + ("str" and "int") \ |
| 907 |
# N: Left operand is of type "int | str" |
| 908 |
x + 'a' # E: Unsupported operand types for + ("int" and "str") \ |
| 909 |
# N: Left operand is of type "int | str" |
| 910 |
[builtins fixtures/isinstancelist.pyi] |
| 911 |
|
| 912 |
[case testModifyLoop] |
| 913 |
# flags: --warn-unreachable |
| 914 |
from typing import Union |
| 915 |
|
| 916 |
def foo() -> Union[int, str]: pass |
| 917 |
|
| 918 |
x = foo() |
| 919 |
def f(): x # Prevent redefinition |
| 920 |
x + 1 # E: Unsupported operand types for + ("str" and "int") \ |
| 921 |
# N: Left operand is of type "int | str" |
| 922 |
x = 'a' |
| 923 |
x + 1 # E: Unsupported operand types for + ("str" and "int") |
| 924 |
x = 1 |
| 925 |
x + 1 |
| 926 |
|
| 927 |
while bool(): |
| 928 |
x + 1 # E: Unsupported operand types for + ("str" and "int") \ |
| 929 |
# N: Left operand is of type "int | str" |
| 930 |
x = 'a' |
| 931 |
[builtins fixtures/isinstancelist.pyi] |
| 932 |
|
| 933 |
[case testModifyLoop2] |
| 934 |
# flags: --warn-unreachable |
| 935 |
from typing import Union |
| 936 |
|
| 937 |
def foo() -> Union[int, str]: pass |
| 938 |
|
| 939 |
x = foo() |
| 940 |
def f(): x # Prevent redefinition |
| 941 |
x + 1 # E: Unsupported operand types for + ("str" and "int") \ |
| 942 |
# N: Left operand is of type "int | str" |
| 943 |
x = 'a' |
| 944 |
x + 1 # E: Unsupported operand types for + ("str" and "int") |
| 945 |
x = 1 |
| 946 |
x + 1 |
| 947 |
|
| 948 |
for i in [1]: |
| 949 |
x = 'a' |
| 950 |
|
| 951 |
x + 1 # E: Unsupported operand types for + ("str" and "int") \ |
| 952 |
# N: Left operand is of type "int | str" |
| 953 |
[builtins fixtures/isinstancelist.pyi] |
| 954 |
|
| 955 |
[case testModifyLoop3] |
| 956 |
# flags: --warn-unreachable |
| 957 |
from typing import Union |
| 958 |
|
| 959 |
def foo() -> Union[int, str]: pass |
| 960 |
|
| 961 |
x = foo() |
| 962 |
def f(): x # Prevent redefinition |
| 963 |
x = 1 |
| 964 |
|
| 965 |
while bool(): |
| 966 |
x + 1 |
| 967 |
x = 'a' |
| 968 |
break |
| 969 |
else: |
| 970 |
x + 1 |
| 971 |
x + 1 # E: Unsupported operand types for + ("str" and "int") \ |
| 972 |
# N: Left operand is of type "int | str" |
| 973 |
x = 1 |
| 974 |
for y in [1]: |
| 975 |
x + 1 |
| 976 |
x = 'a' |
| 977 |
break |
| 978 |
else: |
| 979 |
x + 1 |
| 980 |
x + 1 # E: Unsupported operand types for + ("str" and "int") \ |
| 981 |
# N: Left operand is of type "int | str" |
| 982 |
[builtins fixtures/isinstancelist.pyi] |
| 983 |
|
| 984 |
[case testModifyLoopWhile4] |
| 985 |
# flags: --warn-unreachable |
| 986 |
from typing import Union |
| 987 |
|
| 988 |
def foo() -> Union[int, str]: pass |
| 989 |
|
| 990 |
x = foo() |
| 991 |
def f(): x # Prevent redefinition |
| 992 |
x = 1 |
| 993 |
|
| 994 |
while bool(): |
| 995 |
x + 1 |
| 996 |
if bool(): |
| 997 |
x = 'a' |
| 998 |
break |
| 999 |
else: |
| 1000 |
x + 1 |
| 1001 |
x = 'a' |
| 1002 |
x + 'a' |
| 1003 |
x = 1 |
| 1004 |
while bool(): |
| 1005 |
x + 1 # E: Unsupported operand types for + ("str" and "int") \ |
| 1006 |
# N: Left operand is of type "int | str" |
| 1007 |
if bool(): |
| 1008 |
x = 'a' |
| 1009 |
continue |
| 1010 |
else: |
| 1011 |
x + 1 # E: Unsupported operand types for + ("str" and "int") \ |
| 1012 |
# N: Left operand is of type "int | str" |
| 1013 |
x = 'a' |
| 1014 |
x + 'a' |
| 1015 |
[builtins fixtures/isinstancelist.pyi] |
| 1016 |
|
| 1017 |
[case testModifyLoopFor4] |
| 1018 |
# flags: --warn-unreachable |
| 1019 |
from typing import Union |
| 1020 |
|
| 1021 |
def foo() -> Union[int, str]: pass |
| 1022 |
|
| 1023 |
x = foo() |
| 1024 |
def f(): x # Prevent redefinition |
| 1025 |
x = 1 |
| 1026 |
|
| 1027 |
for y in [1]: |
| 1028 |
x + 1 |
| 1029 |
if bool(): |
| 1030 |
x = 'a' |
| 1031 |
break |
| 1032 |
else: |
| 1033 |
x + 1 |
| 1034 |
x = 'a' |
| 1035 |
x + 'a' |
| 1036 |
x = 1 |
| 1037 |
for y in [1]: |
| 1038 |
x + 1 # E: Unsupported operand types for + ("str" and "int") \ |
| 1039 |
# N: Left operand is of type "int | str" |
| 1040 |
if bool(): |
| 1041 |
x = 'a' |
| 1042 |
continue |
| 1043 |
else: |
| 1044 |
x + 1 # E: Unsupported operand types for + ("str" and "int") \ |
| 1045 |
# N: Left operand is of type "int | str" |
| 1046 |
x = 'a' |
| 1047 |
x + 'a' |
| 1048 |
[builtins fixtures/isinstancelist.pyi] |
| 1049 |
|
| 1050 |
[case testModifyNestedLoop] |
| 1051 |
# flags: --warn-unreachable |
| 1052 |
from typing import Union |
| 1053 |
|
| 1054 |
def foo() -> Union[int, str]: pass |
| 1055 |
|
| 1056 |
x = foo() |
| 1057 |
def f(): x # Prevent redefinition |
| 1058 |
x = 1 |
| 1059 |
|
| 1060 |
for y in [1]: |
| 1061 |
for z in [1]: |
| 1062 |
break |
| 1063 |
else: |
| 1064 |
x = 'a' |
| 1065 |
break |
| 1066 |
else: |
| 1067 |
x + 1 |
| 1068 |
x + 1 # E: Unsupported operand types for + ("str" and "int") \ |
| 1069 |
# N: Left operand is of type "int | str" |
| 1070 |
x = 1 |
| 1071 |
while bool(): |
| 1072 |
while bool(): |
| 1073 |
break |
| 1074 |
else: |
| 1075 |
x = 'a' |
| 1076 |
break |
| 1077 |
else: |
| 1078 |
x + 1 |
| 1079 |
x + 1 # E: Unsupported operand types for + ("str" and "int") \ |
| 1080 |
# N: Left operand is of type "int | str" |
| 1081 |
[builtins fixtures/isinstancelist.pyi] |
| 1082 |
|
| 1083 |
[case testModifyLoopLong] |
| 1084 |
# flags: --warn-unreachable |
| 1085 |
from typing import Union |
| 1086 |
|
| 1087 |
class A: a = 1 |
| 1088 |
|
| 1089 |
def foo() -> Union[int, str, A]: pass |
| 1090 |
|
| 1091 |
def bar() -> None: |
| 1092 |
x = foo() |
| 1093 |
x + 1 # E: Unsupported left operand type for + ("A") \ |
| 1094 |
# N: Left operand is of type "int | str | A" \ |
| 1095 |
# E: Unsupported operand types for + ("str" and "int") |
| 1096 |
if isinstance(x, A): |
| 1097 |
x.a |
| 1098 |
else: |
| 1099 |
if isinstance(x, int): |
| 1100 |
x + 1 |
| 1101 |
x + 'a' # E: Unsupported operand types for + ("int" and "str") |
| 1102 |
else: |
| 1103 |
x + 'a' |
| 1104 |
x.a # E: "str" has no attribute "a" |
| 1105 |
x = A() |
| 1106 |
|
| 1107 |
if isinstance(x, str): |
| 1108 |
x + 'a' |
| 1109 |
else: |
| 1110 |
while bool(): |
| 1111 |
if isinstance(x, int): |
| 1112 |
x + 1 |
| 1113 |
else: |
| 1114 |
x.a |
| 1115 |
break |
| 1116 |
while bool(): |
| 1117 |
if isinstance(x, int): |
| 1118 |
x + 1 |
| 1119 |
else: |
| 1120 |
x.a |
| 1121 |
continue |
| 1122 |
|
| 1123 |
while bool(): |
| 1124 |
if isinstance(x, int): |
| 1125 |
x + 1 |
| 1126 |
else: |
| 1127 |
x.a # E: Item "str" of "str | A" has no attribute "a" |
| 1128 |
x = 'a' |
| 1129 |
[builtins fixtures/isinstancelist.pyi] |
| 1130 |
|
| 1131 |
[case testWhileExitCondition1] |
| 1132 |
# flags: --warn-unreachable |
| 1133 |
from typing import Union |
| 1134 |
x = 1 # type: Union[int, str] |
| 1135 |
while isinstance(x, int): |
| 1136 |
if bool(): |
| 1137 |
continue |
| 1138 |
x = 'a' |
| 1139 |
else: |
| 1140 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 1141 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 1142 |
[builtins fixtures/isinstance.pyi] |
| 1143 |
|
| 1144 |
[case testWhileExitCondition2] |
| 1145 |
# flags: --warn-unreachable |
| 1146 |
from typing import Union |
| 1147 |
x = 1 # type: Union[int, str] |
| 1148 |
while isinstance(x, int): |
| 1149 |
if bool(): |
| 1150 |
break |
| 1151 |
x = 'a' |
| 1152 |
else: |
| 1153 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 1154 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 1155 |
[builtins fixtures/isinstance.pyi] |
| 1156 |
|
| 1157 |
[case testWhileLinkedList] |
| 1158 |
# flags: --warn-unreachable |
| 1159 |
from typing import Union |
| 1160 |
LinkedList = Union['Cons', 'Nil'] |
| 1161 |
class Nil: pass |
| 1162 |
class Cons: |
| 1163 |
tail: LinkedList |
| 1164 |
def last(x: LinkedList) -> Nil: |
| 1165 |
while isinstance(x, Cons): |
| 1166 |
x = x.tail |
| 1167 |
return x |
| 1168 |
[builtins fixtures/isinstance.pyi] |
| 1169 |
|
| 1170 |
[case testReturnAndFlow] |
| 1171 |
# flags: --warn-unreachable |
| 1172 |
def foo() -> int: |
| 1173 |
return 1 and 2 |
| 1174 |
return 'a' # E: Statement is unreachable |
| 1175 |
[case testCastIsinstance] |
| 1176 |
# flags: --warn-unreachable |
| 1177 |
from typing import Union |
| 1178 |
|
| 1179 |
def foo() -> Union[int, str]: pass |
| 1180 |
|
| 1181 |
x = foo() |
| 1182 |
y = 1 # type: int |
| 1183 |
|
| 1184 |
if isinstance(x, str): |
| 1185 |
x = y |
| 1186 |
x + 1 |
| 1187 |
x + 'a' # E: Unsupported operand types for + ("int" and "str") |
| 1188 |
[builtins fixtures/isinstancelist.pyi] |
| 1189 |
|
| 1190 |
[case testUnreachableCode] |
| 1191 |
# flags: --warn-unreachable |
| 1192 |
x = 1 # type: int |
| 1193 |
|
| 1194 |
while bool(): |
| 1195 |
x = 'a' # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 1196 |
break |
| 1197 |
x = 'a' # Note: no error because unreachable code # E: Statement is unreachable |
| 1198 |
[builtins fixtures/isinstancelist.pyi] |
| 1199 |
|
| 1200 |
[case testUnreachableCode2] |
| 1201 |
# flags: --warn-unreachable |
| 1202 |
x = 1 |
| 1203 |
while bool(): |
| 1204 |
try: |
| 1205 |
pass |
| 1206 |
except: |
| 1207 |
continue |
| 1208 |
else: |
| 1209 |
continue |
| 1210 |
x + 'a' # E: Statement is unreachable |
| 1211 |
[builtins fixtures/isinstance.pyi] |
| 1212 |
|
| 1213 |
[case testUnreachableWhileTrue] |
| 1214 |
# flags: --warn-unreachable |
| 1215 |
def f(x: int) -> None: |
| 1216 |
while True: |
| 1217 |
if x: |
| 1218 |
return |
| 1219 |
1() # E: Statement is unreachable |
| 1220 |
[builtins fixtures/bool.pyi] |
| 1221 |
|
| 1222 |
[case testUnreachableAssertFalse] |
| 1223 |
# flags: --warn-unreachable |
| 1224 |
def f() -> None: |
| 1225 |
assert False |
| 1226 |
1() # E: Statement is unreachable |
| 1227 |
[builtins fixtures/bool.pyi] |
| 1228 |
|
| 1229 |
[case testUnreachableAssertFalse2] |
| 1230 |
# flags: --warn-unreachable |
| 1231 |
def f() -> None: |
| 1232 |
# The old parser doesn't understand the syntax below |
| 1233 |
assert False, "hi" |
| 1234 |
1() # E: Statement is unreachable |
| 1235 |
[builtins fixtures/bool.pyi] |
| 1236 |
|
| 1237 |
[case testUnreachableReturnOrAssertFalse] |
| 1238 |
# flags: --warn-unreachable |
| 1239 |
def f(x: int) -> int: |
| 1240 |
if x: |
| 1241 |
return x |
| 1242 |
else: |
| 1243 |
assert False |
| 1244 |
1() # E: Statement is unreachable |
| 1245 |
[builtins fixtures/bool.pyi] |
| 1246 |
|
| 1247 |
[case testUnreachableTryExcept] |
| 1248 |
# flags: --warn-unreachable |
| 1249 |
def f() -> None: |
| 1250 |
try: |
| 1251 |
f() |
| 1252 |
return |
| 1253 |
except BaseException: |
| 1254 |
return |
| 1255 |
1() # E: Statement is unreachable |
| 1256 |
[builtins fixtures/exception.pyi] |
| 1257 |
|
| 1258 |
[case testUnreachableTryExceptElse] |
| 1259 |
# flags: --warn-unreachable |
| 1260 |
def f() -> None: |
| 1261 |
try: |
| 1262 |
f() |
| 1263 |
except BaseException: |
| 1264 |
return |
| 1265 |
else: |
| 1266 |
return |
| 1267 |
1() # E: Statement is unreachable |
| 1268 |
[builtins fixtures/exception.pyi] |
| 1269 |
|
| 1270 |
[case testUnreachableTryReturnFinally1] |
| 1271 |
# flags: --warn-unreachable |
| 1272 |
def f() -> None: |
| 1273 |
try: |
| 1274 |
return |
| 1275 |
finally: |
| 1276 |
pass |
| 1277 |
1() # E: Statement is unreachable |
| 1278 |
|
| 1279 |
[case testUnreachableTryReturnFinally2] |
| 1280 |
# flags: --warn-unreachable |
| 1281 |
def f() -> None: |
| 1282 |
try: |
| 1283 |
pass |
| 1284 |
finally: |
| 1285 |
return |
| 1286 |
1() # E: Statement is unreachable |
| 1287 |
|
| 1288 |
[case testUnreachableTryReturnExceptRaise] |
| 1289 |
# flags: --warn-unreachable |
| 1290 |
def f() -> None: |
| 1291 |
try: |
| 1292 |
return |
| 1293 |
except: |
| 1294 |
raise |
| 1295 |
1() # E: Statement is unreachable |
| 1296 |
|
| 1297 |
[case testUnreachableReturnLambda] |
| 1298 |
# flags: --warn-unreachable |
| 1299 |
from typing import Callable |
| 1300 |
def g(t: Callable[[int], int]) -> int: pass |
| 1301 |
def f() -> int: |
| 1302 |
return g(lambda x: x) |
| 1303 |
1() # E: Statement is unreachable |
| 1304 |
|
| 1305 |
[case testIsinstanceAnd] |
| 1306 |
# flags: --warn-unreachable |
| 1307 |
class A: pass |
| 1308 |
|
| 1309 |
class B(A): |
| 1310 |
flag = 1 |
| 1311 |
|
| 1312 |
x = B() # type: A |
| 1313 |
|
| 1314 |
if isinstance(x, B) and 1: |
| 1315 |
x.flag |
| 1316 |
[builtins fixtures/isinstancelist.pyi] |
| 1317 |
|
| 1318 |
[case testIsinstanceShortcircuit] |
| 1319 |
# flags: --warn-unreachable |
| 1320 |
class A: |
| 1321 |
pass |
| 1322 |
|
| 1323 |
class B(A): |
| 1324 |
flag = 1 |
| 1325 |
|
| 1326 |
x = B() # type: A |
| 1327 |
|
| 1328 |
if isinstance(x, B) and x.flag: |
| 1329 |
pass |
| 1330 |
if isinstance(x, B) or x.flag: # E: "A" has no attribute "flag" |
| 1331 |
pass |
| 1332 |
if not isinstance(x, B) or x.flag: |
| 1333 |
pass |
| 1334 |
if not isinstance(x, B) and x.flag: # E: "A" has no attribute "flag" |
| 1335 |
pass |
| 1336 |
[builtins fixtures/isinstancelist.pyi] |
| 1337 |
|
| 1338 |
[case testIsinstanceExpression] |
| 1339 |
# flags: --warn-unreachable |
| 1340 |
class A: |
| 1341 |
pass |
| 1342 |
|
| 1343 |
class B(A): |
| 1344 |
flag = 1 |
| 1345 |
|
| 1346 |
x = B() # type: A |
| 1347 |
|
| 1348 |
x.flag if isinstance(x, B) else 0 |
| 1349 |
0 if not isinstance(x, B) else x.flag |
| 1350 |
0 if isinstance(x, B) else x.flag # E: "A" has no attribute "flag" |
| 1351 |
[builtins fixtures/isinstancelist.pyi] |
| 1352 |
|
| 1353 |
[case testIsinstanceMultiAnd] |
| 1354 |
# flags: --warn-unreachable |
| 1355 |
class A: pass |
| 1356 |
|
| 1357 |
class B(A): |
| 1358 |
flag = 1 |
| 1359 |
|
| 1360 |
class C(A): |
| 1361 |
glaf = 1 |
| 1362 |
|
| 1363 |
x = B() # type: A |
| 1364 |
y = C() # type: A |
| 1365 |
|
| 1366 |
if isinstance(x, B) and isinstance(y, C): |
| 1367 |
x.flag += 1 |
| 1368 |
y.glaf += 1 |
| 1369 |
x() # E: "B" not callable |
| 1370 |
y() # E: "C" not callable |
| 1371 |
else: |
| 1372 |
x() # E: "A" not callable |
| 1373 |
y() # E: "A" not callable |
| 1374 |
[builtins fixtures/isinstancelist.pyi] |
| 1375 |
|
| 1376 |
[case testIsinstanceMultiAndSpecialCase] |
| 1377 |
# flags: --warn-unreachable |
| 1378 |
class A: |
| 1379 |
# Ensure A.__add__ and int.__add__ are different to |
| 1380 |
# force 'isinstance(y, int)' checks below to never succeed. |
| 1381 |
def __add__(self, other: A) -> A: pass |
| 1382 |
|
| 1383 |
class B(A): |
| 1384 |
flag = 1 |
| 1385 |
|
| 1386 |
class C(A): |
| 1387 |
glaf = 1 |
| 1388 |
|
| 1389 |
x = B() # type: A |
| 1390 |
y = C() # type: A |
| 1391 |
|
| 1392 |
if isinstance(x, B) and isinstance(y, int): # E: Subclass of "A" and "int" cannot exist: would have incompatible method signatures |
| 1393 |
1() # E: Statement is unreachable |
| 1394 |
if isinstance(y, int) and isinstance(x, B): # E: Subclass of "A" and "int" cannot exist: would have incompatible method signatures \ |
| 1395 |
# E: Right operand of "and" is never evaluated |
| 1396 |
1() # E: Statement is unreachable |
| 1397 |
if isinstance(y, int) and y > 42: # E: Subclass of "A" and "int" cannot exist: would have incompatible method signatures \ |
| 1398 |
# E: Right operand of "and" is never evaluated |
| 1399 |
1() # E: Statement is unreachable |
| 1400 |
[builtins fixtures/isinstancelist.pyi] |
| 1401 |
|
| 1402 |
[case testReturnWithCallExprAndIsinstance] |
| 1403 |
# flags: --warn-unreachable |
| 1404 |
from typing import Union |
| 1405 |
|
| 1406 |
def f(x: Union[int, str]) -> None: |
| 1407 |
if not isinstance(x, int): |
| 1408 |
return foo() |
| 1409 |
x() # E: "int" not callable |
| 1410 |
|
| 1411 |
def foo(): pass |
| 1412 |
[builtins fixtures/isinstancelist.pyi] |
| 1413 |
|
| 1414 |
[case testIsinstanceOr1] |
| 1415 |
# flags: --warn-unreachable |
| 1416 |
from typing import Optional |
| 1417 |
|
| 1418 |
def f(a: bool, x: object) -> Optional[int]: |
| 1419 |
if a or not isinstance(x, int): |
| 1420 |
return None |
| 1421 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 1422 |
return x |
| 1423 |
[builtins fixtures/isinstance.pyi] |
| 1424 |
|
| 1425 |
[case testIsinstanceOr2] |
| 1426 |
# flags: --warn-unreachable |
| 1427 |
from typing import Optional |
| 1428 |
|
| 1429 |
def g(a: bool, x: object) -> Optional[int]: |
| 1430 |
if not isinstance(x, int) or a: |
| 1431 |
return None |
| 1432 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 1433 |
return x |
| 1434 |
[builtins fixtures/isinstance.pyi] |
| 1435 |
|
| 1436 |
[case testIsinstanceOr3] |
| 1437 |
# flags: --warn-unreachable |
| 1438 |
from typing import Optional |
| 1439 |
|
| 1440 |
def h(a: bool, x: object) -> Optional[int]: |
| 1441 |
if a or isinstance(x, int): |
| 1442 |
return None |
| 1443 |
return x # E: Incompatible return value type (got "object", expected "int | None") |
| 1444 |
[builtins fixtures/isinstance.pyi] |
| 1445 |
|
| 1446 |
[case testIsinstanceWithOverlappingUnionType] |
| 1447 |
# flags: --warn-unreachable |
| 1448 |
from typing import Union |
| 1449 |
|
| 1450 |
def f(x: Union[float, int]) -> None: |
| 1451 |
if isinstance(x, float): |
| 1452 |
pass |
| 1453 |
if not isinstance(x, int): |
| 1454 |
f(x) |
| 1455 |
[builtins fixtures/isinstance.pyi] |
| 1456 |
|
| 1457 |
[case testIsinstanceWithOverlappingUnionType2] |
| 1458 |
# flags: --warn-unreachable |
| 1459 |
from typing import Union |
| 1460 |
|
| 1461 |
class A: pass |
| 1462 |
class B(A): pass |
| 1463 |
|
| 1464 |
def f(x: Union[A, B]) -> None: |
| 1465 |
if isinstance(x, A): |
| 1466 |
pass |
| 1467 |
if not isinstance(x, B): |
| 1468 |
f(x) |
| 1469 |
[builtins fixtures/isinstance.pyi] |
| 1470 |
|
| 1471 |
[case testIsinstanceWithOverlappingPromotionTypes] |
| 1472 |
# flags: --warn-unreachable |
| 1473 |
from typing import Union |
| 1474 |
|
| 1475 |
def f1(x: Union[float, int]) -> None: |
| 1476 |
# We ignore promotions in isinstance checks |
| 1477 |
if isinstance(x, float): |
| 1478 |
reveal_type(x) # N: Revealed type is "builtins.float" |
| 1479 |
else: |
| 1480 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 1481 |
|
| 1482 |
class FloatLike: pass |
| 1483 |
class IntLike(FloatLike): pass |
| 1484 |
|
| 1485 |
def f2(x: Union[FloatLike, IntLike]) -> None: |
| 1486 |
if isinstance(x, FloatLike): |
| 1487 |
reveal_type(x) # N: Revealed type is "__main__.FloatLike" |
| 1488 |
else: |
| 1489 |
reveal_type(x) # E: Statement is unreachable |
| 1490 |
[builtins fixtures/isinstance.pyi] |
| 1491 |
|
| 1492 |
[case testIsinstanceOfSuperclass] |
| 1493 |
# flags: --warn-unreachable |
| 1494 |
class A: pass |
| 1495 |
class B(A): pass |
| 1496 |
|
| 1497 |
x = B() |
| 1498 |
if isinstance(x, A): |
| 1499 |
reveal_type(x) # N: Revealed type is "__main__.B" |
| 1500 |
if not isinstance(x, A): |
| 1501 |
reveal_type(x) # E: Statement is unreachable |
| 1502 |
x = A() |
| 1503 |
reveal_type(x) # N: Revealed type is "__main__.B" |
| 1504 |
[builtins fixtures/isinstance.pyi] |
| 1505 |
|
| 1506 |
[case testIsinstanceOfNonoverlapping] |
| 1507 |
# flags: --warn-unreachable |
| 1508 |
class A: pass |
| 1509 |
class B: pass |
| 1510 |
|
| 1511 |
x = B() |
| 1512 |
if isinstance(x, A): |
| 1513 |
reveal_type(x) # N: Revealed type is "__main__.<subclass of "__main__.B" and "__main__.A">" |
| 1514 |
else: |
| 1515 |
reveal_type(x) # N: Revealed type is "__main__.B" |
| 1516 |
reveal_type(x) # N: Revealed type is "__main__.B" |
| 1517 |
[builtins fixtures/isinstance.pyi] |
| 1518 |
|
| 1519 |
[case testAssertIsinstance] |
| 1520 |
# flags: --warn-unreachable |
| 1521 |
def f(x: object): |
| 1522 |
assert isinstance(x, int) |
| 1523 |
y = 0 # type: int |
| 1524 |
y = x |
| 1525 |
[builtins fixtures/isinstance.pyi] |
| 1526 |
|
| 1527 |
[case testUnionAssertIsinstance] |
| 1528 |
# flags: --warn-unreachable |
| 1529 |
from typing import Union |
| 1530 |
|
| 1531 |
def f(x: Union[str, int]): |
| 1532 |
assert isinstance(x, int) |
| 1533 |
y = 0 # type: int |
| 1534 |
y = x |
| 1535 |
[builtins fixtures/isinstance.pyi] |
| 1536 |
|
| 1537 |
[case testAnyAssertIsinstance] |
| 1538 |
# flags: --warn-unreachable |
| 1539 |
from typing import Any |
| 1540 |
|
| 1541 |
def f(x: Any): |
| 1542 |
assert isinstance(x, int) # this should narrow x to type int |
| 1543 |
x + "foo" # E: Unsupported operand types for + ("int" and "str") |
| 1544 |
[builtins fixtures/isinstance.pyi] |
| 1545 |
|
| 1546 |
[case testIsinstanceNarrowingAny] |
| 1547 |
# flags: --warn-unreachable |
| 1548 |
from typing import Any |
| 1549 |
|
| 1550 |
class A: pass |
| 1551 |
class B(A): pass |
| 1552 |
|
| 1553 |
def f1(x: Any) -> None: |
| 1554 |
if x and isinstance(x, A): |
| 1555 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 1556 |
if isinstance(x, A) and x: |
| 1557 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 1558 |
|
| 1559 |
def f2(x: Any) -> None: |
| 1560 |
if isinstance(x, A) and not isinstance(x, B): |
| 1561 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 1562 |
if not isinstance(x, B) and isinstance(x, A): |
| 1563 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 1564 |
[builtins fixtures/isinstance.pyi] |
| 1565 |
|
| 1566 |
[case testIsinstanceOfGenericClassRetainsParameters] |
| 1567 |
# flags: --warn-unreachable |
| 1568 |
from typing import List, Union |
| 1569 |
|
| 1570 |
def f(x: Union[List[int], str]) -> None: |
| 1571 |
if isinstance(x, list): |
| 1572 |
x[0]() # E: "int" not callable |
| 1573 |
else: |
| 1574 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 1575 |
reveal_type(x) # N: Revealed type is "builtins.list[builtins.int] | builtins.str" |
| 1576 |
[builtins fixtures/isinstancelist.pyi] |
| 1577 |
|
| 1578 |
[case testIsinstanceOrIsinstance] |
| 1579 |
# flags: --warn-unreachable |
| 1580 |
class A: pass |
| 1581 |
|
| 1582 |
class B(A): |
| 1583 |
flag = 1 |
| 1584 |
|
| 1585 |
class C(A): |
| 1586 |
flag = 2 |
| 1587 |
|
| 1588 |
x1 = A() |
| 1589 |
if isinstance(x1, B) or isinstance(x1, C): |
| 1590 |
reveal_type(x1) # N: Revealed type is "__main__.B | __main__.C" |
| 1591 |
f = x1.flag # type: int |
| 1592 |
else: |
| 1593 |
reveal_type(x1) # N: Revealed type is "__main__.A" |
| 1594 |
f = 0 |
| 1595 |
reveal_type(x1) # N: Revealed type is "__main__.A" |
| 1596 |
x2 = A() |
| 1597 |
if isinstance(x2, A) or isinstance(x2, C): # E: Right operand of "or" is never evaluated |
| 1598 |
reveal_type(x2) # N: Revealed type is "__main__.A" |
| 1599 |
f = x2.flag # E: "A" has no attribute "flag" |
| 1600 |
else: |
| 1601 |
1() # E: Statement is unreachable |
| 1602 |
reveal_type(x2) # N: Revealed type is "__main__.A" |
| 1603 |
[builtins fixtures/isinstance.pyi] |
| 1604 |
|
| 1605 |
[case testComprehensionIsInstance] |
| 1606 |
# flags: --warn-unreachable |
| 1607 |
from typing import List, Union |
| 1608 |
a = [] # type: List[Union[int, str]] |
| 1609 |
l = [x for x in a if isinstance(x, int)] |
| 1610 |
g = (x for x in a if isinstance(x, int)) |
| 1611 |
d = {0: x for x in a if isinstance(x, int)} |
| 1612 |
reveal_type(l) # N: Revealed type is "builtins.list[builtins.int]" |
| 1613 |
reveal_type(g) # N: Revealed type is "typing.Generator[builtins.int, None, None]" |
| 1614 |
reveal_type(d) # N: Revealed type is "builtins.dict[builtins.int, builtins.int]" |
| 1615 |
[builtins fixtures/isinstancelist.pyi] |
| 1616 |
|
| 1617 |
[case testIsinstanceInWrongOrderInBooleanOp] |
| 1618 |
# flags: --warn-unreachable |
| 1619 |
class A: |
| 1620 |
m = 1 |
| 1621 |
|
| 1622 |
def f(x: object) -> None: |
| 1623 |
if x.m and isinstance(x, A) or False: # E: "object" has no attribute "m" |
| 1624 |
pass |
| 1625 |
[builtins fixtures/isinstance.pyi] |
| 1626 |
|
| 1627 |
[case testIsinstanceAndOr] |
| 1628 |
# flags: --warn-unreachable |
| 1629 |
class A: |
| 1630 |
a: 'A' |
| 1631 |
|
| 1632 |
def f(x: object) -> None: |
| 1633 |
b = isinstance(x, A) and x.a or A() |
| 1634 |
reveal_type(b) # N: Revealed type is "__main__.A" |
| 1635 |
[builtins fixtures/isinstance.pyi] |
| 1636 |
|
| 1637 |
[case testIsInstanceWithUnknownType] |
| 1638 |
# flags: --warn-unreachable |
| 1639 |
from typing import Union |
| 1640 |
|
| 1641 |
def f(x: Union[int, str], typ: type) -> None: |
| 1642 |
if isinstance(x, (typ, int)): |
| 1643 |
x + 1 # E: Unsupported operand types for + ("str" and "int") \ |
| 1644 |
# N: Left operand is of type "int | str" |
| 1645 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 1646 |
else: |
| 1647 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 1648 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 1649 |
[builtins fixtures/isinstancelist.pyi] |
| 1650 |
|
| 1651 |
[case testIsInstanceWithUnknownTypeMultipleNarrowing] |
| 1652 |
# flags: --strict-equality --warn-unreachable --python-version 3.10 |
| 1653 |
from __future__ import annotations |
| 1654 |
from typing import Iterable |
| 1655 |
from typing_extensions import TypeAlias |
| 1656 |
import types |
| 1657 |
|
| 1658 |
# Regression test for https://github.com/python/mypy/issues/21181 |
| 1659 |
# We don't have the same type context as with the real stubs, so sort of fake it |
| 1660 |
_ClassInfoLike: TypeAlias = "type | tuple[_ClassInfoLike, ...]" |
| 1661 |
|
| 1662 |
class A: ... |
| 1663 |
class B(A): ... |
| 1664 |
|
| 1665 |
def fake_type_context(ts: list[type[A]]) -> _ClassInfoLike: |
| 1666 |
return tuple(ts) # E: Too many arguments for "tuple" |
| 1667 |
|
| 1668 |
|
| 1669 |
def f1(x: A | None) -> None: |
| 1670 |
if x is not None: |
| 1671 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 1672 |
if isinstance(x, object): |
| 1673 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 1674 |
|
| 1675 |
|
| 1676 |
def f2(x: A | None, ts: list[type[A]]) -> None: |
| 1677 |
if x is not None: |
| 1678 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 1679 |
if isinstance(x, fake_type_context(ts)): |
| 1680 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 1681 |
|
| 1682 |
|
| 1683 |
def f3(x: A | None, t: type | type[A]) -> None: |
| 1684 |
if x is not None: |
| 1685 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 1686 |
if isinstance(x, t): |
| 1687 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 1688 |
|
| 1689 |
|
| 1690 |
def f4(x: A | None, t: type) -> None: |
| 1691 |
if x is not None: |
| 1692 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 1693 |
if isinstance(x, t): |
| 1694 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 1695 |
|
| 1696 |
|
| 1697 |
def f5(x: object | None, ta: type[A], tb: type[B]) -> None: |
| 1698 |
if x is not None: |
| 1699 |
reveal_type(x) # N: Revealed type is "builtins.object" |
| 1700 |
if isinstance(x, ta): |
| 1701 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 1702 |
if isinstance(x, tb): |
| 1703 |
reveal_type(x) # N: Revealed type is "__main__.B" |
| 1704 |
[builtins fixtures/isinstancelist.pyi] |
| 1705 |
|
| 1706 |
[case testIsInstanceWithBoundedType] |
| 1707 |
# flags: --warn-unreachable |
| 1708 |
from typing import Union, Type |
| 1709 |
|
| 1710 |
class A: pass |
| 1711 |
|
| 1712 |
def f(x: Union[int, A], a: Type[A]) -> None: |
| 1713 |
if isinstance(x, (a, int)): |
| 1714 |
reveal_type(x) # N: Revealed type is "builtins.int | __main__.A" |
| 1715 |
else: |
| 1716 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 1717 |
reveal_type(x) # N: Revealed type is "builtins.int | __main__.A" |
| 1718 |
[builtins fixtures/isinstancelist.pyi] |
| 1719 |
|
| 1720 |
[case testIsInstanceWithEmtpy2ndArg] |
| 1721 |
# flags: --warn-unreachable |
| 1722 |
from typing import Union |
| 1723 |
|
| 1724 |
def f(x: Union[int, str]) -> None: |
| 1725 |
if isinstance(x, ()): |
| 1726 |
reveal_type(x) # E: Statement is unreachable |
| 1727 |
else: |
| 1728 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 1729 |
[builtins fixtures/isinstancelist.pyi] |
| 1730 |
|
| 1731 |
[case testIsInstanceWithTypeObject] |
| 1732 |
# flags: --warn-unreachable |
| 1733 |
from typing import Union, Type |
| 1734 |
|
| 1735 |
class A: pass |
| 1736 |
|
| 1737 |
def f(x: Union[int, A], a: Type[A]) -> None: |
| 1738 |
if isinstance(x, a): |
| 1739 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 1740 |
elif isinstance(x, int): |
| 1741 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 1742 |
else: |
| 1743 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 1744 |
reveal_type(x) # N: Revealed type is "builtins.int | __main__.A" |
| 1745 |
[builtins fixtures/isinstancelist.pyi] |
| 1746 |
|
| 1747 |
[case testIssubclassUnreachable] |
| 1748 |
# flags: --warn-unreachable |
| 1749 |
from typing import Type, Sequence, Union |
| 1750 |
|
| 1751 |
x: Type[str] |
| 1752 |
if issubclass(x, int): # E: Subclass of "str" and "int" cannot exist: would have incompatible method signatures |
| 1753 |
reveal_type(x) # E: Statement is unreachable |
| 1754 |
|
| 1755 |
class X: pass |
| 1756 |
class Y(X): pass |
| 1757 |
class Z(X): pass |
| 1758 |
|
| 1759 |
a: Union[Type[Y], Type[Z]] |
| 1760 |
if issubclass(a, X): |
| 1761 |
reveal_type(a) # N: Revealed type is "type[__main__.Y] | type[__main__.Z]" |
| 1762 |
else: |
| 1763 |
reveal_type(a) # E: Statement is unreachable |
| 1764 |
[builtins fixtures/isinstancelist.pyi] |
| 1765 |
|
| 1766 |
[case testIssubclasDestructuringUnions1] |
| 1767 |
# flags: --warn-unreachable |
| 1768 |
from typing import Union, List, Tuple, Dict, Type |
| 1769 |
def f(x: Union[Type[int], Type[str], Type[List]]) -> None: |
| 1770 |
if issubclass(x, (str, (int,))): |
| 1771 |
reveal_type(x) # N: Revealed type is "type[builtins.int] | type[builtins.str]" |
| 1772 |
reveal_type(x()) # N: Revealed type is "builtins.int | builtins.str" |
| 1773 |
x()[1] # E: Value of type "int | str" is not indexable |
| 1774 |
else: |
| 1775 |
reveal_type(x) # N: Revealed type is "type[builtins.list[Any]]" |
| 1776 |
reveal_type(x()) # N: Revealed type is "builtins.list[Any]" |
| 1777 |
x()[1] |
| 1778 |
reveal_type(x) # N: Revealed type is "type[builtins.int] | type[builtins.str] | type[builtins.list[Any]]" |
| 1779 |
reveal_type(x()) # N: Revealed type is "builtins.int | builtins.str | builtins.list[Any]" |
| 1780 |
if issubclass(x, (str, (list,))): |
| 1781 |
reveal_type(x) # N: Revealed type is "type[builtins.str] | type[builtins.list[Any]]" |
| 1782 |
reveal_type(x()) # N: Revealed type is "builtins.str | builtins.list[Any]" |
| 1783 |
x()[1] |
| 1784 |
reveal_type(x) # N: Revealed type is "type[builtins.int] | type[builtins.str] | type[builtins.list[Any]]" |
| 1785 |
reveal_type(x()) # N: Revealed type is "builtins.int | builtins.str | builtins.list[Any]" |
| 1786 |
[builtins fixtures/isinstancelist.pyi] |
| 1787 |
|
| 1788 |
[case testIssubclasDestructuringUnions2] |
| 1789 |
# flags: --warn-unreachable |
| 1790 |
from typing import Union, List, Tuple, Dict, Type |
| 1791 |
|
| 1792 |
def f(x: Type[Union[int, str, List]]) -> None: |
| 1793 |
if issubclass(x, (str, (int,))): |
| 1794 |
reveal_type(x) # N: Revealed type is "type[builtins.int] | type[builtins.str]" |
| 1795 |
reveal_type(x()) # N: Revealed type is "builtins.int | builtins.str" |
| 1796 |
x()[1] # E: Value of type "int | str" is not indexable |
| 1797 |
else: |
| 1798 |
reveal_type(x) # N: Revealed type is "type[builtins.list[Any]]" |
| 1799 |
reveal_type(x()) # N: Revealed type is "builtins.list[Any]" |
| 1800 |
x()[1] |
| 1801 |
reveal_type(x) # N: Revealed type is "type[builtins.int] | type[builtins.str] | type[builtins.list[Any]]" |
| 1802 |
reveal_type(x()) # N: Revealed type is "builtins.int | builtins.str | builtins.list[Any]" |
| 1803 |
if issubclass(x, (str, (list,))): |
| 1804 |
reveal_type(x) # N: Revealed type is "type[builtins.str] | type[builtins.list[Any]]" |
| 1805 |
reveal_type(x()) # N: Revealed type is "builtins.str | builtins.list[Any]" |
| 1806 |
x()[1] |
| 1807 |
reveal_type(x) # N: Revealed type is "type[builtins.int] | type[builtins.str] | type[builtins.list[Any]]" |
| 1808 |
reveal_type(x()) # N: Revealed type is "builtins.int | builtins.str | builtins.list[Any]" |
| 1809 |
[builtins fixtures/isinstancelist.pyi] |
| 1810 |
|
| 1811 |
[case testIssubclasDestructuringUnions3] |
| 1812 |
# flags: --warn-unreachable |
| 1813 |
from typing import Union, List, Tuple, Dict, Type |
| 1814 |
|
| 1815 |
def f(x: Type[Union[int, str, List]]) -> None: |
| 1816 |
reveal_type(x) # N: Revealed type is "type[builtins.int] | type[builtins.str] | type[builtins.list[Any]]" |
| 1817 |
reveal_type(x()) # N: Revealed type is "builtins.int | builtins.str | builtins.list[Any]" |
| 1818 |
if issubclass(x, (str, (int,))): |
| 1819 |
reveal_type(x) # N: Revealed type is "type[builtins.int] | type[builtins.str]" |
| 1820 |
reveal_type(x()) # N: Revealed type is "builtins.int | builtins.str" |
| 1821 |
x()[1] # E: Value of type "int | str" is not indexable |
| 1822 |
else: |
| 1823 |
reveal_type(x) # N: Revealed type is "type[builtins.list[Any]]" |
| 1824 |
reveal_type(x()) # N: Revealed type is "builtins.list[Any]" |
| 1825 |
x()[1] |
| 1826 |
reveal_type(x) # N: Revealed type is "type[builtins.int] | type[builtins.str] | type[builtins.list[Any]]" |
| 1827 |
reveal_type(x()) # N: Revealed type is "builtins.int | builtins.str | builtins.list[Any]" |
| 1828 |
if issubclass(x, (str, (list,))): |
| 1829 |
reveal_type(x) # N: Revealed type is "type[builtins.str] | type[builtins.list[Any]]" |
| 1830 |
reveal_type(x()) # N: Revealed type is "builtins.str | builtins.list[Any]" |
| 1831 |
x()[1] |
| 1832 |
reveal_type(x) # N: Revealed type is "type[builtins.int] | type[builtins.str] | type[builtins.list[Any]]" |
| 1833 |
reveal_type(x()) # N: Revealed type is "builtins.int | builtins.str | builtins.list[Any]" |
| 1834 |
[builtins fixtures/isinstancelist.pyi] |
| 1835 |
|
| 1836 |
[case testIssubclass] |
| 1837 |
# flags: --warn-unreachable |
| 1838 |
from typing import Type, ClassVar |
| 1839 |
|
| 1840 |
class Goblin: |
| 1841 |
level: int |
| 1842 |
|
| 1843 |
class GoblinAmbusher(Goblin): |
| 1844 |
job: ClassVar[str] = 'Ranger' |
| 1845 |
|
| 1846 |
def test_issubclass(cls: Type[Goblin]) -> None: |
| 1847 |
if issubclass(cls, GoblinAmbusher): |
| 1848 |
reveal_type(cls) # N: Revealed type is "type[__main__.GoblinAmbusher]" |
| 1849 |
cls.level |
| 1850 |
cls.job |
| 1851 |
ga = cls() |
| 1852 |
ga.level = 15 |
| 1853 |
ga.job |
| 1854 |
ga.job = "Warrior" # E: Cannot assign to class variable "job" via instance |
| 1855 |
else: |
| 1856 |
reveal_type(cls) # N: Revealed type is "type[__main__.Goblin]" |
| 1857 |
cls.level |
| 1858 |
cls.job # E: "type[Goblin]" has no attribute "job" |
| 1859 |
g = cls() |
| 1860 |
g.level = 15 |
| 1861 |
g.job # E: "Goblin" has no attribute "job" |
| 1862 |
[builtins fixtures/isinstancelist.pyi] |
| 1863 |
|
| 1864 |
[case testIssubclassDeepHierarchy] |
| 1865 |
# flags: --warn-unreachable |
| 1866 |
from typing import Type, ClassVar |
| 1867 |
|
| 1868 |
class Mob: pass |
| 1869 |
|
| 1870 |
class Goblin(Mob): |
| 1871 |
level: int |
| 1872 |
|
| 1873 |
class GoblinAmbusher(Goblin): |
| 1874 |
job: ClassVar[str] = 'Ranger' |
| 1875 |
|
| 1876 |
def test_issubclass(cls: Type[Mob]) -> None: |
| 1877 |
if issubclass(cls, Goblin): |
| 1878 |
reveal_type(cls) # N: Revealed type is "type[__main__.Goblin]" |
| 1879 |
cls.level |
| 1880 |
cls.job # E: "type[Goblin]" has no attribute "job" |
| 1881 |
g = cls() |
| 1882 |
g.level = 15 |
| 1883 |
g.job # E: "Goblin" has no attribute "job" |
| 1884 |
if issubclass(cls, GoblinAmbusher): |
| 1885 |
reveal_type(cls) # N: Revealed type is "type[__main__.GoblinAmbusher]" |
| 1886 |
cls.level |
| 1887 |
cls.job |
| 1888 |
g = cls() |
| 1889 |
g.level = 15 |
| 1890 |
g.job |
| 1891 |
g.job = 'Warrior' # E: Cannot assign to class variable "job" via instance |
| 1892 |
else: |
| 1893 |
reveal_type(cls) # N: Revealed type is "type[__main__.Mob]" |
| 1894 |
cls.job # E: "type[Mob]" has no attribute "job" |
| 1895 |
cls.level # E: "type[Mob]" has no attribute "level" |
| 1896 |
m = cls() |
| 1897 |
m.level = 15 # E: "Mob" has no attribute "level" |
| 1898 |
m.job # E: "Mob" has no attribute "job" |
| 1899 |
if issubclass(cls, GoblinAmbusher): |
| 1900 |
reveal_type(cls) # N: Revealed type is "type[__main__.GoblinAmbusher]" |
| 1901 |
cls.job |
| 1902 |
cls.level |
| 1903 |
ga = cls() |
| 1904 |
ga.level = 15 |
| 1905 |
ga.job |
| 1906 |
ga.job = 'Warrior' # E: Cannot assign to class variable "job" via instance |
| 1907 |
|
| 1908 |
if issubclass(cls, GoblinAmbusher): |
| 1909 |
reveal_type(cls) # N: Revealed type is "type[__main__.GoblinAmbusher]" |
| 1910 |
cls.level |
| 1911 |
cls.job |
| 1912 |
ga = cls() |
| 1913 |
ga.level = 15 |
| 1914 |
ga.job |
| 1915 |
ga.job = "Warrior" # E: Cannot assign to class variable "job" via instance |
| 1916 |
[builtins fixtures/isinstancelist.pyi] |
| 1917 |
|
| 1918 |
[case testIssubclassTuple] |
| 1919 |
# flags: --warn-unreachable |
| 1920 |
from typing import Type, ClassVar |
| 1921 |
|
| 1922 |
class Mob: pass |
| 1923 |
|
| 1924 |
class Goblin(Mob): |
| 1925 |
level: int |
| 1926 |
|
| 1927 |
class GoblinAmbusher(Goblin): |
| 1928 |
job: ClassVar[str] = 'Ranger' |
| 1929 |
|
| 1930 |
class GoblinDigger(Goblin): |
| 1931 |
job: ClassVar[str] = 'Thief' |
| 1932 |
|
| 1933 |
def test_issubclass(cls: Type[Mob]) -> None: |
| 1934 |
if issubclass(cls, (Goblin, GoblinAmbusher)): |
| 1935 |
reveal_type(cls) # N: Revealed type is "type[__main__.Goblin]" |
| 1936 |
cls.level |
| 1937 |
cls.job # E: "type[Goblin]" has no attribute "job" |
| 1938 |
g = cls() |
| 1939 |
g.level = 15 |
| 1940 |
g.job # E: "Goblin" has no attribute "job" |
| 1941 |
if issubclass(cls, GoblinAmbusher): |
| 1942 |
cls.level |
| 1943 |
reveal_type(cls) # N: Revealed type is "type[__main__.GoblinAmbusher]" |
| 1944 |
cls.job |
| 1945 |
ga = cls() |
| 1946 |
ga.level = 15 |
| 1947 |
ga.job |
| 1948 |
ga.job = "Warrior" # E: Cannot assign to class variable "job" via instance |
| 1949 |
else: |
| 1950 |
reveal_type(cls) # N: Revealed type is "type[__main__.Mob]" |
| 1951 |
cls.job # E: "type[Mob]" has no attribute "job" |
| 1952 |
cls.level # E: "type[Mob]" has no attribute "level" |
| 1953 |
m = cls() |
| 1954 |
m.level = 15 # E: "Mob" has no attribute "level" |
| 1955 |
m.job # E: "Mob" has no attribute "job" |
| 1956 |
if issubclass(cls, GoblinAmbusher): |
| 1957 |
reveal_type(cls) # N: Revealed type is "type[__main__.GoblinAmbusher]" |
| 1958 |
cls.job |
| 1959 |
cls.level |
| 1960 |
ga = cls() |
| 1961 |
ga.level = 15 |
| 1962 |
ga.job |
| 1963 |
ga.job = "Warrior" # E: Cannot assign to class variable "job" via instance |
| 1964 |
|
| 1965 |
if issubclass(cls, (GoblinDigger, GoblinAmbusher)): |
| 1966 |
reveal_type(cls) # N: Revealed type is "type[__main__.GoblinDigger] | type[__main__.GoblinAmbusher]" |
| 1967 |
cls.level |
| 1968 |
cls.job |
| 1969 |
g = cls() |
| 1970 |
g.level = 15 |
| 1971 |
g.job |
| 1972 |
g.job = "Warrior" # E: Cannot assign to class variable "job" via instance |
| 1973 |
[builtins fixtures/isinstancelist.pyi] |
| 1974 |
|
| 1975 |
[case testIssubclassBuiltins] |
| 1976 |
# flags: --warn-unreachable |
| 1977 |
from typing import List, Type |
| 1978 |
|
| 1979 |
class MyList(List): pass |
| 1980 |
class MyIntList(List[int]): pass |
| 1981 |
|
| 1982 |
def f(cls: Type[object]) -> None: |
| 1983 |
if issubclass(cls, MyList): |
| 1984 |
reveal_type(cls) # N: Revealed type is "type[__main__.MyList]" |
| 1985 |
cls()[0] |
| 1986 |
else: |
| 1987 |
reveal_type(cls) # N: Revealed type is "type[builtins.object]" |
| 1988 |
cls()[0] # E: Value of type "object" is not indexable |
| 1989 |
|
| 1990 |
if issubclass(cls, MyIntList): |
| 1991 |
reveal_type(cls) # N: Revealed type is "type[__main__.MyIntList]" |
| 1992 |
cls()[0] + 1 |
| 1993 |
[builtins fixtures/isinstancelist.pyi] |
| 1994 |
|
| 1995 |
[case testIssubclassTypeVar] |
| 1996 |
# flags: --strict-equality --warn-unreachable |
| 1997 |
from __future__ import annotations |
| 1998 |
from typing import TypeVar |
| 1999 |
|
| 2000 |
ClassT = TypeVar("ClassT", bound=type) |
| 2001 |
|
| 2002 |
def directed_meet(cls0: ClassT, cls1: ClassT) -> ClassT | None: |
| 2003 |
if issubclass(cls1, cls0): |
| 2004 |
return cls1 |
| 2005 |
if issubclass(cls0, cls1): |
| 2006 |
return cls0 |
| 2007 |
return None |
| 2008 |
[builtins fixtures/isinstancelist.pyi] |
| 2009 |
|
| 2010 |
[case testIsinstanceTypeArgs] |
| 2011 |
# flags: --warn-unreachable |
| 2012 |
from typing import Iterable, TypeVar |
| 2013 |
x = 1 |
| 2014 |
isinstance(x, Iterable) |
| 2015 |
isinstance(x, Iterable[int]) # E: Parameterized generics cannot be used with class or instance checks |
| 2016 |
isinstance(x, (int, Iterable[int])) # E: Parameterized generics cannot be used with class or instance checks |
| 2017 |
isinstance(x, (int, (str, Iterable[int]))) # E: Parameterized generics cannot be used with class or instance checks |
| 2018 |
[builtins fixtures/isinstancelist.pyi] |
| 2019 |
[typing fixtures/typing-full.pyi] |
| 2020 |
|
| 2021 |
[case testIsinstanceAnyAlias] |
| 2022 |
# flags: --warn-unreachable |
| 2023 |
from typing import Any |
| 2024 |
A = Any |
| 2025 |
isinstance(object(), A) # E: Cannot use isinstance() with Any type |
| 2026 |
[builtins fixtures/isinstance.pyi] |
| 2027 |
|
| 2028 |
[case testIsinstanceTypeArgsAliases] |
| 2029 |
# flags: --warn-unreachable |
| 2030 |
from typing import Iterable, TypeVar |
| 2031 |
x = 1 |
| 2032 |
T = TypeVar('T') |
| 2033 |
It = Iterable |
| 2034 |
It2 = Iterable[T] |
| 2035 |
|
| 2036 |
isinstance(x, It[int]) # E: Parameterized generics cannot be used with class or instance checks |
| 2037 |
isinstance(x, It) |
| 2038 |
isinstance(x, It2[int]) # E: Parameterized generics cannot be used with class or instance checks |
| 2039 |
isinstance(x, It2) # E: Parameterized generics cannot be used with class or instance checks |
| 2040 |
[builtins fixtures/isinstance.pyi] |
| 2041 |
[typing fixtures/typing-full.pyi] |
| 2042 |
|
| 2043 |
[case testIssubclassTypeArgs] |
| 2044 |
# flags: --warn-unreachable |
| 2045 |
from typing import Iterable, TypeVar |
| 2046 |
x = int |
| 2047 |
issubclass(x, Iterable) |
| 2048 |
issubclass(x, Iterable[int]) # E: Parameterized generics cannot be used with class or instance checks |
| 2049 |
issubclass(x, (int, Iterable[int])) # E: Parameterized generics cannot be used with class or instance checks |
| 2050 |
[builtins fixtures/isinstance.pyi] |
| 2051 |
[typing fixtures/typing-full.pyi] |
| 2052 |
|
| 2053 |
[case testIssubclassWithMetaclasses] |
| 2054 |
# flags: --no-strict-optional --warn-unreachable |
| 2055 |
class FooMetaclass(type): ... |
| 2056 |
class Foo(metaclass=FooMetaclass): ... |
| 2057 |
class Bar: ... |
| 2058 |
|
| 2059 |
fm: FooMetaclass |
| 2060 |
reveal_type(fm) # N: Revealed type is "__main__.FooMetaclass" |
| 2061 |
if issubclass(fm, Foo): |
| 2062 |
reveal_type(fm) # N: Revealed type is "type[__main__.Foo]" |
| 2063 |
if issubclass(fm, Bar): |
| 2064 |
reveal_type(fm) # N: Revealed type is "None" |
| 2065 |
[builtins fixtures/isinstance.pyi] |
| 2066 |
|
| 2067 |
[case testIssubclassWithMetaclassesStrictOptional] |
| 2068 |
# flags: --warn-unreachable |
| 2069 |
class FooMetaclass(type): ... |
| 2070 |
class BarMetaclass(type): ... |
| 2071 |
class Foo(metaclass=FooMetaclass): ... |
| 2072 |
class Bar(metaclass=BarMetaclass): ... |
| 2073 |
class Baz: ... |
| 2074 |
|
| 2075 |
fm: FooMetaclass |
| 2076 |
reveal_type(fm) # N: Revealed type is "__main__.FooMetaclass" |
| 2077 |
if issubclass(fm, Foo): |
| 2078 |
reveal_type(fm) # N: Revealed type is "type[__main__.Foo]" |
| 2079 |
if issubclass(fm, Bar): |
| 2080 |
reveal_type(fm) # N: Revealed type is "type[__main__.Bar]" |
| 2081 |
if issubclass(fm, Baz): |
| 2082 |
reveal_type(fm) # N: Revealed type is "type[__main__.Baz]" |
| 2083 |
[builtins fixtures/isinstance.pyi] |
| 2084 |
|
| 2085 |
[case testIsinstanceAndNarrowTypeVariable] |
| 2086 |
# flags: --warn-unreachable |
| 2087 |
from typing import TypeVar |
| 2088 |
|
| 2089 |
class A: pass |
| 2090 |
class B(A): |
| 2091 |
attr: int |
| 2092 |
|
| 2093 |
T = TypeVar('T', bound=A) |
| 2094 |
|
| 2095 |
def f(x: T) -> None: |
| 2096 |
if isinstance(x, B): |
| 2097 |
reveal_type(x) # N: Revealed type is "T`-1" |
| 2098 |
reveal_type(x.attr) # N: Revealed type is "builtins.int" |
| 2099 |
else: |
| 2100 |
reveal_type(x) # N: Revealed type is "T`-1" |
| 2101 |
x.attr # E: "T" has no attribute "attr" |
| 2102 |
reveal_type(x) # N: Revealed type is "T`-1" |
| 2103 |
x.attr # E: "T" has no attribute "attr" |
| 2104 |
[builtins fixtures/isinstance.pyi] |
| 2105 |
|
| 2106 |
[case testIsinstanceAndNegativeNarrowTypeVariableWithUnionBound1] |
| 2107 |
# flags: --warn-unreachable |
| 2108 |
from typing import Union, TypeVar |
| 2109 |
|
| 2110 |
class A: |
| 2111 |
a: int |
| 2112 |
class B: |
| 2113 |
b: int |
| 2114 |
|
| 2115 |
T = TypeVar("T", bound=Union[A, B]) |
| 2116 |
|
| 2117 |
def f(x: T) -> T: |
| 2118 |
if isinstance(x, A): |
| 2119 |
reveal_type(x) # N: Revealed type is "T`-1" |
| 2120 |
x.a |
| 2121 |
x.b # E: "T" has no attribute "b" |
| 2122 |
if bool(): |
| 2123 |
return x |
| 2124 |
else: |
| 2125 |
reveal_type(x) # N: Revealed type is "T`-1" |
| 2126 |
x.a # E: "T" has no attribute "a" |
| 2127 |
x.b |
| 2128 |
x.a # E: Item "B" of the upper bound "A | B" of type variable "T" has no attribute "a" |
| 2129 |
x.b # E: Item "A" of the upper bound "A | B" of type variable "T" has no attribute "b" |
| 2130 |
return x |
| 2131 |
[builtins fixtures/isinstance.pyi] |
| 2132 |
|
| 2133 |
[case testIsinstanceAndNegativeNarrowTypeVariableWithUnionBound2] |
| 2134 |
# flags: --warn-unreachable |
| 2135 |
from typing import Union, TypeVar |
| 2136 |
|
| 2137 |
class A: |
| 2138 |
a: int |
| 2139 |
class B: |
| 2140 |
b: int |
| 2141 |
|
| 2142 |
T = TypeVar("T", bound=Union[A, B]) |
| 2143 |
|
| 2144 |
def f(x: T) -> T: |
| 2145 |
if isinstance(x, A): |
| 2146 |
return x |
| 2147 |
x.a # E: "T" has no attribute "a" |
| 2148 |
x.b # OK |
| 2149 |
return x |
| 2150 |
[builtins fixtures/isinstance.pyi] |
| 2151 |
|
| 2152 |
[case testIsinstanceAndTypeType] |
| 2153 |
# flags: --warn-unreachable |
| 2154 |
from typing import Type |
| 2155 |
def f(x: Type[int]) -> None: |
| 2156 |
if isinstance(x, type): |
| 2157 |
reveal_type(x) # N: Revealed type is "type[builtins.int]" |
| 2158 |
else: |
| 2159 |
reveal_type(x) # E: Statement is unreachable |
| 2160 |
reveal_type(x) # N: Revealed type is "type[builtins.int]" |
| 2161 |
[builtins fixtures/isinstance.pyi] |
| 2162 |
|
| 2163 |
[case testIsinstanceVariableSubstitution] |
| 2164 |
# flags: --warn-unreachable |
| 2165 |
T = (int, str) |
| 2166 |
U = (list, T) |
| 2167 |
x: object = None |
| 2168 |
|
| 2169 |
if isinstance(x, T): |
| 2170 |
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" |
| 2171 |
|
| 2172 |
if isinstance(x, U): |
| 2173 |
reveal_type(x) # N: Revealed type is "builtins.list[Any] | builtins.int | builtins.str" |
| 2174 |
|
| 2175 |
if isinstance(x, (set, (list, T))): |
| 2176 |
reveal_type(x) # N: Revealed type is "builtins.set[Any] | builtins.list[Any] | builtins.int | builtins.str" |
| 2177 |
[builtins fixtures/isinstancelist.pyi] |
| 2178 |
|
| 2179 |
[case testIsInstanceTooFewArgs] |
| 2180 |
# flags: --warn-unreachable |
| 2181 |
isinstance() # E: Missing positional arguments "x", "t" in call to "isinstance" |
| 2182 |
x: object |
| 2183 |
if isinstance(): # E: Missing positional arguments "x", "t" in call to "isinstance" |
| 2184 |
x = 1 |
| 2185 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 2186 |
if isinstance(x): # E: Missing positional argument "t" in call to "isinstance" |
| 2187 |
x = 1 |
| 2188 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 2189 |
[builtins fixtures/isinstancelist.pyi] |
| 2190 |
|
| 2191 |
[case testIsSubclassTooFewArgs] |
| 2192 |
# flags: --warn-unreachable |
| 2193 |
from typing import Type |
| 2194 |
|
| 2195 |
issubclass() # E: Missing positional arguments "x", "t" in call to "issubclass" |
| 2196 |
y: Type[object] |
| 2197 |
if issubclass(): # E: Missing positional arguments "x", "t" in call to "issubclass" |
| 2198 |
reveal_type(y) # N: Revealed type is "type[builtins.object]" |
| 2199 |
if issubclass(y): # E: Missing positional argument "t" in call to "issubclass" |
| 2200 |
reveal_type(y) # N: Revealed type is "type[builtins.object]" |
| 2201 |
|
| 2202 |
[builtins fixtures/isinstancelist.pyi] |
| 2203 |
|
| 2204 |
[case testIsInstanceTooManyArgs] |
| 2205 |
# flags: --warn-unreachable |
| 2206 |
isinstance(1, 1, 1) # E: Too many arguments for "isinstance" \ |
| 2207 |
# E: Argument 2 to "isinstance" has incompatible type "int"; expected "type | tuple[Any, ...]" |
| 2208 |
x: object |
| 2209 |
if isinstance(x, str, 1): # E: Too many arguments for "isinstance" |
| 2210 |
reveal_type(x) # N: Revealed type is "builtins.object" |
| 2211 |
x = 1 |
| 2212 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 2213 |
[builtins fixtures/isinstancelist.pyi] |
| 2214 |
|
| 2215 |
[case testIsinstanceNarrowAnyExplicit] |
| 2216 |
# flags: --warn-unreachable |
| 2217 |
from typing import Any |
| 2218 |
|
| 2219 |
def narrow_any_to_str_then_reassign_to_int() -> None: |
| 2220 |
v: Any = 1 |
| 2221 |
|
| 2222 |
if isinstance(v, str): |
| 2223 |
reveal_type(v) # N: Revealed type is "builtins.str" |
| 2224 |
v = 2 |
| 2225 |
reveal_type(v) # N: Revealed type is "Any" |
| 2226 |
[builtins fixtures/isinstance.pyi] |
| 2227 |
|
| 2228 |
[case testIsinstanceNarrowAnyImplicit] |
| 2229 |
# flags: --warn-unreachable |
| 2230 |
def foo(): ... |
| 2231 |
|
| 2232 |
def narrow_any_to_str_then_reassign_to_int() -> None: |
| 2233 |
v = foo() |
| 2234 |
|
| 2235 |
if isinstance(v, str): |
| 2236 |
reveal_type(v) # N: Revealed type is "builtins.str" |
| 2237 |
v = 2 |
| 2238 |
reveal_type(v) # N: Revealed type is "builtins.int" |
| 2239 |
[builtins fixtures/isinstance.pyi] |
| 2240 |
|
| 2241 |
[case testNarrowTypeAfterInList] |
| 2242 |
# flags: --warn-unreachable |
| 2243 |
from typing import List, Optional |
| 2244 |
|
| 2245 |
x: List[int] |
| 2246 |
y: Optional[int] |
| 2247 |
|
| 2248 |
if y in x: |
| 2249 |
reveal_type(y) # N: Revealed type is "builtins.int" |
| 2250 |
else: |
| 2251 |
reveal_type(y) # N: Revealed type is "builtins.int | None" |
| 2252 |
if y not in x: |
| 2253 |
reveal_type(y) # N: Revealed type is "builtins.int | None" |
| 2254 |
else: |
| 2255 |
reveal_type(y) # N: Revealed type is "builtins.int" |
| 2256 |
[builtins fixtures/list.pyi] |
| 2257 |
|
| 2258 |
[case testNarrowTypeAfterInListOfOptional] |
| 2259 |
# flags: --warn-unreachable |
| 2260 |
from typing import List, Optional |
| 2261 |
|
| 2262 |
x: List[Optional[int]] |
| 2263 |
y: Optional[int] |
| 2264 |
|
| 2265 |
if y not in x: |
| 2266 |
reveal_type(y) # N: Revealed type is "builtins.int | None" |
| 2267 |
else: |
| 2268 |
reveal_type(y) # N: Revealed type is "builtins.int | None" |
| 2269 |
[builtins fixtures/list.pyi] |
| 2270 |
|
| 2271 |
[case testNarrowTypeAfterInListNonOverlapping] |
| 2272 |
# flags: --warn-unreachable |
| 2273 |
from typing import List, Optional |
| 2274 |
|
| 2275 |
def f(x: List[str], y: Optional[int]) -> None: |
| 2276 |
if y in x: |
| 2277 |
reveal_type(y) # E: Statement is unreachable |
| 2278 |
else: |
| 2279 |
reveal_type(y) # N: Revealed type is "builtins.int | None" |
| 2280 |
[builtins fixtures/list.pyi] |
| 2281 |
|
| 2282 |
[case testNarrowTypeAfterInListNested] |
| 2283 |
# flags: --warn-unreachable |
| 2284 |
from typing import List, Optional, Any |
| 2285 |
|
| 2286 |
def f(x: Optional[int], lst: Optional[List[int]], nested_any: List[List[Any]]) -> None: |
| 2287 |
if lst in nested_any: |
| 2288 |
reveal_type(lst) # N: Revealed type is "builtins.list[builtins.int]" |
| 2289 |
if x in nested_any: |
| 2290 |
reveal_type(x) # E: Statement is unreachable |
| 2291 |
[builtins fixtures/list.pyi] |
| 2292 |
|
| 2293 |
[case testNarrowTypeAfterInTuple] |
| 2294 |
# flags: --warn-unreachable |
| 2295 |
class A: pass |
| 2296 |
class B(A): pass |
| 2297 |
class C(A): pass |
| 2298 |
|
| 2299 |
def f(y: B | None): |
| 2300 |
if y in (B(), C()): |
| 2301 |
reveal_type(y) # N: Revealed type is "__main__.B" |
| 2302 |
else: |
| 2303 |
reveal_type(y) # N: Revealed type is "__main__.B | None" |
| 2304 |
[builtins fixtures/tuple.pyi] |
| 2305 |
|
| 2306 |
[case testNarrowTypeAfterInNamedTuple] |
| 2307 |
# flags: --warn-unreachable |
| 2308 |
from typing import NamedTuple, Optional |
| 2309 |
class NT(NamedTuple): |
| 2310 |
x: int |
| 2311 |
y: int |
| 2312 |
nt: NT |
| 2313 |
|
| 2314 |
y: Optional[int] |
| 2315 |
if y not in nt: |
| 2316 |
reveal_type(y) # N: Revealed type is "builtins.int | None" |
| 2317 |
else: |
| 2318 |
reveal_type(y) # N: Revealed type is "builtins.int" |
| 2319 |
[builtins fixtures/tuple.pyi] |
| 2320 |
|
| 2321 |
[case testNarrowTypeAfterInDict] |
| 2322 |
# flags: --warn-unreachable |
| 2323 |
from typing import Dict, Optional |
| 2324 |
x: Dict[str, int] |
| 2325 |
y: Optional[str] |
| 2326 |
|
| 2327 |
if y in x: |
| 2328 |
reveal_type(y) # N: Revealed type is "builtins.str" |
| 2329 |
else: |
| 2330 |
reveal_type(y) # N: Revealed type is "builtins.str | None" |
| 2331 |
if y not in x: |
| 2332 |
reveal_type(y) # N: Revealed type is "builtins.str | None" |
| 2333 |
else: |
| 2334 |
reveal_type(y) # N: Revealed type is "builtins.str" |
| 2335 |
[builtins fixtures/dict.pyi] |
| 2336 |
|
| 2337 |
[case testNarrowTypeAfterInNoAnyOrObject] |
| 2338 |
# flags: --warn-unreachable |
| 2339 |
from typing import Any, List, Optional |
| 2340 |
x: List[Any] |
| 2341 |
z: List[object] |
| 2342 |
|
| 2343 |
y: Optional[int] |
| 2344 |
if y in x: |
| 2345 |
reveal_type(y) # N: Revealed type is "builtins.int | None" |
| 2346 |
else: |
| 2347 |
reveal_type(y) # N: Revealed type is "builtins.int | None" |
| 2348 |
|
| 2349 |
if y not in z: |
| 2350 |
reveal_type(y) # N: Revealed type is "builtins.int | None" |
| 2351 |
else: |
| 2352 |
reveal_type(y) # N: Revealed type is "builtins.int | None" |
| 2353 |
[typing fixtures/typing-medium.pyi] |
| 2354 |
[builtins fixtures/list.pyi] |
| 2355 |
|
| 2356 |
[case testNarrowTypeAfterInUserDefined] |
| 2357 |
# flags: --warn-unreachable |
| 2358 |
from typing import Container, Optional |
| 2359 |
|
| 2360 |
class C(Container[int]): |
| 2361 |
def __contains__(self, item: object) -> bool: |
| 2362 |
return item is 'surprise' |
| 2363 |
|
| 2364 |
y: Optional[int] |
| 2365 |
# We never trust user defined types |
| 2366 |
if y in C(): |
| 2367 |
reveal_type(y) # N: Revealed type is "builtins.int | None" |
| 2368 |
else: |
| 2369 |
reveal_type(y) # N: Revealed type is "builtins.int | None" |
| 2370 |
if y not in C(): |
| 2371 |
reveal_type(y) # N: Revealed type is "builtins.int | None" |
| 2372 |
else: |
| 2373 |
reveal_type(y) # N: Revealed type is "builtins.int | None" |
| 2374 |
[typing fixtures/typing-full.pyi] |
| 2375 |
[builtins fixtures/list.pyi] |
| 2376 |
|
| 2377 |
[case testNarrowTypeAfterInSet] |
| 2378 |
# flags: --warn-unreachable |
| 2379 |
from typing import Optional, Set |
| 2380 |
s: Set[str] |
| 2381 |
|
| 2382 |
y: Optional[str] |
| 2383 |
if y in {'a', 'b', 'c'}: |
| 2384 |
reveal_type(y) # N: Revealed type is "builtins.str" |
| 2385 |
else: |
| 2386 |
reveal_type(y) # N: Revealed type is "builtins.str | None" |
| 2387 |
if y not in s: |
| 2388 |
reveal_type(y) # N: Revealed type is "builtins.str | None" |
| 2389 |
else: |
| 2390 |
reveal_type(y) # N: Revealed type is "builtins.str" |
| 2391 |
[builtins fixtures/set.pyi] |
| 2392 |
|
| 2393 |
[case testNarrowTypeAfterInTypedDict] |
| 2394 |
# flags: --warn-unreachable |
| 2395 |
from typing import Optional, TypedDict |
| 2396 |
class TD(TypedDict): |
| 2397 |
a: int |
| 2398 |
b: str |
| 2399 |
td: TD |
| 2400 |
|
| 2401 |
def f() -> None: |
| 2402 |
x: Optional[str] |
| 2403 |
if x not in td: |
| 2404 |
return |
| 2405 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 2406 |
[builtins fixtures/dict.pyi] |
| 2407 |
[typing fixtures/typing-typeddict.pyi] |
| 2408 |
|
| 2409 |
[case testIsinstanceWidensWithAnyArg] |
| 2410 |
# flags: --warn-unreachable |
| 2411 |
from typing import Any |
| 2412 |
class A: ... |
| 2413 |
B: Any |
| 2414 |
x: A |
| 2415 |
x.foo() # E: "A" has no attribute "foo" |
| 2416 |
assert isinstance(x, B) |
| 2417 |
x.foo() |
| 2418 |
reveal_type(x) # N: Revealed type is "Any" |
| 2419 |
[builtins fixtures/isinstance.pyi] |
| 2420 |
|
| 2421 |
[case testIsinstanceWidensUnionWithAnyArg] |
| 2422 |
# flags: --warn-unreachable |
| 2423 |
from typing import Any, Union |
| 2424 |
class A: ... |
| 2425 |
B: Any |
| 2426 |
x: Union[A, B] |
| 2427 |
reveal_type(x) # N: Revealed type is "__main__.A | Any" |
| 2428 |
assert isinstance(x, B) |
| 2429 |
reveal_type(x) # N: Revealed type is "Any" |
| 2430 |
[builtins fixtures/isinstance.pyi] |
| 2431 |
|
| 2432 |
[case testIsinstanceNarrowsAfterNoneCheckWithAnyUnion] |
| 2433 |
# flags: --warn-unreachable |
| 2434 |
from typing import Any, Union |
| 2435 |
|
| 2436 |
def f(val: Union[Any, str]) -> None: |
| 2437 |
if val is None or isinstance(val, str): |
| 2438 |
reveal_type(val) # N: Revealed type is "None | builtins.str" |
| 2439 |
return |
| 2440 |
reveal_type(val) # N: Revealed type is "Any" |
| 2441 |
[builtins fixtures/isinstance.pyi] |
| 2442 |
|
| 2443 |
[case testIsinstanceIgnoredImport] |
| 2444 |
# flags: --warn-unreachable |
| 2445 |
from typing import Union |
| 2446 |
from foo import A # type: ignore |
| 2447 |
def f(x: Union[A, str]) -> None: |
| 2448 |
x.method_only_in_a() # E: Item "str" of "Any | str" has no attribute "method_only_in_a" |
| 2449 |
if isinstance(x, A): |
| 2450 |
x.method_only_in_a() |
| 2451 |
[builtins fixtures/isinstance.pyi] |
| 2452 |
|
| 2453 |
[case testIsinstanceIgnoredImportDualAny] |
| 2454 |
# flags: --warn-unreachable |
| 2455 |
from typing import Any |
| 2456 |
from foo import Bad, OtherBad # type: ignore |
| 2457 |
x: Any |
| 2458 |
if isinstance(x, Bad): |
| 2459 |
reveal_type(x) # N: Revealed type is "Any" |
| 2460 |
else: |
| 2461 |
reveal_type(x) # N: Revealed type is "Any" |
| 2462 |
|
| 2463 |
if isinstance(x, (Bad, OtherBad)): |
| 2464 |
reveal_type(x) # N: Revealed type is "Any" |
| 2465 |
else: |
| 2466 |
reveal_type(x) # N: Revealed type is "Any" |
| 2467 |
|
| 2468 |
y: object |
| 2469 |
if isinstance(y, Bad): |
| 2470 |
reveal_type(y) # N: Revealed type is "Any" |
| 2471 |
else: |
| 2472 |
reveal_type(y) # N: Revealed type is "builtins.object" |
| 2473 |
|
| 2474 |
class Ok: pass |
| 2475 |
z: Any |
| 2476 |
if isinstance(z, Ok): |
| 2477 |
reveal_type(z) # N: Revealed type is "__main__.Ok" |
| 2478 |
else: |
| 2479 |
reveal_type(z) # N: Revealed type is "Any" |
| 2480 |
[builtins fixtures/isinstance.pyi] |
| 2481 |
|
| 2482 |
[case testIsInstanceInitialNoneCheckSkipsImpossibleCasesNoStrictOptional] |
| 2483 |
# flags: --warn-unreachable |
| 2484 |
from typing import Optional, Union |
| 2485 |
|
| 2486 |
class A: pass |
| 2487 |
|
| 2488 |
def foo1(x: Union[A, str, None]) -> None: |
| 2489 |
if x is None: |
| 2490 |
reveal_type(x) # N: Revealed type is "None" |
| 2491 |
elif isinstance(x, A): |
| 2492 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 2493 |
else: |
| 2494 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 2495 |
|
| 2496 |
def foo2(x: Optional[str]) -> None: |
| 2497 |
if x is None: |
| 2498 |
reveal_type(x) # N: Revealed type is "None" |
| 2499 |
elif isinstance(x, A): |
| 2500 |
reveal_type(x) # N: Revealed type is "__main__.<subclass of "builtins.str" and "__main__.A">" |
| 2501 |
else: |
| 2502 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 2503 |
[builtins fixtures/isinstance.pyi] |
| 2504 |
|
| 2505 |
[case testIsInstanceInitialNoneCheckSkipsImpossibleCasesInNoStrictOptional] |
| 2506 |
# flags: --no-strict-optional --warn-unreachable |
| 2507 |
from typing import Optional, Union |
| 2508 |
|
| 2509 |
class A: pass |
| 2510 |
|
| 2511 |
def foo1(x: Union[A, str, None]) -> None: |
| 2512 |
if x is None: |
| 2513 |
reveal_type(x) # N: Revealed type is "None" |
| 2514 |
elif isinstance(x, A): |
| 2515 |
# Note that Union[None, A] == A in no-strict-optional |
| 2516 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 2517 |
else: |
| 2518 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 2519 |
|
| 2520 |
def foo2(x: Optional[str]) -> None: |
| 2521 |
if x is None: |
| 2522 |
reveal_type(x) # N: Revealed type is "None" |
| 2523 |
elif isinstance(x, A): |
| 2524 |
reveal_type(x) # N: Revealed type is "__main__.<subclass of "builtins.str" and "__main__.A">" |
| 2525 |
else: |
| 2526 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 2527 |
[builtins fixtures/isinstance.pyi] |
| 2528 |
|
| 2529 |
[case testNoneCheckDoesNotMakeTypeVarOptional] |
| 2530 |
# flags: --warn-unreachable |
| 2531 |
from typing import TypeVar |
| 2532 |
|
| 2533 |
T = TypeVar('T') |
| 2534 |
|
| 2535 |
def foo_if(x: T) -> T: |
| 2536 |
out = None |
| 2537 |
out = x |
| 2538 |
if out is None: |
| 2539 |
pass |
| 2540 |
return out |
| 2541 |
|
| 2542 |
def foo_while(x: T) -> T: |
| 2543 |
out = None |
| 2544 |
out = x |
| 2545 |
while out is None: |
| 2546 |
pass |
| 2547 |
return out |
| 2548 |
[builtins fixtures/isinstance.pyi] |
| 2549 |
|
| 2550 |
[case testNoneCheckDoesNotNarrowWhenUsingTypeVarsNoStrictOptional] |
| 2551 |
# flags: --no-strict-optional --warn-unreachable |
| 2552 |
from typing import TypeVar |
| 2553 |
|
| 2554 |
T = TypeVar('T') |
| 2555 |
|
| 2556 |
def foo(x: T) -> T: |
| 2557 |
out = None |
| 2558 |
out = x |
| 2559 |
if out is None: |
| 2560 |
pass |
| 2561 |
return out |
| 2562 |
[builtins fixtures/isinstance.pyi] |
| 2563 |
|
| 2564 |
[case testNoneAndGenericTypesOverlapNoStrictOptional] |
| 2565 |
# flags: --no-strict-optional --warn-unreachable |
| 2566 |
from typing import Union, Optional, List |
| 2567 |
|
| 2568 |
# Note: this test is indirectly making sure meet.is_overlapping_types |
| 2569 |
# correctly ignores 'None' in unions. |
| 2570 |
|
| 2571 |
def foo(x: Optional[List[str]]) -> None: |
| 2572 |
reveal_type(x) # N: Revealed type is "builtins.list[builtins.str] | None" |
| 2573 |
assert isinstance(x, list) |
| 2574 |
reveal_type(x) # N: Revealed type is "builtins.list[builtins.str]" |
| 2575 |
|
| 2576 |
def bar(x: Union[List[str], List[int], None]) -> None: |
| 2577 |
reveal_type(x) # N: Revealed type is "builtins.list[builtins.str] | builtins.list[builtins.int] | None" |
| 2578 |
assert isinstance(x, list) |
| 2579 |
reveal_type(x) # N: Revealed type is "builtins.list[builtins.str] | builtins.list[builtins.int]" |
| 2580 |
[builtins fixtures/isinstancelist.pyi] |
| 2581 |
|
| 2582 |
[case testNoneAndGenericTypesOverlapStrictOptional] |
| 2583 |
# flags: --warn-unreachable |
| 2584 |
from typing import Union, Optional, List |
| 2585 |
|
| 2586 |
# This test is the same as the one above, except for strict-optional. |
| 2587 |
# It isn't testing anything explicitly and mostly exists for the sake |
| 2588 |
# of completeness. |
| 2589 |
|
| 2590 |
def foo(x: Optional[List[str]]) -> None: |
| 2591 |
reveal_type(x) # N: Revealed type is "builtins.list[builtins.str] | None" |
| 2592 |
assert isinstance(x, list) |
| 2593 |
reveal_type(x) # N: Revealed type is "builtins.list[builtins.str]" |
| 2594 |
|
| 2595 |
def bar(x: Union[List[str], List[int], None]) -> None: |
| 2596 |
reveal_type(x) # N: Revealed type is "builtins.list[builtins.str] | builtins.list[builtins.int] | None" |
| 2597 |
assert isinstance(x, list) |
| 2598 |
reveal_type(x) # N: Revealed type is "builtins.list[builtins.str] | builtins.list[builtins.int]" |
| 2599 |
[builtins fixtures/isinstancelist.pyi] |
| 2600 |
|
| 2601 |
[case testIsInstanceWithStarExpression] |
| 2602 |
# flags: --warn-unreachable |
| 2603 |
from typing import Union, List, Tuple |
| 2604 |
|
| 2605 |
def f(var: Union[List[str], Tuple[str, str], str]) -> None: |
| 2606 |
reveal_type(var) # N: Revealed type is "builtins.list[builtins.str] | tuple[builtins.str, builtins.str] | builtins.str" |
| 2607 |
if isinstance(var, (list, *(str, int))): |
| 2608 |
reveal_type(var) # N: Revealed type is "builtins.list[builtins.str] | builtins.str" |
| 2609 |
[builtins fixtures/isinstancelist.pyi] |
| 2610 |
|
| 2611 |
[case testIsInstanceWithStarExpressionAndVariable] |
| 2612 |
# flags: --warn-unreachable |
| 2613 |
from typing import Union |
| 2614 |
|
| 2615 |
def f(var: Union[int, str]) -> None: |
| 2616 |
reveal_type(var) # N: Revealed type is "builtins.int | builtins.str" |
| 2617 |
some_types = (str, tuple) |
| 2618 |
another_type = list |
| 2619 |
if isinstance(var, (*some_types, another_type)): |
| 2620 |
reveal_type(var) # N: Revealed type is "builtins.str" |
| 2621 |
[builtins fixtures/isinstancelist.pyi] |
| 2622 |
|
| 2623 |
[case testIsInstanceWithWrongStarExpression] |
| 2624 |
# flags: --warn-unreachable |
| 2625 |
var = 'some string' |
| 2626 |
if isinstance(var, *(str, int)): # E: Too many arguments for "isinstance" |
| 2627 |
pass |
| 2628 |
[builtins fixtures/isinstancelist.pyi] |
| 2629 |
|
| 2630 |
[case testIsInstanceAdHocIntersectionBasic] |
| 2631 |
# flags: --warn-unreachable |
| 2632 |
class A: |
| 2633 |
def f1(self) -> int: ... |
| 2634 |
class B: |
| 2635 |
def f2(self) -> int: ... |
| 2636 |
class C: |
| 2637 |
def f3(self) -> int: ... |
| 2638 |
|
| 2639 |
x: A |
| 2640 |
if isinstance(x, B): |
| 2641 |
reveal_type(x) # N: Revealed type is "__main__.<subclass of "__main__.A" and "__main__.B">" |
| 2642 |
if isinstance(x, C): |
| 2643 |
reveal_type(x) # N: Revealed type is "__main__.<subclass of "__main__.A", "__main__.B", and "__main__.C">" |
| 2644 |
reveal_type(x.f1()) # N: Revealed type is "builtins.int" |
| 2645 |
reveal_type(x.f2()) # N: Revealed type is "builtins.int" |
| 2646 |
reveal_type(x.f3()) # N: Revealed type is "builtins.int" |
| 2647 |
x.bad() # E: "<subclass of "__main__.A", "__main__.B", and "__main__.C">" has no attribute "bad" |
| 2648 |
else: |
| 2649 |
reveal_type(x) # N: Revealed type is "__main__.<subclass of "__main__.A" and "__main__.B">" |
| 2650 |
else: |
| 2651 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 2652 |
[builtins fixtures/isinstance.pyi] |
| 2653 |
|
| 2654 |
[case testIsInstanceAdHocIntersectionRepeatedChecks] |
| 2655 |
# flags: --warn-unreachable |
| 2656 |
|
| 2657 |
class A: pass |
| 2658 |
class B: pass |
| 2659 |
|
| 2660 |
x: A |
| 2661 |
if isinstance(x, B): |
| 2662 |
reveal_type(x) # N: Revealed type is "__main__.<subclass of "__main__.A" and "__main__.B">" |
| 2663 |
if isinstance(x, A): |
| 2664 |
reveal_type(x) # N: Revealed type is "__main__.<subclass of "__main__.A" and "__main__.B">" |
| 2665 |
if isinstance(x, B): |
| 2666 |
reveal_type(x) # N: Revealed type is "__main__.<subclass of "__main__.A" and "__main__.B">" |
| 2667 |
[builtins fixtures/isinstance.pyi] |
| 2668 |
|
| 2669 |
[case testIsInstanceAdHocIntersectionIncompatibleClasses] |
| 2670 |
# flags: --warn-unreachable |
| 2671 |
class A: |
| 2672 |
def f(self) -> int: ... |
| 2673 |
class B: |
| 2674 |
def f(self) -> str: ... |
| 2675 |
class C: |
| 2676 |
def f(self) -> str: ... |
| 2677 |
|
| 2678 |
class Example(A, B): pass # E: Definition of "f" in base class "A" is incompatible with definition in base class "B" |
| 2679 |
x: A |
| 2680 |
if isinstance(x, B): # E: Subclass of "A" and "B" cannot exist: would have incompatible method signatures |
| 2681 |
reveal_type(x) # E: Statement is unreachable |
| 2682 |
else: |
| 2683 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 2684 |
|
| 2685 |
y: C |
| 2686 |
if isinstance(y, B): |
| 2687 |
reveal_type(y) # N: Revealed type is "__main__.<subclass of "__main__.C" and "__main__.B">" |
| 2688 |
if isinstance(y, A): # E: Subclass of "C", "B", and "A" cannot exist: would have incompatible method signatures |
| 2689 |
reveal_type(y) # E: Statement is unreachable |
| 2690 |
[builtins fixtures/isinstance.pyi] |
| 2691 |
|
| 2692 |
[case testIsInstanceAdHocIntersectionReversed] |
| 2693 |
# flags: --warn-unreachable |
| 2694 |
|
| 2695 |
from abc import abstractmethod |
| 2696 |
from typing import Literal |
| 2697 |
|
| 2698 |
class A0: |
| 2699 |
def f(self) -> Literal[0]: |
| 2700 |
... |
| 2701 |
|
| 2702 |
class A1: |
| 2703 |
def f(self) -> Literal[1]: |
| 2704 |
... |
| 2705 |
|
| 2706 |
class A2: |
| 2707 |
def f(self) -> Literal[2]: |
| 2708 |
... |
| 2709 |
|
| 2710 |
class B: |
| 2711 |
@abstractmethod |
| 2712 |
def f(self) -> Literal[1, 2]: |
| 2713 |
... |
| 2714 |
|
| 2715 |
def t0(self) -> None: |
| 2716 |
if isinstance(self, A0): # E: Subclass of "B" and "A0" cannot exist: would have incompatible method signatures |
| 2717 |
x0: Literal[0] = self.f() # E: Statement is unreachable |
| 2718 |
|
| 2719 |
def t1(self) -> None: |
| 2720 |
if isinstance(self, A1): |
| 2721 |
reveal_type(self) # N: Revealed type is "__main__.<subclass of "__main__.A1" and "__main__.B">" |
| 2722 |
x0: Literal[0] = self.f() # E: Incompatible types in assignment (expression has type "Literal[1]", variable has type "Literal[0]") |
| 2723 |
x1: Literal[1] = self.f() |
| 2724 |
|
| 2725 |
def t2(self) -> None: |
| 2726 |
if isinstance(self, (A0, A1)): |
| 2727 |
reveal_type(self) # N: Revealed type is "__main__.<subclass of "__main__.A1" and "__main__.B">" |
| 2728 |
x0: Literal[0] = self.f() # E: Incompatible types in assignment (expression has type "Literal[1]", variable has type "Literal[0]") |
| 2729 |
x1: Literal[1] = self.f() |
| 2730 |
|
| 2731 |
def t3(self) -> None: |
| 2732 |
if isinstance(self, (A1, A2)): |
| 2733 |
reveal_type(self) # N: Revealed type is "__main__.<subclass of "__main__.A1" and "__main__.B"> | __main__.<subclass of "__main__.A2" and "__main__.B">" |
| 2734 |
x0: Literal[0] = self.f() # E: Incompatible types in assignment (expression has type "Literal[1, 2]", variable has type "Literal[0]") |
| 2735 |
x1: Literal[1] = self.f() # E: Incompatible types in assignment (expression has type "Literal[1, 2]", variable has type "Literal[1]") |
| 2736 |
|
| 2737 |
[builtins fixtures/isinstance.pyi] |
| 2738 |
|
| 2739 |
[case testIsInstanceAdHocIntersectionGenerics] |
| 2740 |
# flags: --warn-unreachable |
| 2741 |
from typing import Generic, TypeVar |
| 2742 |
|
| 2743 |
class Parent: pass |
| 2744 |
class Child(Parent): pass |
| 2745 |
|
| 2746 |
T = TypeVar('T') |
| 2747 |
class A(Generic[T]): |
| 2748 |
def f(self) -> T: ... |
| 2749 |
class B: |
| 2750 |
def f(self) -> Parent: ... |
| 2751 |
|
| 2752 |
x: A[int] |
| 2753 |
if isinstance(x, B): # E: Subclass of "A[int]" and "B" cannot exist: would have incompatible method signatures |
| 2754 |
reveal_type(x) # E: Statement is unreachable |
| 2755 |
else: |
| 2756 |
reveal_type(x) # N: Revealed type is "__main__.A[builtins.int]" |
| 2757 |
|
| 2758 |
y: A[Parent] |
| 2759 |
if isinstance(y, B): |
| 2760 |
reveal_type(y) # N: Revealed type is "__main__.<subclass of "__main__.A[__main__.Parent]" and "__main__.B">" |
| 2761 |
reveal_type(y.f()) # N: Revealed type is "__main__.Parent" |
| 2762 |
else: |
| 2763 |
reveal_type(y) # N: Revealed type is "__main__.A[__main__.Parent]" |
| 2764 |
|
| 2765 |
z: A[Child] |
| 2766 |
if isinstance(z, B): |
| 2767 |
reveal_type(z) # N: Revealed type is "__main__.<subclass of "__main__.A[__main__.Child]" and "__main__.B">" |
| 2768 |
reveal_type(z.f()) # N: Revealed type is "__main__.Child" |
| 2769 |
else: |
| 2770 |
reveal_type(z) # N: Revealed type is "__main__.A[__main__.Child]" |
| 2771 |
[builtins fixtures/isinstance.pyi] |
| 2772 |
|
| 2773 |
[case testIsInstanceAdHocIntersectionGenericsWithValues] |
| 2774 |
# flags: --warn-unreachable |
| 2775 |
from typing import TypeVar |
| 2776 |
|
| 2777 |
class A: |
| 2778 |
attr: int |
| 2779 |
class B: |
| 2780 |
attr: int |
| 2781 |
class C: |
| 2782 |
attr: str |
| 2783 |
|
| 2784 |
T1 = TypeVar('T1', A, B) |
| 2785 |
def f1(x: T1) -> T1: |
| 2786 |
if isinstance(x, A): |
| 2787 |
reveal_type(x) # N: Revealed type is "__main__.A" \ |
| 2788 |
# N: Revealed type is "__main__.<subclass of "__main__.B" and "__main__.A">" |
| 2789 |
if isinstance(x, B): |
| 2790 |
reveal_type(x) # N: Revealed type is "__main__.<subclass of "__main__.A" and "__main__.B">" \ |
| 2791 |
# N: Revealed type is "__main__.<subclass of "__main__.B" and "__main__.A">" |
| 2792 |
else: |
| 2793 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 2794 |
else: |
| 2795 |
reveal_type(x) # N: Revealed type is "__main__.B" |
| 2796 |
return x |
| 2797 |
|
| 2798 |
T2 = TypeVar('T2', B, C) |
| 2799 |
def f2(x: T2) -> T2: |
| 2800 |
if isinstance(x, B): |
| 2801 |
reveal_type(x) # N: Revealed type is "__main__.B" |
| 2802 |
# Note: even though --warn-unreachable is set, we don't report |
| 2803 |
# errors for the below: we don't yet have a way of filtering out |
| 2804 |
# reachability errors that occur for only one variation of the |
| 2805 |
# TypeVar yet. |
| 2806 |
if isinstance(x, C): |
| 2807 |
reveal_type(x) |
| 2808 |
else: |
| 2809 |
reveal_type(x) # N: Revealed type is "__main__.B" |
| 2810 |
else: |
| 2811 |
reveal_type(x) # N: Revealed type is "__main__.C" |
| 2812 |
return x |
| 2813 |
[builtins fixtures/isinstance.pyi] |
| 2814 |
|
| 2815 |
[case testIsInstanceAdHocIntersectionGenericsWithValuesDirectReturn] |
| 2816 |
# flags: --warn-unreachable |
| 2817 |
from typing import TypeVar |
| 2818 |
|
| 2819 |
class A: |
| 2820 |
attr: int |
| 2821 |
class B: |
| 2822 |
attr: int |
| 2823 |
class C: |
| 2824 |
attr: str |
| 2825 |
|
| 2826 |
T1 = TypeVar('T1', A, B) |
| 2827 |
def f1(x: T1) -> T1: |
| 2828 |
if isinstance(x, A): |
| 2829 |
# The error message is confusing, but we indeed do run into problems if |
| 2830 |
# 'x' is a subclass of __main__.A and __main__.B |
| 2831 |
return A() # E: Incompatible return value type (got "A", expected "B") |
| 2832 |
else: |
| 2833 |
return B() |
| 2834 |
|
| 2835 |
T2 = TypeVar('T2', B, C) |
| 2836 |
def f2(x: T2) -> T2: |
| 2837 |
if isinstance(x, B): |
| 2838 |
# In contrast, it's impossible for a subclass of "B" and "C" to |
| 2839 |
# exist, so this is fine |
| 2840 |
return B() |
| 2841 |
else: |
| 2842 |
return C() |
| 2843 |
[builtins fixtures/isinstance.pyi] |
| 2844 |
|
| 2845 |
[case testIsInstanceDisjointBase] |
| 2846 |
# flags: --warn-unreachable |
| 2847 |
from typing_extensions import disjoint_base |
| 2848 |
|
| 2849 |
@disjoint_base |
| 2850 |
class Disjoint1: pass |
| 2851 |
@disjoint_base |
| 2852 |
class Disjoint2: pass |
| 2853 |
@disjoint_base |
| 2854 |
class Disjoint3(Disjoint1): pass |
| 2855 |
class Child(Disjoint1): pass |
| 2856 |
class Unrelated: pass |
| 2857 |
|
| 2858 |
def f(d1: Disjoint1, u: Unrelated, c: Child) -> object: |
| 2859 |
if isinstance(d1, Disjoint2): # E: Subclass of "Disjoint1" and "Disjoint2" cannot exist: have distinct disjoint bases |
| 2860 |
return u # E: Statement is unreachable |
| 2861 |
if isinstance(u, Disjoint1): # OK |
| 2862 |
return d1 |
| 2863 |
if isinstance(c, Disjoint3): # OK |
| 2864 |
return c |
| 2865 |
if isinstance(c, Disjoint2): # E: Subclass of "Child" and "Disjoint2" cannot exist: have distinct disjoint bases |
| 2866 |
return c # E: Statement is unreachable |
| 2867 |
return d1 |
| 2868 |
|
| 2869 |
[builtins fixtures/isinstance.pyi] |
| 2870 |
|
| 2871 |
|
| 2872 |
[case testIsInstanceAdHocIntersectionUsage] |
| 2873 |
# flags: --warn-unreachable |
| 2874 |
class A: pass |
| 2875 |
class B: pass |
| 2876 |
class Concrete(A, B): pass |
| 2877 |
|
| 2878 |
def accept_a(a: A) -> None: pass |
| 2879 |
def accept_b(a: B) -> None: pass |
| 2880 |
def accept_concrete(c: Concrete) -> None: pass |
| 2881 |
|
| 2882 |
x: A |
| 2883 |
if isinstance(x, B): |
| 2884 |
var = x |
| 2885 |
reveal_type(var) # N: Revealed type is "__main__.<subclass of "__main__.A" and "__main__.B">" |
| 2886 |
accept_a(var) |
| 2887 |
accept_b(var) |
| 2888 |
accept_concrete(var) # E: Argument 1 to "accept_concrete" has incompatible type "<subclass of "__main__.A" and "__main__.B">"; expected "Concrete" |
| 2889 |
[builtins fixtures/isinstance.pyi] |
| 2890 |
|
| 2891 |
[case testIsInstanceAdHocIntersectionReinfer] |
| 2892 |
# flags: --warn-unreachable |
| 2893 |
class A: pass |
| 2894 |
class B: pass |
| 2895 |
|
| 2896 |
x: A |
| 2897 |
assert isinstance(x, B) |
| 2898 |
reveal_type(x) # N: Revealed type is "__main__.<subclass of "__main__.A" and "__main__.B">" |
| 2899 |
|
| 2900 |
y: A |
| 2901 |
assert isinstance(y, B) |
| 2902 |
reveal_type(y) # N: Revealed type is "__main__.<subclass of "__main__.A" and "__main__.B">" |
| 2903 |
|
| 2904 |
x = y |
| 2905 |
reveal_type(x) # N: Revealed type is "__main__.<subclass of "__main__.A" and "__main__.B">" |
| 2906 |
[builtins fixtures/isinstance.pyi] |
| 2907 |
|
| 2908 |
[case testIsInstanceAdHocIntersectionWithUnions] |
| 2909 |
# flags: --warn-unreachable |
| 2910 |
from typing import Type, Union |
| 2911 |
class A: pass |
| 2912 |
class B: pass |
| 2913 |
class C: pass |
| 2914 |
class D: pass |
| 2915 |
|
| 2916 |
v1: A |
| 2917 |
if isinstance(v1, (B, C)): |
| 2918 |
reveal_type(v1) # N: Revealed type is "__main__.<subclass of "__main__.A" and "__main__.B"> | __main__.<subclass of "__main__.A" and "__main__.C">" |
| 2919 |
|
| 2920 |
v2: Union[A, B] |
| 2921 |
if isinstance(v2, C): |
| 2922 |
reveal_type(v2) # N: Revealed type is "__main__.<subclass of "__main__.A" and "__main__.C"> | __main__.<subclass of "__main__.B" and "__main__.C">" |
| 2923 |
|
| 2924 |
v3: Union[A, B] |
| 2925 |
if isinstance(v3, (C, D)): |
| 2926 |
reveal_type(v3) # N: Revealed type is "__main__.<subclass of "__main__.A" and "__main__.C"> | __main__.<subclass of "__main__.A" and "__main__.D"> | __main__.<subclass of "__main__.B" and "__main__.C"> | __main__.<subclass of "__main__.B" and "__main__.D">" |
| 2927 |
[builtins fixtures/isinstance.pyi] |
| 2928 |
|
| 2929 |
[case testIsInstanceAdHocIntersectionSameNames] |
| 2930 |
# flags: --warn-unreachable |
| 2931 |
from foo import A as A2 |
| 2932 |
class A: pass |
| 2933 |
|
| 2934 |
x: A |
| 2935 |
if isinstance(x, A2): |
| 2936 |
reveal_type(x) # N: Revealed type is "__main__.<subclass of "__main__.A" and "foo.A">" |
| 2937 |
|
| 2938 |
[file foo.py] |
| 2939 |
class A: pass |
| 2940 |
[builtins fixtures/isinstance.pyi] |
| 2941 |
|
| 2942 |
[case testIsInstanceAdHocIntersectionBadMro] |
| 2943 |
# flags: --warn-unreachable |
| 2944 |
class X: pass |
| 2945 |
class Y: pass |
| 2946 |
class A(X, Y): pass |
| 2947 |
class B(Y, X): pass |
| 2948 |
|
| 2949 |
foo: A |
| 2950 |
if isinstance(foo, B): # E: Subclass of "A" and "B" cannot exist: would have inconsistent method resolution order |
| 2951 |
reveal_type(foo) # E: Statement is unreachable |
| 2952 |
[builtins fixtures/isinstance.pyi] |
| 2953 |
|
| 2954 |
[case testIsInstanceAdHocIntersectionAmbiguousClass] |
| 2955 |
# flags: --warn-unreachable |
| 2956 |
from typing import Any |
| 2957 |
|
| 2958 |
class Concrete: |
| 2959 |
x: int |
| 2960 |
class Ambiguous: |
| 2961 |
x: Any |
| 2962 |
|
| 2963 |
# We bias towards assuming these two classes could be overlapping |
| 2964 |
foo: Concrete |
| 2965 |
if isinstance(foo, Ambiguous): |
| 2966 |
reveal_type(foo) # N: Revealed type is "__main__.<subclass of "__main__.Concrete" and "__main__.Ambiguous">" |
| 2967 |
reveal_type(foo.x) # N: Revealed type is "builtins.int" |
| 2968 |
[builtins fixtures/isinstance.pyi] |
| 2969 |
|
| 2970 |
[case testIsSubclassAdHocIntersection] |
| 2971 |
# flags: --warn-unreachable |
| 2972 |
from typing import Type |
| 2973 |
|
| 2974 |
class A: |
| 2975 |
x: int |
| 2976 |
class B: |
| 2977 |
x: int |
| 2978 |
class C: |
| 2979 |
x: str |
| 2980 |
|
| 2981 |
x: Type[A] |
| 2982 |
if issubclass(x, B): |
| 2983 |
reveal_type(x) # N: Revealed type is "type[__main__.<subclass of "__main__.A" and "__main__.B">]" |
| 2984 |
if issubclass(x, C): # E: Subclass of "A", "B", and "C" cannot exist: would have incompatible method signatures |
| 2985 |
reveal_type(x) # E: Statement is unreachable |
| 2986 |
else: |
| 2987 |
reveal_type(x) # N: Revealed type is "type[__main__.<subclass of "__main__.A" and "__main__.B">]" |
| 2988 |
else: |
| 2989 |
reveal_type(x) # N: Revealed type is "type[__main__.A]" |
| 2990 |
[builtins fixtures/isinstance.pyi] |
| 2991 |
|
| 2992 |
[case testHasAttrExistingAttribute] |
| 2993 |
# flags: --warn-unreachable |
| 2994 |
class C: |
| 2995 |
x: int |
| 2996 |
c: C |
| 2997 |
if hasattr(c, "x"): |
| 2998 |
reveal_type(c.x) # N: Revealed type is "builtins.int" |
| 2999 |
else: |
| 3000 |
# We don't mark this unreachable since people may check for deleted attributes |
| 3001 |
reveal_type(c.x) # N: Revealed type is "builtins.int" |
| 3002 |
[builtins fixtures/isinstance.pyi] |
| 3003 |
|
| 3004 |
[case testHasAttrMissingAttributeInstance] |
| 3005 |
# flags: --warn-unreachable |
| 3006 |
class B: ... |
| 3007 |
b: B |
| 3008 |
if hasattr(b, "x"): |
| 3009 |
reveal_type(b.x) # N: Revealed type is "Any" |
| 3010 |
else: |
| 3011 |
b.x # E: "B" has no attribute "x" |
| 3012 |
[builtins fixtures/isinstance.pyi] |
| 3013 |
|
| 3014 |
[case testHasAttrMissingAttributeFunction] |
| 3015 |
# flags: --warn-unreachable |
| 3016 |
def foo(x: int) -> None: ... |
| 3017 |
if hasattr(foo, "x"): |
| 3018 |
reveal_type(foo.x) # N: Revealed type is "Any" |
| 3019 |
[builtins fixtures/isinstance.pyi] |
| 3020 |
|
| 3021 |
[case testHasAttrMissingAttributeClassObject] |
| 3022 |
# flags: --warn-unreachable |
| 3023 |
class C: ... |
| 3024 |
if hasattr(C, "x"): |
| 3025 |
reveal_type(C.x) # N: Revealed type is "Any" |
| 3026 |
[builtins fixtures/isinstance.pyi] |
| 3027 |
|
| 3028 |
[case testHasAttrMissingAttributeTypeType] |
| 3029 |
# flags: --warn-unreachable |
| 3030 |
from typing import Type |
| 3031 |
class C: ... |
| 3032 |
c: Type[C] |
| 3033 |
if hasattr(c, "x"): |
| 3034 |
reveal_type(c.x) # N: Revealed type is "Any" |
| 3035 |
[builtins fixtures/isinstance.pyi] |
| 3036 |
|
| 3037 |
[case testHasAttrMissingAttributeTypeVar] |
| 3038 |
# flags: --warn-unreachable |
| 3039 |
from typing import TypeVar |
| 3040 |
|
| 3041 |
T = TypeVar("T") |
| 3042 |
def foo(x: T) -> T: |
| 3043 |
if hasattr(x, "x"): |
| 3044 |
reveal_type(x.x) # N: Revealed type is "Any" |
| 3045 |
return x |
| 3046 |
else: |
| 3047 |
return x |
| 3048 |
[builtins fixtures/isinstance.pyi] |
| 3049 |
|
| 3050 |
[case testHasAttrMissingAttributeChained] |
| 3051 |
# flags: --warn-unreachable |
| 3052 |
class B: ... |
| 3053 |
b: B |
| 3054 |
if hasattr(b, "x"): |
| 3055 |
reveal_type(b.x) # N: Revealed type is "Any" |
| 3056 |
elif hasattr(b, "y"): |
| 3057 |
reveal_type(b.y) # N: Revealed type is "Any" |
| 3058 |
[builtins fixtures/isinstance.pyi] |
| 3059 |
|
| 3060 |
[case testHasAttrMissingAttributeNested] |
| 3061 |
# flags: --warn-unreachable |
| 3062 |
class A: ... |
| 3063 |
class B: ... |
| 3064 |
|
| 3065 |
x: A |
| 3066 |
if hasattr(x, "x"): |
| 3067 |
if isinstance(x, B): |
| 3068 |
reveal_type(x.x) # N: Revealed type is "Any" |
| 3069 |
|
| 3070 |
if hasattr(x, "x") and hasattr(x, "y"): |
| 3071 |
reveal_type(x.x) # N: Revealed type is "Any" |
| 3072 |
reveal_type(x.y) # N: Revealed type is "Any" |
| 3073 |
|
| 3074 |
if hasattr(x, "x"): |
| 3075 |
if hasattr(x, "y"): |
| 3076 |
reveal_type(x.x) # N: Revealed type is "Any" |
| 3077 |
reveal_type(x.y) # N: Revealed type is "Any" |
| 3078 |
|
| 3079 |
if hasattr(x, "x") or hasattr(x, "y"): |
| 3080 |
x.x # E: "A" has no attribute "x" |
| 3081 |
x.y # E: "A" has no attribute "y" |
| 3082 |
[builtins fixtures/isinstance.pyi] |
| 3083 |
|
| 3084 |
[case testHasAttrPreciseType] |
| 3085 |
# flags: --warn-unreachable |
| 3086 |
class A: ... |
| 3087 |
|
| 3088 |
x: A |
| 3089 |
if hasattr(x, "a") and isinstance(x.a, int): |
| 3090 |
reveal_type(x.a) # N: Revealed type is "builtins.int" |
| 3091 |
[builtins fixtures/isinstance.pyi] |
| 3092 |
|
| 3093 |
[case testHasAttrMissingAttributeUnion] |
| 3094 |
# flags: --warn-unreachable |
| 3095 |
from typing import Union |
| 3096 |
|
| 3097 |
class A: ... |
| 3098 |
class B: |
| 3099 |
x: int |
| 3100 |
|
| 3101 |
xu: Union[A, B] |
| 3102 |
if hasattr(xu, "x"): |
| 3103 |
reveal_type(xu) # N: Revealed type is "__main__.A | __main__.B" |
| 3104 |
reveal_type(xu.x) # N: Revealed type is "Any | builtins.int" |
| 3105 |
else: |
| 3106 |
reveal_type(xu) # N: Revealed type is "__main__.A" |
| 3107 |
[builtins fixtures/isinstance.pyi] |
| 3108 |
|
| 3109 |
[case testHasAttrMissingAttributeOuterUnion] |
| 3110 |
# flags: --warn-unreachable |
| 3111 |
from typing import Union |
| 3112 |
|
| 3113 |
class A: ... |
| 3114 |
class B: ... |
| 3115 |
xu: Union[A, B] |
| 3116 |
if isinstance(xu, B): |
| 3117 |
if hasattr(xu, "x"): |
| 3118 |
reveal_type(xu.x) # N: Revealed type is "Any" |
| 3119 |
|
| 3120 |
if isinstance(xu, B) and hasattr(xu, "x"): |
| 3121 |
reveal_type(xu.x) # N: Revealed type is "Any" |
| 3122 |
[builtins fixtures/isinstance.pyi] |
| 3123 |
|
| 3124 |
[case testHasAttrDoesntInterfereGetAttr] |
| 3125 |
# flags: --warn-unreachable |
| 3126 |
class C: |
| 3127 |
def __getattr__(self, attr: str) -> str: ... |
| 3128 |
|
| 3129 |
c: C |
| 3130 |
if hasattr(c, "foo"): |
| 3131 |
reveal_type(c.foo) # N: Revealed type is "builtins.str" |
| 3132 |
[builtins fixtures/isinstance.pyi] |
| 3133 |
|
| 3134 |
[case testHasAttrMissingAttributeLiteral] |
| 3135 |
# flags: --warn-unreachable |
| 3136 |
from typing import Final |
| 3137 |
class B: ... |
| 3138 |
b: B |
| 3139 |
ATTR: Final = "x" |
| 3140 |
if hasattr(b, ATTR): |
| 3141 |
reveal_type(b.x) # N: Revealed type is "Any" |
| 3142 |
else: |
| 3143 |
b.x # E: "B" has no attribute "x" |
| 3144 |
[builtins fixtures/isinstance.pyi] |
| 3145 |
|
| 3146 |
[case testHasAttrDeferred] |
| 3147 |
# flags: --warn-unreachable |
| 3148 |
def foo() -> str: ... |
| 3149 |
|
| 3150 |
class Test: |
| 3151 |
def stream(self) -> None: |
| 3152 |
if hasattr(self, "_body"): |
| 3153 |
reveal_type(self._body) # N: Revealed type is "builtins.str" |
| 3154 |
|
| 3155 |
def body(self) -> str: |
| 3156 |
if not hasattr(self, "_body"): |
| 3157 |
self._body = foo() |
| 3158 |
return self._body |
| 3159 |
[builtins fixtures/isinstance.pyi] |
| 3160 |
|
| 3161 |
[case testHasAttrModule] |
| 3162 |
# flags: --warn-unreachable |
| 3163 |
import mod |
| 3164 |
|
| 3165 |
if hasattr(mod, "y"): |
| 3166 |
reveal_type(mod.y) # N: Revealed type is "Any" |
| 3167 |
reveal_type(mod.x) # N: Revealed type is "builtins.int" |
| 3168 |
else: |
| 3169 |
mod.y # E: Module has no attribute "y" |
| 3170 |
reveal_type(mod.x) # N: Revealed type is "builtins.int" |
| 3171 |
|
| 3172 |
if hasattr(mod, "x"): |
| 3173 |
mod.y # E: Module has no attribute "y" |
| 3174 |
reveal_type(mod.x) # N: Revealed type is "builtins.int" |
| 3175 |
else: |
| 3176 |
mod.y # E: Module has no attribute "y" |
| 3177 |
reveal_type(mod.x) # N: Revealed type is "builtins.int" |
| 3178 |
|
| 3179 |
[file mod.py] |
| 3180 |
x: int |
| 3181 |
[builtins fixtures/module.pyi] |
| 3182 |
|
| 3183 |
[case testHasAttrDoesntInterfereModuleGetAttr] |
| 3184 |
# flags: --warn-unreachable |
| 3185 |
import mod |
| 3186 |
|
| 3187 |
if hasattr(mod, "y"): |
| 3188 |
reveal_type(mod.y) # N: Revealed type is "builtins.str" |
| 3189 |
|
| 3190 |
[file mod.py] |
| 3191 |
def __getattr__(attr: str) -> str: ... |
| 3192 |
[builtins fixtures/module.pyi] |
| 3193 |
|
| 3194 |
[case testMultipleHasAttr] |
| 3195 |
# flags: --warn-unreachable |
| 3196 |
# https://github.com/python/mypy/issues/20596 |
| 3197 |
from __future__ import annotations |
| 3198 |
from typing import Any |
| 3199 |
|
| 3200 |
def len(obj: object) -> Any: ... |
| 3201 |
|
| 3202 |
def f(x: type | str): |
| 3203 |
if ( |
| 3204 |
hasattr(x, "__origin__") |
| 3205 |
and x.__origin__ is list |
| 3206 |
and hasattr(x, "__args__") |
| 3207 |
and len(x.__args__) == 1 |
| 3208 |
): |
| 3209 |
reveal_type(x.__args__[0]) # N: Revealed type is "Any" |
| 3210 |
[builtins fixtures/module.pyi] |
| 3211 |
|
| 3212 |
[case testTypeIsntLostAfterNarrowing] |
| 3213 |
# flags: --warn-unreachable |
| 3214 |
from typing import Any |
| 3215 |
|
| 3216 |
var: Any |
| 3217 |
reveal_type(var) # N: Revealed type is "Any" |
| 3218 |
assert isinstance(var, (bool, str)) |
| 3219 |
reveal_type(var) # N: Revealed type is "builtins.bool | builtins.str" |
| 3220 |
|
| 3221 |
if isinstance(var, bool): |
| 3222 |
reveal_type(var) # N: Revealed type is "builtins.bool" |
| 3223 |
|
| 3224 |
# Type of var shouldn't fall back to Any |
| 3225 |
reveal_type(var) # N: Revealed type is "builtins.bool | builtins.str" |
| 3226 |
[builtins fixtures/isinstance.pyi] |
| 3227 |
|
| 3228 |
[case testReuseIntersectionForRepeatedIsinstanceCalls] |
| 3229 |
# flags: --warn-unreachable |
| 3230 |
|
| 3231 |
class A: ... |
| 3232 |
class B: ... |
| 3233 |
|
| 3234 |
a: A |
| 3235 |
if isinstance(a, B): |
| 3236 |
c = a |
| 3237 |
if isinstance(a, B): |
| 3238 |
c = a |
| 3239 |
|
| 3240 |
[builtins fixtures/isinstance.pyi] |
| 3241 |
|
| 3242 |
[case testIsInstanceTypeAny] |
| 3243 |
# flags: --strict-equality --warn-unreachable |
| 3244 |
from typing import Any |
| 3245 |
|
| 3246 |
def foo(x: object, t: type[Any]): |
| 3247 |
if isinstance(x, t): |
| 3248 |
reveal_type(x) # N: Revealed type is "Any" |
| 3249 |
[builtins fixtures/isinstance.pyi] |
| 3250 |
|
| 3251 |
[case testIsInstanceObject] |
| 3252 |
# flags: --strict-equality --warn-unreachable |
| 3253 |
from typing import Any |
| 3254 |
|
| 3255 |
def foo(x: object, t: object): |
| 3256 |
if isinstance(x, t): # E: Argument 2 to "isinstance" has incompatible type "object"; expected "type[object] | tuple[type[object], ...]" |
| 3257 |
reveal_type(x) # N: Revealed type is "builtins.object" |
| 3258 |
[builtins fixtures/isinstance.pyi] |
| 3259 |
|
| 3260 |
[case testIsInstanceOrExprInTuple] |
| 3261 |
# flags: --strict-equality --warn-unreachable |
| 3262 |
from typing import Any |
| 3263 |
|
| 3264 |
def f1(x: object): |
| 3265 |
if isinstance(x, str | (int, dict)): # E: Argument 2 to "isinstance" has incompatible type "object"; expected "type | tuple[Any, ...]" |
| 3266 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 3267 |
if type(x) == str | (int, dict): |
| 3268 |
reveal_type(x) # N: Revealed type is "builtins.object" |
| 3269 |
|
| 3270 |
def f2(x: Any): |
| 3271 |
if isinstance(x, str | (int, dict)): # E: Argument 2 to "isinstance" has incompatible type "object"; expected "type | tuple[Any, ...]" |
| 3272 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 3273 |
if type(x) == str | (int, dict): |
| 3274 |
reveal_type(x) # N: Revealed type is "Any" |
| 3275 |
[builtins fixtures/primitives.pyi] |
| 3276 |
|
| 3277 |
[case testIsInstanceUnionOfTuples] |
| 3278 |
# flags: --strict-equality --warn-unreachable |
| 3279 |
from __future__ import annotations |
| 3280 |
from typing import TypeVar, Iterator, final |
| 3281 |
|
| 3282 |
T1 = TypeVar("T1") |
| 3283 |
T2 = TypeVar("T2") |
| 3284 |
T3 = TypeVar("T3") |
| 3285 |
|
| 3286 |
def extract( |
| 3287 |
values: object, |
| 3288 |
ts: ( |
| 3289 |
tuple[type[T1]] |
| 3290 |
| tuple[type[T1], type[T2]] |
| 3291 |
| tuple[type[T1], type[T2], type[T3]] |
| 3292 |
) |
| 3293 |
) -> Iterator[T1 | T2 | T3]: |
| 3294 |
if isinstance(values, ts): |
| 3295 |
reveal_type(values) # N: Revealed type is "T1`-1 | T2`-2 | T3`-3" |
| 3296 |
yield values |
| 3297 |
raise |
| 3298 |
|
| 3299 |
class A: ... |
| 3300 |
class B: ... |
| 3301 |
|
| 3302 |
def f1(x: A | B, t: tuple[type[A]] | tuple[type[B]]): |
| 3303 |
if isinstance(x, t): |
| 3304 |
reveal_type(x) # N: Revealed type is "__main__.A | __main__.B" |
| 3305 |
else: |
| 3306 |
reveal_type(x) # N: Revealed type is "__main__.A | __main__.B" |
| 3307 |
|
| 3308 |
def f2(x: object, t: tuple[type[A]] | tuple[type[B]]): |
| 3309 |
if isinstance(x, t): |
| 3310 |
reveal_type(x) # N: Revealed type is "__main__.A | __main__.B" |
| 3311 |
else: |
| 3312 |
reveal_type(x) # N: Revealed type is "builtins.object" |
| 3313 |
|
| 3314 |
@final |
| 3315 |
class FA: ... |
| 3316 |
@final |
| 3317 |
class FB: ... |
| 3318 |
|
| 3319 |
def g1(x: FA | FB, t: tuple[type[FA]] | tuple[type[FB]]): |
| 3320 |
if isinstance(x, t): |
| 3321 |
reveal_type(x) # N: Revealed type is "__main__.FA | __main__.FB" |
| 3322 |
else: |
| 3323 |
reveal_type(x) # N: Revealed type is "__main__.FA | __main__.FB" |
| 3324 |
|
| 3325 |
def g2(x: object, t: tuple[type[FA]] | tuple[type[FB]]): |
| 3326 |
if isinstance(x, t): |
| 3327 |
reveal_type(x) # N: Revealed type is "__main__.FA | __main__.FB" |
| 3328 |
else: |
| 3329 |
reveal_type(x) # N: Revealed type is "builtins.object" |
| 3330 |
[builtins fixtures/primitives.pyi] |
| 3331 |
|
| 3332 |
|
| 3333 |
[case testIsInstanceTypeVarBoundToType] |
| 3334 |
# flags: --strict-equality --warn-unreachable |
| 3335 |
from __future__ import annotations |
| 3336 |
from typing import TypeVar, Protocol |
| 3337 |
|
| 3338 |
class A(Protocol): |
| 3339 |
x: int |
| 3340 |
|
| 3341 |
T = TypeVar("T", bound=type[A]) |
| 3342 |
|
| 3343 |
def foo(x: object, t: T): |
| 3344 |
if isinstance(x, t): |
| 3345 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 3346 |
reveal_type(x.x) # N: Revealed type is "builtins.int" |
| 3347 |
[builtins fixtures/primitives.pyi] |
| 3348 |
|
| 3349 |
|
| 3350 |
[case testIsInstanceFinalTypeObject] |
| 3351 |
# flags: --strict-equality --warn-unreachable |
| 3352 |
from __future__ import annotations |
| 3353 |
from typing import final |
| 3354 |
|
| 3355 |
@final |
| 3356 |
class A: ... |
| 3357 |
|
| 3358 |
def f1(x: A, t: type[A]): |
| 3359 |
if isinstance(x, A): |
| 3360 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 3361 |
else: |
| 3362 |
reveal_type(x) # E: Statement is unreachable |
| 3363 |
|
| 3364 |
if isinstance(x, t): |
| 3365 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 3366 |
else: |
| 3367 |
reveal_type(x) # E: Statement is unreachable |
| 3368 |
|
| 3369 |
def f2(x: A | None, t: type[A]): |
| 3370 |
if isinstance(x, A): |
| 3371 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 3372 |
else: |
| 3373 |
reveal_type(x) # N: Revealed type is "None" |
| 3374 |
|
| 3375 |
if isinstance(x, t): |
| 3376 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 3377 |
else: |
| 3378 |
reveal_type(x) # N: Revealed type is "None" |
| 3379 |
[builtins fixtures/isinstancelist.pyi] |