From c0a92c3af274ee7cd9edbc5c3f5cfb1150266a18 Mon Sep 17 00:00:00 2001 From: Jasha10 <8935917+Jasha10@users.noreply.github.com> Date: Tue, 28 Dec 2021 15:27:32 -0600 Subject: [PATCH 1/2] `builtins.filter` compat with `typing.TypeGuard` 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) ``` --- stdlib/builtins.pyi | 2 ++ 1 file changed, 2 insertions(+) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index cf5799238b76..12a0441553a0 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -1052,6 +1052,8 @@ class filter(Iterator[_T], Generic[_T]): def __init__(self, __function: None, __iterable: Iterable[_T | None]) -> None: ... @overload def __init__(self, __function: Callable[[_T], Any], __iterable: Iterable[_T]) -> None: ... + @overload + def __init__(self, __function: Callable[[_S], TypeGuard[_T]], __iterable: Iterable[_S]) -> None: ... def __iter__(self) -> Iterator[_T]: ... def __next__(self) -> _T: ... From 47fd973baece2d96a77dbdf7bbf789bb4a203f9f Mon Sep 17 00:00:00 2001 From: Jasha10 <8935917+Jasha10@users.noreply.github.com> Date: Wed, 29 Dec 2021 19:06:11 -0600 Subject: [PATCH 2/2] Update stdlib/builtins.pyi Co-authored-by: Alex Waygood --- stdlib/builtins.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 12a0441553a0..3769aecf30f3 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -1051,9 +1051,9 @@ class filter(Iterator[_T], Generic[_T]): @overload def __init__(self, __function: None, __iterable: Iterable[_T | None]) -> None: ... @overload - def __init__(self, __function: Callable[[_T], Any], __iterable: Iterable[_T]) -> 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: ...