From b70f26b7c45d207ace1cec73defbd2e5ef9ec808 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 29 Feb 2024 07:37:45 -0800 Subject: [PATCH] PEP 742: Allow inferring the intersection type; carry over TypeGuard spec change (#3696) --- peps/pep-0742.rst | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/peps/pep-0742.rst b/peps/pep-0742.rst index aaea71b21..c558df77f 100644 --- a/peps/pep-0742.rst +++ b/peps/pep-0742.rst @@ -190,6 +190,8 @@ argument's previously-known type:: def f(x: Awaitable[int] | int) -> None: if isawaitable(x): + # Type checkers may also infer the more precise type + # "Awaitable[int] | (int & Awaitable[Any])" assert_type(x, Awaitable[int]) else: assert_type(x, int) @@ -204,11 +206,10 @@ It is an error to narrow to a type that is not consistent with the input type:: Subtyping --------- -``TypeIs`` is not a subtype of ``bool``. -The type ``Callable[..., TypeIs[int]]`` is not assignable to -``Callable[..., bool]`` or ``Callable[..., TypeGuard[int]]``, and vice versa. -This restriction is carried over from :pep:`647`. It may be possible to relax -it in the future, but that is outside the scope of this PEP. +``TypeIs`` is also valid as the return type of a callable, for example +in callback protocols and in the ``Callable`` special form. In these +contexts, it is treated as a subtype of bool. For example, ``Callable[..., TypeIs[int]]`` +is assignable to ``Callable[..., bool]``. Unlike ``TypeGuard``, ``TypeIs`` is invariant in its argument type: ``TypeIs[B]`` is not a subtype of ``TypeIs[A]``,