Skip to content

Commit

Permalink
builtins.filter compat with typing.TypeGuard (#6726)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Jasha10 and AlexWaygood authored Jan 22, 2022
1 parent 2bf7458 commit 7c4ca27
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Expand Down

0 comments on commit 7c4ca27

Please sign in to comment.