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

False positive used-before-assignment for type annotations that take assignment via nonlocal #5394

Closed
IRDonch opened this issue Nov 25, 2021 · 5 comments · Fixed by #6185
Closed
Labels
Bug 🪲 C: used-before-assignment Issues related to 'used-before-assignment' check Control flow Requires control flow understanding False Positive 🦟 A message is emitted but nothing is wrong with the code
Milestone

Comments

@IRDonch
Copy link

IRDonch commented Nov 25, 2021

Bug description

Create test.py with the following content:

def f():
    x: int

    def g():
        nonlocal x
        x = 5

    g()
    print(x)

f()

Configuration

No response

Command used

pylint test.py

Pylint output

************* Module test
test.py:9:10: E0602: Undefined variable 'x' (undefined-variable)

------------------------------------------------------------------
Your code has been rated at 3.75/10 (previous run: 3.75/10, +0.00)

Expected behavior

There should be no errors reported.

Pylint version

pylint 2.12.1
astroid 2.9.0
Python 3.6.9 (default, Jan 26 2021, 15:33:00)
[GCC 8.4.0]

OS / Environment

No response

Additional dependencies

No response

@IRDonch IRDonch added Bug 🪲 Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Nov 25, 2021
@IRDonch
Copy link
Author

IRDonch commented Nov 25, 2021

This is a regression in pylint 2.12. Probably caused by #5158.

@Pierre-Sassoulas Pierre-Sassoulas added False Positive 🦟 A message is emitted but nothing is wrong with the code and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Nov 25, 2021
@DanielNoord
Copy link
Collaborator

This might be difficult to solve as it requires control-flow knowledge. We can't do a simple check for a definition in an inner function as then this wouldn't be catched:

def f():
    x: int

    def g():
        nonlocal x
        x = 5

    print(x) # x is undefined
    g()

f()

For now I would advise to add an disable to this line. Sorry about that!

@DanielNoord DanielNoord added the Control flow Requires control flow understanding label Nov 25, 2021
@IRDonch
Copy link
Author

IRDonch commented Nov 25, 2021

I understand that it's undecidable in the general case. You could, however, err on the side of avoiding false positives by only emitting this error if the variable is not assigned in any scope.

@DanielNoord
Copy link
Collaborator

I don't remember the code by heart but I'll have a look in the near future. I'm not sure whether that approach would greatly increase the amount of false negatives, but I/we can at least try.

If somebody else would like to have a look before me, please go ahead!

@cdce8p cdce8p added the C: undefined-variable Issues related to 'undefined-variable' check label Nov 25, 2021
zhiltsov-max pushed a commit to openvinotoolkit/datumaro that referenced this issue Nov 25, 2021
@jacobtylerwalls jacobtylerwalls added this to the 2.13.5 milestone Apr 4, 2022
@jacobtylerwalls
Copy link
Member

On 2.13.x this emits used-before-assignment instead due to #5718

@jacobtylerwalls jacobtylerwalls added C: used-before-assignment Issues related to 'used-before-assignment' check and removed C: undefined-variable Issues related to 'undefined-variable' check labels Apr 5, 2022
@jacobtylerwalls jacobtylerwalls changed the title False positive undefined-variable when the variable is initialized in a nested scope False positive used-before-assignment when the variable is initialized in a nested scope Apr 5, 2022
@jacobtylerwalls jacobtylerwalls changed the title False positive used-before-assignment when the variable is initialized in a nested scope False positive used-before-assignment for type annotations that take assignment via nonlocal Apr 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 C: used-before-assignment Issues related to 'used-before-assignment' check Control flow Requires control flow understanding False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
5 participants