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 <Alex.Waygood@Gmail.com>
This commit is contained in:
Jasha10
2022-01-21 22:48:02 -06:00
committed by GitHub
parent 2bf7458091
commit 7c4ca27083

View File

@@ -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: ...