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
MyPy can lose type information in an inner function deduced in an outer function.
Consider the following sample code:
from typing import Callable, Optional
def f(x: Optional[int]) -> Callable[[], int]:
if x is None:
x = 0
reveal_type(x)
def g() -> int:
reveal_type(x)
return x + 1
return g
I would expect that MyPy accept this code without error, and for both reveal_type calls to return builtins.int. Running python -m mypy minimal.py we get
minimal.py:7: error: Revealed type is 'builtins.int'
minimal.py:10: error: Revealed type is 'Union[builtins.int, None]'
minimal.py:11: error: Unsupported operand types for + ("None" and "int")
minimal.py:11: note: Left operand is of type "Optional[int]"
The first reveal_type shows MyPy has correctly narrowed Optional[int] to int. However, inside the inner function we see that MyPy forgets this narrowing.
This was run using Python 3.6.0 and MyPy 0.660. The error persisted when running MyPy from the current master branch, currently hash f03ad7094e7a9bd174011e2ae3bc0c7204c95e22.
The text was updated successfully, but these errors were encountered:
MyPy can lose type information in an inner function deduced in an outer function.
Consider the following sample code:
I would expect that MyPy accept this code without error, and for both
reveal_type
calls to returnbuiltins.int
. Runningpython -m mypy minimal.py
we getThe first
reveal_type
shows MyPy has correctly narrowedOptional[int]
toint
. However, inside the inner function we see that MyPy forgets this narrowing.This was run using Python 3.6.0 and MyPy 0.660. The error persisted when running MyPy from the current
master
branch, currently hashf03ad7094e7a9bd174011e2ae3bc0c7204c95e22
.The text was updated successfully, but these errors were encountered: