Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type overload with filter(None, iter) #125

Closed
dishang-le opened this issue Feb 2, 2024 · 1 comment · Fixed by #127
Closed

Add type overload with filter(None, iter) #125

dishang-le opened this issue Feb 2, 2024 · 1 comment · Fixed by #127
Labels
typing bug Some type hints aren't working

Comments

@dishang-le
Copy link

The built-in sync filter has a overload type hint to infer that the return type of filter(None, iter) is not None, can you add similar type overload?

e.g.

maybe_none: Iterable[int | None] = [1, 2, None]
not_none: Iterable[int] = filter(None, maybe_none) # it knows not_none is type Iterable[int] without cast
@maxfischer2781 maxfischer2781 added the typing bug Some type hints aren't working label Feb 5, 2024
@maxfischer2781
Copy link
Owner

Indeed the typeshed annotations for filter handle more cases. Good catch, thanks for the report.

class filter(Iterator[_T]):
    @overload
    def __new__(cls, __function: None, __iterable: Iterable[_T | None]) -> Self: ...
    @overload
    def __new__(cls, __function: Callable[[_S], TypeGuard[_T]], __iterable: Iterable[_S]) -> Self: ...
    @overload
    def __new__(cls, __function: Callable[[_T], Any], __iterable: Iterable[_T]) -> Self: ...
    def __iter__(self) -> Self: ...
    def __next__(self) -> _T: ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typing bug Some type hints aren't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants