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 undefined-loop-variable with pylint 2.5.3 #3711

Closed
lukasoyen opened this issue Jun 26, 2020 · 1 comment · Fixed by pylint-dev/astroid#1111 or #4777
Closed

False positive undefined-loop-variable with pylint 2.5.3 #3711

lukasoyen opened this issue Jun 26, 2020 · 1 comment · Fixed by pylint-dev/astroid#1111 or #4777
Labels

Comments

@lukasoyen
Copy link

Steps to reproduce

  1. Save code as false-positive.py
  2. Run python3 -m pylint --disable=all --enable=undefined-loop-variable false-positive.py
def test(content):
    def handle_line(line):
        if "X" in line:
            line = line.replace("X", "Y")
        elif "Y" in line:  # line 5
            line = '{}'.format(line)
        elif "Z" in line:  # line 7
            line = f'{line}'
        else:
            line = '%s' % line  # line 10

    for line in content.split('\n'):
        handle_line(line)

Current behavior

This prints:

false-positive.py:5:20: W0631: Using possibly undefined loop variable 'line' (undefined-loop-variable)
false-positive.py:7:20: W0631: Using possibly undefined loop variable 'line' (undefined-loop-variable)
false-positive.py:10:26: W0631: Using possibly undefined loop variable 'line' (undefined-loop-variable)

Expected behavior

No undefined-loop-variable violations.

pylint --version output

pylint 2.5.3
astroid 2.4.2
Python 3.7.7 (default, Mar 10 2020, 06:34:06) 
[GCC 9.3.0]
@bodograumann
Copy link

I am seeing a similar issue here:

lst = []
lst2 = [1, 2, 3]

for item in lst:
    pass

bigger = [
    [
        x for x in lst2 if x > item
    ]
    for item in lst
]

pylint complains:

pylint-test.py:9:31: W0631: Using possibly undefined loop variable 'item' (undefined-loop-variable)

Removing the first for loop makes it vanish.
When pylint determines that lst definitely contains items, the warning also disappears, but it does not apply even for an empty lst, because then the outer list comprehension is simply empty as well.

Pierre-Sassoulas added a commit to Pierre-Sassoulas/pylint that referenced this issue Jul 19, 2021
Pierre-Sassoulas added a commit to Pierre-Sassoulas/pylint that referenced this issue Jul 19, 2021
Pierre-Sassoulas added a commit that referenced this issue Jul 19, 2021
Pierre-Sassoulas added a commit to Pierre-Sassoulas/pylint that referenced this issue Jul 30, 2021
Pierre-Sassoulas added a commit to Pierre-Sassoulas/pylint that referenced this issue Jul 30, 2021
Pierre-Sassoulas added a commit to Pierre-Sassoulas/pylint that referenced this issue Jul 31, 2021
Pierre-Sassoulas added a commit to Pierre-Sassoulas/pylint that referenced this issue Aug 1, 2021
Pierre-Sassoulas added a commit to Pierre-Sassoulas/pylint that referenced this issue Aug 3, 2021
Pierre-Sassoulas added a commit to Pierre-Sassoulas/pylint that referenced this issue Aug 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants