Skip to content

None check is not respected in nested function #13432

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

Closed
maksbotan opened this issue Aug 16, 2022 · 1 comment
Closed

None check is not respected in nested function #13432

maksbotan opened this issue Aug 16, 2022 · 1 comment
Labels
bug mypy got something wrong

Comments

@maksbotan
Copy link
Contributor

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:

image

@maksbotan maksbotan added the bug mypy got something wrong label Aug 16, 2022
@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Aug 16, 2022

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Aug 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants