Skip to content

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

Open
@JelleZijlstra

Description

@JelleZijlstra
$ 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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions