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

Mypy issue with python scoping rule #8746

Open
h8hawk opened this issue Apr 29, 2020 · 2 comments
Open

Mypy issue with python scoping rule #8746

h8hawk opened this issue Apr 29, 2020 · 2 comments
Labels
bug mypy got something wrong priority-1-normal topic-runtime-semantics mypy doesn't model runtime semantics correctly topic-variable-scope

Comments

@h8hawk
Copy link

h8hawk commented Apr 29, 2020

Mypy pass following code as valid:

def now() -> str:
    return "now"


def usage() -> None:
    now = now()
    print(now + "!")


usage()

Snippet will raise UnboundLocalError error.

@JukkaL JukkaL added bug mypy got something wrong priority-1-normal labels May 1, 2020
@JukkaL
Copy link
Collaborator

JukkaL commented May 1, 2020

It would be nice if mypy would catch this error.

@ehudhala
Copy link

ehudhala commented May 9, 2020

Hey, I started working on an implementation but ran into many edge cases.
After researching, it seems this behavior has been discussed previously and it was decided not to implement it (#2400 ).

I would like to add that semantic analysis currently doesn't know where in the scope a symbol is defined, so even a simple case like the following passes mypy successfully:

def func() -> None:
    assigned = unassigned + 1
    unassigned: int = 0

Another interesting behavior is that it confuses the type-checker somewhere down the road (I haven't looked into that yet) and it can't determine the type of an unassigned variable, so the following error is produced:

def func() -> None:
    assigned = unassigned + 1
    unassigned = 0
[out]
a.py:2: error: Cannot determine type of 'unassigned'

@JukkaL - how would you suggest we proceed from here?

@JelleZijlstra JelleZijlstra changed the title Mypy issue with python scopping rule Mypy issue with python scoping rule May 9, 2020
@AlexWaygood AlexWaygood added feature bug mypy got something wrong topic-runtime-semantics mypy doesn't model runtime semantics correctly topic-variable-scope and removed bug mypy got something wrong feature labels Mar 27, 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 priority-1-normal topic-runtime-semantics mypy doesn't model runtime semantics correctly topic-variable-scope
Projects
None yet
Development

No branches or pull requests

4 participants