-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
used-before-assignment
false positive with list comprehension and try/except
#6035
Comments
Waiting for next release for some false-positives to be fixed. Especially, `used-before-assignment` and `unnecessary-ellipsis`. See pylint-dev/pylint#6035.
in the spirit of possibly being helpful: somewhat similar list comprehension statement, but not in a try-except block if isinstance(d, dict):
dict_keys = [c for c in d if isinstance(d[c], dict)] E:191,59: Using variable 'c' before assignment (used-before-assignment) pylint 2.13.3, pytest-pylint 0.18.0, python 3.8.13, pytest-7.1.1 |
I can't reproduce with that example: $ cat a.py
if isinstance(d, dict):
dict_keys = [c for c in d if isinstance(d[c], dict)]
$ python3 -m pylint a.py
************* Module a
a.py:1:14: E0602: Undefined variable 'd' (undefined-variable)
a.py:2:29: E0602: Undefined variable 'd' (undefined-variable)
a.py:2:45: E0602: Undefined variable 'd' (undefined-variable) |
my apologies @jacobtylerwalls - my example wasn't meant to be complete, just the minimal snippet showing the statement. My immediate take-away: any pytest-pylint reported errors, rerun with standalone pylint to see if any differences. /tmp/a.py: d = { 'a': {}, 'b':1 }
dict_keys = []
if isinstance(d, dict):
dict_keys = [c for c in d if isinstance(d[c], dict)]
print("dict_keys: " + str(dict_keys))
dict_keys: ['a'] python3.7 -m pylint /tmp/a.py ************* Module a With python3.8: python3.8 -m pylint --version
root@abb086d6fbf6:/usr/src/app# python3.8 -m pylint /tmp/a.py
|
Thanks, I misunderstood -- thought you were saying you found a case reproducible without a try/except block. I think it's worth adding this case to the tests. |
I can still reproduce this in 2.13.4: # test.py
def select_dict(data, keys):
try:
d = {key: data[key] for key in keys}
except KeyError as e:
key, *_ = e.args
raise Exception(f"{key} not found")
return d $ pip install -U pylint==2.13.4 -q && pylint test.py
************* Module test
test.py:3:23: E0601: Using variable 'key' before assignment (used-before-assignment)
------------------------------------------------------------------
Your code has been rated at 2.86/10 (previous run: 2.86/10, +0.00) |
Thanks for the report: this one does not involve an if test in the comprehension, so I'm going to open a new issue for it. I have a patch ready. |
Bug description
Somewhat similarly to #5965,
leads to an
used-before-assignment
false-positive for the second line, yetasset
is always defined there.I bisected this to bd55b27, part of #5402 ("Fix #4761: Emit used-before-assignment where single assignment only made in except blocks")
Configuration
No response
Command used
Pylint output
Expected behavior
No error.
Pylint version
(environment in which I originally encountered the bug - output above is from a git install of both astroid and pylint)
Also happens with latest git main (astroid b18c7828, pylint 0bc45e9). Does not happen on pylint 2.12.2 and astroid 2.9.3 which I used previously.
OS / Environment
Archlinux
Additional dependencies
No response
The text was updated successfully, but these errors were encountered: