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

don't narrow locals if there is a nonlocal reference to it #976

Open
KotlinIsland opened this issue Jan 4, 2025 · 0 comments
Open

don't narrow locals if there is a nonlocal reference to it #976

KotlinIsland opened this issue Jan 4, 2025 · 0 comments
Labels
type checking / linting issues relating to existing diagnostic rules or proposals for new diagnostic rules

Comments

@KotlinIsland
Copy link
Collaborator

KotlinIsland commented Jan 4, 2025

playground

def f():
    def m():
        nonlocal a
        a += 1
    a = 1
    m()
    if a == 2: # error 1 != 2
        print("hmm")     

here, the nonlocal a is a direct indicator that a will be mutated and should therefore not be narrowed:

def f():
    def m():
        nonlocal a
        a += 1
    a = 1
    reveal_type(a)  # int
    m()
    if a == 2: # no error
        print("hmm")     

in a more complex analysis, we could only apply these widenings after the appropriate function (m) is invoked, this sounds plausible to do, but a lot more work

@KotlinIsland KotlinIsland added the type checking / linting issues relating to existing diagnostic rules or proposals for new diagnostic rules label Jan 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type checking / linting issues relating to existing diagnostic rules or proposals for new diagnostic rules
Projects
None yet
Development

No branches or pull requests

1 participant