user1659464
user1659464

Reputation: 333

Pylint warning: Possible unbalanced tuple unpacking with sequence

I have a piece of Python code:

def func1():                                                                                                                  
    a=set()
    b = ','.join(map(str, list(a)))
    return  b, []

def func2():
    d = 1
    e = 2
    return func1() + (d, e,)

def main():
    a,b,c,d = func2()

if __name__ == '__main__':
    main()

When I run it through pylint (1.4.0), I receive the warning:

W: 12, 4: Possible unbalanced tuple unpacking with sequence: left side has 4 label(s), right side has 3 value(s) (unbalanced-tuple-unpacking)

It seems that func2 will always return four results. What does the error mean and why?

Upvotes: 17

Views: 13443

Answers (2)

The current version of pylint (3.0.2 as of 2023-11-23) does not have this bug any more:

$ pylint test_pylint_unpacking.py
************* Module test_pylint_unpacking
test_pylint_unpacking.py:1:12: C0303: Trailing whitespace (trailing-whitespace)
test_pylint_unpacking.py:1:0: C0114: Missing module docstring (missing-module-docstring)
test_pylint_unpacking.py:1:0: C0116: Missing function or method docstring (missing-function-docstring)
test_pylint_unpacking.py:6:0: C0116: Missing function or method docstring (missing-function-docstring)
test_pylint_unpacking.py:11:0: C0116: Missing function or method docstring (missing-function-docstring)
test_pylint_unpacking.py:12:4: W0612: Unused variable 'a' (unused-variable)
test_pylint_unpacking.py:12:6: W0612: Unused variable 'b' (unused-variable)
test_pylint_unpacking.py:12:8: W0612: Unused variable 'c' (unused-variable)
test_pylint_unpacking.py:12:10: W0612: Unused variable 'd' (unused-variable)

------------------------------------------------------------------
Your code has been rated at 2.50/10

Upvotes: 0

The Chess Guy 20
The Chess Guy 20

Reputation: 311

If the warning is erroneous, it can be disabled by appending # pylint: disable=unbalanced-tuple-unpacking to the line.

Upvotes: 21

Related Questions