2Tests TypeIs functionality.
5# Specification: https://typing.readthedocs.io/en/latest/spec/narrowing.html#typeis
7from collections.abc import Awaitable
8from typing import Any, Callable, Protocol, Self, TypeGuard, TypeVar, assert_type
9from typing_extensions import TypeIs
14def is_two_element_tuple(val: tuple[T, ...]) -> TypeIs[tuple[T, T]]:
17def func1(names: tuple[str, ...]):
18 if is_two_element_tuple(names):
19 assert_type(names, tuple[str, str])
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]):
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.
41T_A = TypeVar("T_A", bound="A")
44 def tg_1(self, val: object) -> TypeIs[int]:
45 return isinstance(val, int)
48 def tg_2(cls, val: object) -> TypeIs[int]:
49 return isinstance(val, int)
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))
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.
72 assert_type(val1, int)
76 assert_type(val2, int)
80 assert_type(val3, int)
84 assert_type(val4, int)
88 assert_type(val5, int)
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).
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
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
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:
123def takes_callable_str(f: Callable[[object], str]) -> None:
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:
147def takes_callable_str_proto(f: CallableStrProto) -> None:
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:
159def takes_typeis(f: Callable[[object], TypeIs[int]]) -> None:
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:
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)