Skip to content

False positive undefined-variable for variable in inner scope matching outer scope type annotation #5326

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

Closed
jacobtylerwalls opened this issue Nov 16, 2021 · 4 comments · Fixed by #5651
Labels
C: undefined-variable Issues related to 'undefined-variable' check False Positive 🦟 A message is emitted but nothing is wrong with the code
Milestone

Comments

@jacobtylerwalls
Copy link
Member

Bug description

def func():
    a: bool
    _ = [print(kwarg1=a, kwarg2=a) for a in range(10)]

Configuration

No response

Command used

pylint a.py

Pylint output

a.py:3: [E0602(undefined-variable), func] Undefined variable 'a'

Expected behavior

No messages

Pylint version

pylint 2.11.2-dev0
astroid 2.8.5
Python 3.9.6 (v3.9.6:db3ff76da1, Jun 28 2021, 11:49:53)

Does NOT occur on 2.11.1, possible regression in dev version?

OS / Environment

No response

Additional dependencies

No response

@jacobtylerwalls jacobtylerwalls added Bug 🪲 Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Nov 16, 2021
@Pierre-Sassoulas Pierre-Sassoulas removed the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Nov 16, 2021
@jacobtylerwalls
Copy link
Member Author

After bisecting, the change occurred in b1c4735. Reading that change, it seems undefined-variable is the new intended behavior, but it should be raised on line 2, not line 3. And if only one kwarg involves the variable, then unused-variable is emitted instead, which I suppose is a false negative for undefined-variable.

@jacobtylerwalls jacobtylerwalls changed the title False positive undefined-variable for variable in inner scope matching outer scope type annotation False positive undefined-variable for variable in inner scope matching unused outer scope type annotation Nov 16, 2021
@cdce8p cdce8p added C: undefined-variable Issues related to 'undefined-variable' check False Negative 🦋 No message is emitted but something is wrong with the code False Positive 🦟 A message is emitted but nothing is wrong with the code and removed Bug 🪲 False Negative 🦋 No message is emitted but something is wrong with the code labels Nov 17, 2021
@IceTDrinker
Copy link

IceTDrinker commented Nov 30, 2021

hello to pop in and say we have that issue but with the annotated outer scope variable name being used/assigned to later

Basically something like:

node: MyNode  # triggers the undefined-variable which is hard to comprehend
list_of_nodes = [node for node in maybe_node_list if isinstance(node, MyNode)]

for node in other_list_of_node:
    pass

@IceTDrinker
Copy link

IceTDrinker commented Nov 30, 2021

This also fails on the first line

list_of_nodes = [node for node in maybe_node_list if isinstance(node, MyNode)]

node: MyNode

for node in other_list_of_node:
    pass

Edit: feels like it should not, we'll be freezing our pylint dependency to the previous version until this is fixed as it prevents from having proper typing for mypy and sensible code in those cases

@jacobtylerwalls jacobtylerwalls changed the title False positive undefined-variable for variable in inner scope matching unused outer scope type annotation False positive undefined-variable for variable in inner scope matching outer scope type annotation Jan 8, 2022
jacobtylerwalls added a commit to jacobtylerwalls/pylint that referenced this issue Jan 8, 2022
@jacobtylerwalls
Copy link
Member Author

jacobtylerwalls commented Jan 8, 2022

I broke the false negative out into #5654. That means this ticket is now just a false positive with a PR to close it.

jacobtylerwalls added a commit to jacobtylerwalls/pylint that referenced this issue Jan 8, 2022
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.13.0 milestone Jan 9, 2022
Pierre-Sassoulas pushed a commit that referenced this issue Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: undefined-variable Issues related to 'undefined-variable' check False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
4 participants