| -- Conditional type checks. |
| |
| |
| [case testSimpleIsinstance] |
| x = None # type: object |
| n = None # type: int |
| s = None # type: str |
| if int(): |
| n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int") |
| if isinstance(x, int): |
| n = x |
| s = x # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| if int(): |
| n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int") |
| [builtins fixtures/isinstance.pyi] |
| |
| [case testSimpleIsinstance2] |
| import typing |
| def f(x: object, n: int, s: str) -> None: |
| if int(): |
| n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int") |
| if isinstance(x, int): |
| n = x |
| s = x # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int") |
| [builtins fixtures/isinstance.pyi] |
| [out] |
| |
| [case testSimpleIsinstance3] |
| class A: |
| x = None # type: object |
| n = None # type: int |
| s = None # type: str |
| if int(): |
| n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int") |
| if isinstance(x, int): |
| n = x |
| s = x # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| else: |
| n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int") |
| [builtins fixtures/isinstance.pyi] |
| [out] |
| |
| [case testMultipleIsinstanceTests] |
| import typing |
| class A: pass |
| class B(A): pass |
| def f(x: object, a: A, b: B, c: int) -> None: |
| if isinstance(x, A): |
| if isinstance(x, B): |
| b = x |
| x = a |
| a = x |
| c = x # E: Incompatible types in assignment (expression has type "A", variable has type "int") |
| [builtins fixtures/isinstance.pyi] |
| [out] |
| |
| [case testMultipleIsinstanceTests2] |
| import typing |
| class A: pass |
| class B(A): pass |
| def f(x: object, y: object, n: int, s: str) -> None: |
| if isinstance(x, int): |
| if isinstance(y, str): |
| n = x |
| s = y |
| s = x # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| n = y # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| s = y # E: Incompatible types in assignment (expression has type "object", variable has type "str") |
| n = y # E: Incompatible types in assignment (expression has type "object", variable has type "int") |
| n = x |
| [builtins fixtures/isinstance.pyi] |
| [out] |
| |
| [case testIsinstanceAndElif] |
| import typing |
| def f(x: object, n: int, s: str) -> None: |
| if int(): |
| n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int") |
| if isinstance(x, int): |
| n = x |
| s = x # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| elif isinstance(x, str): |
| s = x |
| n = x # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| else: |
| n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int") |
| s = x # E: Incompatible types in assignment (expression has type "object", variable has type "str") |
| n = x # E: Incompatible types in assignment (expression has type "object", variable has type "int") |
| [builtins fixtures/isinstance.pyi] |
| [out] |
| |
| [case testIsinstanceAndAnyType] |
| from typing import Any |
| def f(x: Any, n: int, s: str) -> None: |
| if int(): |
| s = x |
| if isinstance(x, int): |
| n = x |
| s = x # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| s = x |
| [builtins fixtures/isinstance.pyi] |
| [out] |
| |
| [case testIsinstanceAndGenericType] |
| from typing import TypeVar, Generic |
| T = TypeVar('T') |
| class C(Generic[T]): |
| def f(self, x: T) -> None: pass |
| def f(x: object) -> None: |
| if isinstance(x, C): |
| x.f(1) |
| x.f('') |
| x.g() # E: "C[Any]" has no attribute "g" |
| x.g() # E: "object" has no attribute "g" |
| [builtins fixtures/isinstance.pyi] |
| [out] |