You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
fromtypingimportOptionaldefmaybe_int() ->Optional[int]:
return123# or maybe Noneforniniter(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 not infer that n is of type int (not Optional[int]) and should not report any errors about adding int and None values.
The text was updated successfully, but these errors were encountered:
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 not 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: