8000 two-arg iter() with None sentinel does not remove ‘Optional’ · Issue #3201 · python/typeshed · GitHub
[go: up one dir, main page]

Skip to content

two-arg iter() with None sentinel does not remove ‘Optional’ #3201

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

Closed
wbolster-eiq opened this issue Aug 20, 2019 · 2 comments · Fixed by #3291
Closed

two-arg iter() with None sentinel does not remove ‘Optional’ #3201

wbolster-eiq opened this issue Aug 20, 2019 · 2 comments · Fixed by #3291
Labels
stubs: false positive Type checkers report false errors

Comments

@wbolster-eiq
Copy link
wbolster-eiq commented Aug 20, 2019

[moved from https://github.com/python/mypy/issues/7371]

The 2-arg form of iter() takes a callable and a sentinel value. Quoting its docstring:

iter(callable, sentinel) -> iterator

[...], the callable is called until it returns the sentinel.

When the sentinel is the literal None, this means that a callable that returns an Optional[T] is transformed into a Iterator[T]. However, mypy does not detect that, and reveal_type on the iter() result claims its Revealed type is 'typing.Iterator[Union[T, None]]', which allows for None values, which is impossible.

Here's a small example:

from typing import Optional

def maybe_int() -> Optional[int]:
    return 123  # or maybe None

for n in iter(maybe_int, None):
    print(n + n)

Even though n is always an int inside the loop (never None), mypy (version 0.720) is not aware of that, and complains:

error: Unsupported operand types for + ("int" and "None")
error: Unsupported operand types for + ("None" and "int")
error: Unsupported left operand type for + ("None")
Both left and right operands are unions

Since the code is perfectly safe, mypy should infer that n is of type int (not Optional[int]) and should not report any errors about adding int and None values.

@wbolster-eiq
Copy link
Author
wbolster-eiq commented Aug 20, 2019

current definition:

@overload
def iter(__function: Callable[[], _T], __sentinel: _T) -> Iterator[_T]: ...

@srittau
Copy link
Collaborator
srittau commented Aug 20, 2019

Changing the overload to use two type vars could work, but I haven't tested it:

@overload
def iter(__function: Callable[[], Union[_T, _U]], __sentinel: _U) -> Iterator[_T]: ...

@srittau srittau added size-small stubs: false positive Type checkers report false errors labels Aug 20, 2019
utkarsh2102 added a commit to utkarsh2102/typeshed that referenced this issue Oct 1, 2019
utkarsh2102 added a commit to utkarsh2102/typeshed that referenced this issue Oct 1, 2019
utkarsh2102 added a commit to utkarsh2102/typeshed that referenced this issue Oct 3, 2019
srittau pushed a commit that referenced this issue Oct 3, 2019
srittau pushed a commit that referenced this issue Oct 9, 2019
)

This is a continuation of #3291, which was the initial fix for #3201.

The 2-arg version of iter() turns a callable into an iterator. The
changes made in #3291 introduce an Any return type for both the
callable's return type and the iterator's type, while in reality the
return type of the function is always the same as the iterator's type.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stubs: false positive Type checkers report false errors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants
0