-- Test cases for type promotion (e.g. int -> float). [case testPromoteIntToFloat] def f(x: float) -> None: pass f(1) [builtins fixtures/primitives.pyi] [case testCantPromoteFloatToInt] def f(x: int) -> None: pass f(1.1) # E: Argument 1 to "f" has incompatible type "float"; expected "int" [builtins fixtures/primitives.pyi] [case testPromoteFloatToComplex] def f(x: complex) -> None: pass f(1) [builtins fixtures/primitives.pyi] [case testPromoteIntToComplex] def f(x: complex) -> None: pass f(1) [builtins fixtures/primitives.pyi] [case testPromoteBytearrayToByte] # flags: --no-strict-bytes def f(x: bytes) -> None: pass f(bytearray(b'')) [builtins fixtures/primitives.pyi] [case testPromoteMemoryviewToBytes] # flags: --no-strict-bytes def f(x: bytes) -> None: pass f(memoryview(b'')) [builtins fixtures/primitives.pyi] [case testDisableBytearrayMemoryviewPromotion] # flags: --strict-bytes --strict-equality --warn-unreachable def f(x: bytes) -> None: ... f(bytearray(b"asdf")) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes" f(memoryview(b"asdf")) # E: Argument 1 to "f" has incompatible type "memoryview"; expected "bytes" ba = bytearray(b"") if ba == b"": f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes" if b"" == ba: f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes" if ba == bytes(): f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes" if bytes() == ba: f(ba) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes" [builtins fixtures/primitives.pyi] [case testEnableBytearrayMemoryviewPromotion] # flags: --no-strict-bytes --strict-equality --warn-unreachable def f(x: bytes) -> None: ... f(bytearray(b"asdf")) f(memoryview(b"asdf")) ba = bytearray(b"") if ba == b"": f(ba) if b"" == ba: f(ba) if ba == bytes(): f(ba) if bytes() == ba: f(ba) [builtins fixtures/primitives.pyi] [case testDisableBytearrayMemoryviewPromotionStrictEquality] # flags: --strict-equality --strict-bytes def f(x: bytes, y: bytearray, z: memoryview) -> None: x == y y == z x == z 97 in x 97 in y 97 in z x in y x in z [builtins fixtures/primitives.pyi] [case testEnableBytearrayMemoryviewPromotionStrictEquality] # flags: --strict-equality --no-strict-bytes def f(x: bytes, y: bytearray, z: memoryview) -> None: x == y y == z x == z 97 in x 97 in y 97 in z x in y x in z [builtins fixtures/primitives.pyi] [case testNarrowingDownFromPromoteTargetType] y = 0.0 y = 1 y() # E: "int" not callable [builtins fixtures/primitives.pyi] [case testNarrowingDownFromPromoteTargetType2] y = 0.0 y = 1 y.x # E: "int" has no attribute "x" [builtins fixtures/primitives.pyi] [case testTypePromotionsDontInterfereWithProtocols] from typing import TypeVar, Union, Protocol class SupportsFloat(Protocol): def __float__(self) -> float: pass T = TypeVar('T') def f(x: Union[SupportsFloat, T]) -> Union[SupportsFloat, T]: pass f(0) # should not crash [builtins fixtures/primitives.pyi] [out] [case testIntersectionUsingPromotion1] # flags: --warn-unreachable from typing import Union x: complex = 1 reveal_type(x) # N: Revealed type is "builtins.complex" if isinstance(x, int): reveal_type(x) # N: Revealed type is "builtins.int" else: reveal_type(x) # N: Revealed type is "builtins.complex" reveal_type(x) # N: Revealed type is "builtins.complex" y: Union[int, float] if isinstance(y, float): reveal_type(y) # N: Revealed type is "builtins.float" else: reveal_type(y) # N: Revealed type is "builtins.int" reveal_type(y) # N: Revealed type is "builtins.int | builtins.float" if isinstance(y, int): reveal_type(y) # N: Revealed type is "builtins.int" else: reveal_type(y) # N: Revealed type is "builtins.float" [builtins fixtures/primitives.pyi] [case testIntersectionUsingPromotion2] # flags: --warn-unreachable x: complex = 1 reveal_type(x) # N: Revealed type is "builtins.complex" if isinstance(x, (int, float)): reveal_type(x) # N: Revealed type is "builtins.int | builtins.float" else: reveal_type(x) # N: Revealed type is "builtins.complex" # Note we make type precise, since type promotions are involved reveal_type(x) # N: Revealed type is "builtins.complex" [builtins fixtures/primitives.pyi] [case testIntersectionUsingPromotion3] # flags: --warn-unreachable x: object if isinstance(x, int) and isinstance(x, complex): reveal_type(x) # N: Revealed type is "builtins.int" if isinstance(x, complex) and isinstance(x, int): reveal_type(x) # N: Revealed type is "builtins.int" [builtins fixtures/primitives.pyi] [case testIntersectionUsingPromotion4] # flags: --warn-unreachable x: object if isinstance(x, int): if isinstance(x, complex): reveal_type(x) # N: Revealed type is "builtins.int" else: reveal_type(x) # N: Revealed type is "builtins.int" if isinstance(x, complex): if isinstance(x, int): reveal_type(x) # N: Revealed type is "builtins.int" else: reveal_type(x) # N: Revealed type is "builtins.complex" [builtins fixtures/primitives.pyi] [case testIntersectionUsingPromotion5] # flags: --warn-unreachable from typing import Union x: Union[float, complex] if isinstance(x, int): reveal_type(x) # N: Revealed type is "builtins.int" else: reveal_type(x) # N: Revealed type is "builtins.float | builtins.complex" reveal_type(x) # N: Revealed type is "builtins.float | builtins.complex" [builtins fixtures/primitives.pyi] [case testIntersectionUsingPromotion6] # flags: --warn-unreachable from typing import Union x: Union[str, complex] if isinstance(x, int): reveal_type(x) # N: Revealed type is "builtins.int" else: reveal_type(x) # N: Revealed type is "builtins.str | builtins.complex" reveal_type(x) # N: Revealed type is "builtins.str | builtins.complex" [builtins fixtures/primitives.pyi] [case testIntersectionUsingPromotion7] # flags: --warn-unreachable from typing import Union x: Union[int, float, complex] if isinstance(x, int): reveal_type(x) # N: Revealed type is "builtins.int" else: reveal_type(x) # N: Revealed type is "builtins.float | builtins.complex" reveal_type(x) # N: Revealed type is "builtins.int | builtins.float | builtins.complex" if isinstance(x, float): reveal_type(x) # N: Revealed type is "builtins.float" else: reveal_type(x) # N: Revealed type is "builtins.int | builtins.complex" reveal_type(x) # N: Revealed type is "builtins.int | builtins.float | builtins.complex" if isinstance(x, complex): reveal_type(x) # N: Revealed type is "builtins.complex" else: reveal_type(x) # N: Revealed type is "builtins.int | builtins.float" reveal_type(x) # N: Revealed type is "builtins.int | builtins.float | builtins.complex" [builtins fixtures/primitives.pyi] [case testIntersectionUsingPromotion8] # flags: --warn-unreachable from typing import Union x: Union[int, float, complex] if isinstance(x, (int, float)): reveal_type(x) # N: Revealed type is "builtins.int | builtins.float" else: reveal_type(x) # N: Revealed type is "builtins.complex" if isinstance(x, (int, complex)): reveal_type(x) # N: Revealed type is "builtins.int | builtins.complex" else: reveal_type(x) # N: Revealed type is "builtins.float" if isinstance(x, (float, complex)): reveal_type(x) # N: Revealed type is "builtins.float | builtins.complex" else: reveal_type(x) # N: Revealed type is "builtins.int" [builtins fixtures/primitives.pyi] [case testRejectPromotionsForProtocols] from typing import Protocol class H(Protocol): def hex(self, /) -> str: ... f: H = 1.0 o: H = object() # E: Incompatible types in assignment (expression has type "object", variable has type "H") c: H = 1j # E: Incompatible types in assignment (expression has type "complex", variable has type "H") i: H = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "H") b: H = False # E: Incompatible types in assignment (expression has type "bool", variable has type "H") class N(float): ... n: H = N() [builtins fixtures/primitives.pyi]