mypy
mypy copied to clipboard
Variable with narrowed type does not get carried into nested function
Bug Report
Variables with narrowed types do not seem to be transferred into a nested function.
To Reproduce
from typing import Optional
def foo(value: Optional[int]) -> int:
if value is None:
return 0
def bar() -> int:
# value should be inferred to int here, but mypy detects it as Optional[int] and throws an error
return value
return value
def spam(value: Union[int, str]) -> str:
if isinstance(value, str):
def baz() -> str:
return value
return value
else:
def baz() -> int:
return value
return value
Expected Behavior
value should be narrowed in the nested function.
Actual Behavior
mypy raises errors in the nested function
Your Environment
Tested this on Windows in WSL with Python 3.8, and on Windows with Python 3.10
-
Mypy version used: 0.991
-
Mypy command-line flags: None
-
Mypy configuration options from
mypy.ini(and other config files): warn_no_return = true warn_redundant_casts = true warn_return_any = true warn_unreachable = true warn_unused_ignores = true ignore_missing_imports = true -
Python version used: 3.8 / 3.10