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 unbalanced-dict-unpacking #9986

Open
mthuurne opened this issue Sep 29, 2024 · 0 comments
Open

False positive unbalanced-dict-unpacking #9986

mthuurne opened this issue Sep 29, 2024 · 0 comments
Labels
Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling

Comments

@mthuurne
Copy link
Contributor

Bug description

class C:
    def __init__(self):
        self.data = {}

    def add(self, name, value):
        self.data[name] = value

    def dump(self):
        if len(self.data) == 1:
            (name, value), = self.data.items()
            print(f"just one: {name} -> {value}")
        else:
            print("empty or multiple items")

Configuration

No response

Command used

pylint testcase.py

Pylint output

testcase.py:10:12: W0644: Possible unbalanced dict unpacking with self.data.items(): left side has 1 label, right side has 0 values (unbalanced-dict-unpacking)

Expected behavior

I would expect no message to be issued.

The problem goes away if data is initialized with 1 item in it.

Ideally, pylint would take into account that the length of the dictionary has just been checked. I imagine that wouldn't be easy to implement though.

A possible alternative would be to not issue this message when a dictionary might have had its contents changed after its initialization.

Pylint version

pylint 3.3.1
astroid 3.3.4
Python 3.11.10 (main, Sep 09 2024, 17:03:08) [GCC]

OS / Environment

Linux (openSUSE Tumbleweed)

Additional dependencies

No response

@mthuurne mthuurne added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Sep 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling
Projects
None yet
Development

No branches or pull requests

1 participant