-
-
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
False reports on W0644 unbalanced-dict-unpacking in pylint 2.16.0 #8156
Comments
Thank you @Spindel. Indeed it is falling over when for x, y, z in {1: ("a", "b", "c"), 2: ("d", "e", "f")}.values():
print(x) |
That's a much simpler reproducer, indeed. |
- Remove unnecessary else after raise - Remove unnecessary else after return - Disable unbalanced-dict-unpacking false positive see pylint-dev/pylint#8156 Signed-off-by: Lukas Puehringer <[email protected]>
- Remove unnecessary else after raise - Remove unnecessary else after return - Disable unbalanced-dict-unpacking false positive see pylint-dev/pylint#8156 Signed-off-by: Lukas Puehringer <[email protected]>
The following code shows the false-positive isn't caused by how many values are in the tuple, but by how many elements are in the dictionary: # The unpacking error in this for-loop is intentional to show the warning doen't point to the actual error
for value1, value2 in {1: 'a', 2: 'b', 3: 'c'}.values():
pass
# lint_warn.py:2:0: W0644: Possible unbalanced dict unpacking with {1: 'a', 2: 'b', 3: 'c'}.values(): left side has 2 labels, right side has 3 values (unbalanced-dict-unpacking) The warning tells me I should have 3 values unpacked, instead of 1, despite this raising a runtime error. for value1, value2 in {1: ('a', 'b'), 2: ('c', 'd'), 3: ('e', 'f')}.values():
pass
#lint_warn.py:6:0: W0644: Possible unbalanced dict unpacking with {1: ('a', 'b'), 2: ('c', 'd'), 3: ('e', 'f')}.values(): left side has 2 labels, right side has 3 values (unbalanced-dict-unpacking) The warning tells me to unpack 3 elements instead of 2. # This unpacking error here is intentional to show there's no warning in this case
for value1, value2 in {1: ('a', 'b', 'c'), 2: ('d', 'e', 'f')}.values():
pass Pylint raises no warning here, despite the unpacking error. for value1, value2 in {1: ('a', 'b'), 2: ('c', 'd'), 3: ('e', 'f'), 4: ('g', 'h')}.values():
pass
#lint_warn.py:15:0: W0644: Possible unbalanced dict unpacking with {1: ('a', 'b'), 2: ('c', 'd'), 3: ('e', 'f'), 4: ('g', 'h')}.values(): left side has 2 labels, right side has 4 values (unbalanced-dict-unpacking) The warning tells me to unpack 4 elements instead of 2. It appears that pylint expects To drive the point home, here's pylint warning me about W0644 when the dictionary value can't exist: for value1, value2 in {1: does_not_exist(1), 2: does_not_exist(2), 3: does_not_exist(3)}.values():
pass
#lint_warn.py:19:0: W0644: Possible unbalanced dict unpacking with {1: does_not_exist(1), 2: does_not_exist(2), 3: does_not_exist(3)}.values(): left side has 2 labels, right side has 3 values (unbalanced-dict-unpacking)
#lint_warn.py:19:26: E0602: Undefined variable 'does_not_exist' (undefined-variable)
#lint_warn.py:19:48: E0602: Undefined variable 'does_not_exist' (undefined-variable)
#lint_warn.py:19:70: E0602: Undefined variable 'does_not_exist' (undefined-variable) |
Thanks for that @Neowizard. Essentially the tuple values of the dict are treated as if they were non iterable values. It just isn’t handled at all at the moment. |
@mbyrnepr2 no worries. I created PR #8892 that I believe fixes the issue |
Bug description
When unpacking while iterating over dict.values() we seem to get a spurious warning about unbalanced dict unpacking.
Here's a test-case, result vs. result1 may require a few changes to the types, but the original code was doing a lot more dynamic things with callbacks and more.
Configuration
No response
Command used
Pylint output
Expected behavior
No warning should happen
Pylint version
OS / Environment
Fedora 37, x86_64
Additional dependencies
No response
The text was updated successfully, but these errors were encountered: