Skip to content
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

New semanal: Spurious "no binding for nonlocal" #7057

Open
JelleZijlstra opened this issue Jun 25, 2019 · 1 comment
Open

New semanal: Spurious "no binding for nonlocal" #7057

JelleZijlstra opened this issue Jun 25, 2019 · 1 comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal semantic-analyzer Problems that happen during semantic analysis topic-runtime-semantics mypy doesn't model runtime semantics correctly topic-variable-scope

Comments

@JelleZijlstra
Copy link
Member

$ cat santa/scheduler/loop.py 
import signal
from typing import Any


def loop() -> None:
    def signal_handler(signum: Any, frame: Any) -> None:
        nonlocal running
        running = False

    signal.signal(signal.SIGINT, signal_handler)
    signal.signal(signal.SIGTERM, signal_handler)

    running = True
    while running:
        pass
$ mypy --new-semantic-analyzer santa/scheduler/loop.py 
santa/scheduler/loop.py:7: error: No binding for nonlocal 'running' found

This code passes under the old semantic analyzer. The error goes away if I move the running = True line above the definition of the nested function.

It doesn't seem unreasonable to me to require that the definition of a variable is lexically before its use as a nonlocal, so maybe this doesn't need to be fixed.

@JelleZijlstra JelleZijlstra added the semantic-analyzer Problems that happen during semantic analysis label Jun 25, 2019
@JukkaL JukkaL added bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal labels Jun 25, 2019
@JukkaL
Copy link
Collaborator

JukkaL commented Jun 25, 2019

I'm marking this as a bug since it's a regression. I agree that this doesn't seem very high priority, but perhaps still worth fixing if the fix is easy enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal semantic-analyzer Problems that happen during semantic analysis topic-runtime-semantics mypy doesn't model runtime semantics correctly topic-variable-scope
Projects
None yet
Development

No branches or pull requests

3 participants