← Back to index

narrowing_typeis.py

True Positive
False Positive
False Negative
Optional (detected)
Warning or Info
TP: 9
FP: 0
FN: 0
Optional: 0 / 0
1"""
2Tests TypeIs functionality.
3"""
4
5# Specification: https://typing.readthedocs.io/en/latest/spec/narrowing.html#typeis
6
7from collections.abc import Awaitable
8from typing import Any, Callable, Protocol, Self, TypeGuard, TypeVar, assert_type
9from typing_extensions import TypeIs
12T = TypeVar("T")
14def is_two_element_tuple(val: tuple[T, ...]) -> TypeIs[tuple[T, T]]:
15 return len(val) == 2
17def func1(names: tuple[str, ...]):
18 if is_two_element_tuple(names):
19 assert_type(names, tuple[str, str])
20 else:
21 assert_type(names, tuple[str, ...])
24# > The final narrowed type may be narrower than **R**, due to the constraints of the
25# > argument's previously-known type
27def is_awaitable(val: object) -> TypeIs[Awaitable[Any]]:
28 return isinstance(val, Awaitable)
30async def func2(val: int | Awaitable[int]):
31 if is_awaitable(val):
32 # Not `assert_type(val, Awaitable[int])` because a conformant
33 # implementation could allow the possibility that instead it is
34 # `int & Awaitable[Any]`, an awaitable subclass of int.
35 x: int = await val
36 return x
37 else:
38 assert_type(val, int)
41T_A = TypeVar("T_A", bound="A")
43class A:
44 def tg_1(self, val: object) -> TypeIs[int]:
45 return isinstance(val, int)
47 @classmethod
48 def tg_2(cls, val: object) -> TypeIs[int]:
49 return isinstance(val, int)
51 @staticmethod
52 def tg_3(val: object) -> TypeIs[int]:
53 return isinstance(val, int)
55 def tg4(self, val: object) -> TypeIs[Self]:
56 return isinstance(val, type(self))
58 def tg5(self: T_A, val: object) -> TypeIs[T_A]:
59 return isinstance(val, type(self))
61class B(A):
62 pass
64# > The type narrowing behavior is applied to the first positional argument
65# > passed to the function. The function may accept additional arguments,
66# > but they are not affected by type narrowing.
69def func3() -> None:
70 val1 = object()
71 if A().tg_1(val1):
72 assert_type(val1, int)
74 val2 = object()
75 if A().tg_2(val2):
76 assert_type(val2, int)
78 val3 = object()
79 if A.tg_2(val3):
80 assert_type(val3, int)
82 val4 = object()
83 if A().tg_3(val4):
84 assert_type(val4, int)
86 val5 = object()
87 if A.tg_3(val5):
88 assert_type(val5, int)
90 val6 = object()
91 if B().tg4(val6):
92 assert_type(val6, B)
94 val7 = object()
95 if B().tg4(val7):
96 assert_type(val7, B)
99# > If a type narrowing function
100# > is implemented as an instance method or class method, the first positional
101# > argument maps to the second parameter (after self or cls).
103class C:
104 # Type checker should emit error here.
105 def tg_1(self) -> TypeIs[int]: # E
[invalid-type-guard-definition] `TypeIs` function must have a parameter to narrow
106 return False
108 @classmethod
109 # Type checker should emit error here.
110 def tg_2(cls) -> TypeIs[int]: # E
[invalid-type-guard-definition] `TypeIs` function must have a parameter to narrow
111 return False
113# > ``TypeIs`` is also valid as the return type of a callable, for example
114# > in callback protocols and in the ``Callable`` special form. In these
115# > contexts, it is treated as a subtype of bool. For example, ``Callable[..., TypeIs[int]]``
116# > is assignable to ``Callable[..., bool]``.
119def takes_callable_bool(f: Callable[[object], bool]) -> None:
120 pass
123def takes_callable_str(f: Callable[[object], str]) -> None:
124 pass
127def simple_typeguard(val: object) -> TypeIs[int]:
128 return isinstance(val, int)
131takes_callable_bool(simple_typeguard) # OK
132takes_callable_str(simple_typeguard) # E
[invalid-argument-type] Argument to function `takes_callable_str` is incorrect: Expected `(object, /) -> str`, found `def simple_typeguard(val: object) -> TypeIs[int]`
135class CallableBoolProto(Protocol):
136 def __call__(self, val: object) -> bool: ...
139class CallableStrProto(Protocol):
140 def __call__(self, val: object) -> str: ...
143def takes_callable_bool_proto(f: CallableBoolProto) -> None:
144 pass
147def takes_callable_str_proto(f: CallableStrProto) -> None:
148 pass
151takes_callable_bool_proto(simple_typeguard) # OK
152takes_callable_str_proto(simple_typeguard) # E
[invalid-argument-type] Argument to function `takes_callable_str_proto` is incorrect: Expected `CallableStrProto`, found `def simple_typeguard(val: object) -> TypeIs[int]`
154# TypeIs and TypeGuard are not compatible with each other.
156def takes_typeguard(f: Callable[[object], TypeGuard[int]]) -> None:
157 pass
159def takes_typeis(f: Callable[[object], TypeIs[int]]) -> None:
160 pass
162def is_int_typeis(val: object) -> TypeIs[int]:
163 return isinstance(val, int)
165def is_int_typeguard(val: object) -> TypeGuard[int]:
166 return isinstance(val, int)
168takes_typeguard(is_int_typeguard) # OK
169takes_typeguard(is_int_typeis) # E
[invalid-argument-type] Argument to function `takes_typeguard` is incorrect: Expected `(object, /) -> TypeGuard[int]`, found `def is_int_typeis(val: object) -> TypeIs[int]`
170takes_typeis(is_int_typeguard) # E
[invalid-argument-type] Argument to function `takes_typeis` is incorrect: Expected `(object, /) -> TypeIs[int]`, found `def is_int_typeguard(val: object) -> TypeGuard[int]`
171takes_typeis(is_int_typeis) # OK
174# > Unlike ``TypeGuard``, ``TypeIs`` is invariant in its argument type:
175# > ``TypeIs[B]`` is not a subtype of ``TypeIs[A]``,
176# > even if ``B`` is a subtype of ``A``.
178def takes_int_typeis(f: Callable[[object], TypeIs[int]]) -> None:
179 pass
182def int_typeis(val: object) -> TypeIs[int]:
183 return isinstance(val, int)
186def bool_typeis(val: object) -> TypeIs[bool]:
187 return isinstance(val, bool)
190takes_int_typeis(int_typeis) # OK
191takes_int_typeis(bool_typeis) # E
[invalid-argument-type] Argument to function `takes_int_typeis` is incorrect: Expected `(object, /) -> TypeIs[int]`, found `def bool_typeis(val: object) -> TypeIs[bool]`
193# > It is an error to narrow to a type that is not consistent with the input type
195def bad_typeis(x: int) -> TypeIs[str]: # E
[invalid-type-guard-definition] Narrowed type `str` is not assignable to the declared parameter type `int`
196 return isinstance(x, str)
199def bad_typeis_variance(x: list[object]) -> TypeIs[list[int]]: # E
[invalid-type-guard-definition] Narrowed type `list[int]` is not assignable to the declared parameter type `list[object]`
200 return all(isinstance(x, int) for x in x)