Closed as not planned
Description
Bug Report
mypy does not respect None check in nested functions defined after the check.
To Reproduce
Typecheck this code with Python 3.10 and mypy 0.971:
from typing import Optional
def foo(arg: Optional[int]) -> int:
reveal_type(arg)
if arg is None:
return 0
reveal_type(arg)
def sub() -> int:
reveal_type(arg)
return arg + 5
return sub()
Expected Behavior
Since arg
is checked against None
before sub()
is even defined, we can't get to that definition if arg
is None
Actual Behavior
mypy thinks that arg
is still Optional[int]
inside sub
body:
main.py:4: note: Revealed type is "Union[builtins.int, None]"
main.py:9: note: Revealed type is "builtins.int"
main.py:12: note: Revealed type is "Union[builtins.int, None]"
main.py:13: error: Unsupported operand types for + ("None" and "int")
main.py:13: note: Left operand is of type "Optional[int]"
Found 1 error in 1 file (checked 1 source file)
Compare messages for lines 9 and 12.
Your Environment
- Mypy version used: 0.971
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini
(and other config files): none - Python version used: 3.10
- Operating system and version: mypy-play playground, also on macOS with python 3.9 and mypy 0.961.
Pyright in VSCode gets it right: