| 1 |
-- Conditional type checks. |
| 2 |
|
| 3 |
|
| 4 |
[case testSimpleIsinstance] |
| 5 |
x: object |
| 6 |
n: int |
| 7 |
s: str |
| 8 |
if int(): |
| 9 |
n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int") |
| 10 |
if isinstance(x, int): |
| 11 |
n = x |
| 12 |
s = x # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 13 |
if int(): |
| 14 |
n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int") |
| 15 |
[builtins fixtures/isinstance.pyi] |
| 16 |
|
| 17 |
[case testSimpleIsinstance2] |
| 18 |
import typing |
| 19 |
def f(x: object, n: int, s: str) -> None: |
| 20 |
if int(): |
| 21 |
n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int") |
| 22 |
if isinstance(x, int): |
| 23 |
n = x |
| 24 |
s = x # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 25 |
n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int") |
| 26 |
[builtins fixtures/isinstance.pyi] |
| 27 |
[out] |
| 28 |
|
| 29 |
[case testSimpleIsinstance3] |
| 30 |
class A: |
| 31 |
x: object |
| 32 |
n: int |
| 33 |
s: str |
| 34 |
if int(): |
| 35 |
n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int") |
| 36 |
if isinstance(x, int): |
| 37 |
n = x |
| 38 |
s = x # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 39 |
else: |
| 40 |
n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int") |
| 41 |
[builtins fixtures/isinstance.pyi] |
| 42 |
[out] |
| 43 |
|
| 44 |
[case testMultipleIsinstanceTests] |
| 45 |
import typing |
| 46 |
class A: pass |
| 47 |
class B(A): pass |
| 48 |
def f(x: object, a: A, b: B, c: int) -> None: |
| 49 |
if isinstance(x, A): |
| 50 |
if isinstance(x, B): |
| 51 |
b = x |
| 52 |
x = a |
| 53 |
a = x |
| 54 |
c = x # E: Incompatible types in assignment (expression has type "A", variable has type "int") |
| 55 |
[builtins fixtures/isinstance.pyi] |
| 56 |
[out] |
| 57 |
|
| 58 |
[case testMultipleIsinstanceTests2] |
| 59 |
import typing |
| 60 |
class A: pass |
| 61 |
class B(A): pass |
| 62 |
def f(x: object, y: object, n: int, s: str) -> None: |
| 63 |
if isinstance(x, int): |
| 64 |
if isinstance(y, str): |
| 65 |
n = x |
| 66 |
s = y |
| 67 |
s = x # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 68 |
n = y # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 69 |
s = y # E: Incompatible types in assignment (expression has type "object", variable has type "str") |
| 70 |
n = y # E: Incompatible types in assignment (expression has type "object", variable has type "int") |
| 71 |
n = x |
| 72 |
[builtins fixtures/isinstance.pyi] |
| 73 |
[out] |
| 74 |
|
| 75 |
[case testIsinstanceAndElif] |
| 76 |
import typing |
| 77 |
def f(x: object, n: int, s: str) -> None: |
| 78 |
if int(): |
| 79 |
n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int") |
| 80 |
if isinstance(x, int): |
| 81 |
n = x |
| 82 |
s = x # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 83 |
elif isinstance(x, str): |
| 84 |
s = x |
| 85 |
n = x # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 86 |
else: |
| 87 |
n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int") |
| 88 |
s = x # E: Incompatible types in assignment (expression has type "object", variable has type "str") |
| 89 |
n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int") |
| 90 |
[builtins fixtures/isinstance.pyi] |
| 91 |
[out] |
| 92 |
|
| 93 |
[case testIsinstanceAndAnyType] |
| 94 |
from typing import Any |
| 95 |
def f(x: Any, n: int, s: str) -> None: |
| 96 |
if int(): |
| 97 |
s = x |
| 98 |
if isinstance(x, int): |
| 99 |
n = x |
| 100 |
s = x # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 101 |
s = x |
| 102 |
[builtins fixtures/isinstance.pyi] |
| 103 |
[out] |
| 104 |
|
| 105 |
[case testIsinstanceAndGenericType] |
| 106 |
from typing import TypeVar, Generic |
| 107 |
T = TypeVar('T') |
| 108 |
class C(Generic[T]): |
| 109 |
def f(self, x: T) -> None: pass |
| 110 |
def f(x: object) -> None: |
| 111 |
if isinstance(x, C): |
| 112 |
x.f(1) |
| 113 |
x.f('') |
| 114 |
x.g() # E: "C[Any]" has no attribute "g" |
| 115 |
x.g() # E: "object" has no attribute "g" |
| 116 |
[builtins fixtures/isinstance.pyi] |
| 117 |
[out] |