From 7c4ca2708341c24d30b8fa1b5c7215ea8731ad53 Mon Sep 17 00:00:00 2001 From: Jasha10 <8935917+Jasha10@users.noreply.github.com> Date: Fri, 21 Jan 2022 22:48:02 -0600 Subject: [PATCH] `builtins.filter` compat with `typing.TypeGuard` (#6726) This change enables the following use-case: ```python def is_not_none(x: Optional[int]) -> TypeGuard[int]: return x is not None list_optional: list[Optional[int]] = [0, None, 1, None, 2] generate_ints: Iterable[int] = filter(is_not_none, list_optional) ``` Co-authored-by: Alex Waygood --- stdlib/builtins.pyi | 2 ++ 1 file changed, 2 insertions(+) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 8abdf0afd..7155a5d80 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -1065,6 +1065,8 @@ class filter(Iterator[_T], Generic[_T]): @overload def __init__(self, __function: None, __iterable: Iterable[_T | None]) -> None: ... @overload + def __init__(self, __function: Callable[[_S], TypeGuard[_T]], __iterable: Iterable[_S]) -> None: ... + @overload def __init__(self, __function: Callable[[_T], Any], __iterable: Iterable[_T]) -> None: ... def __iter__(self) -> Iterator[_T]: ... def __next__(self) -> _T: ...