-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Labels
stubs: false positive
Type checkers report false errors
Comments
current definition: @overload
def iter(__function: Callable[[], _T], __sentinel: _T) -> Iterator[_T]: ... |
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]: ... |
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
[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:When the sentinel is the literal
None
, this means that a callable that returns anOptional[T]
is transformed into aIterator[T]
. However, mypy does not detect that, andreveal_type
on theiter()
result claims itsRevealed type is 'typing.Iterator[Union[T, None]]'
, which allows forNone
values, which is impossible.Here's a small example:
Even though
n
is always anint
inside the loop (neverNone
), mypy (version 0.720) is not aware of that, and complains:Since the code is perfectly safe, mypy should infer that
n
is of typeint
(notOptional[int]
) and should not report any errors about addingint
andNone
values.The text was updated successfully, but these errors were encountered: