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

Add B041: Duplicate key-value pairs in dictionary literals #496

Merged
merged 3 commits into from
Oct 31, 2024

Conversation

yangdanny97
Copy link
Contributor

@yangdanny97 yangdanny97 commented Oct 30, 2024

This is the counterpart to the duplicate dictionary key linter in pyflakes: PyCQA/pyflakes#72

Pyflakes only emits an error if the values are different for the duplicate keys. This lint copies most of the logic but does the opposite - when we have multiple identical keys with the identical values, we emit an error on all but the first entry.

As with Pyflakes, it only works when the keys & values are tuples, literals, or variable names. Any other syntax will be treated as if they were different.

This could have been much simpler if we could just emit an error on all duplicate keys regardless of their values, but the conversation in the PR for B033 (#373) indicated that we should avoid duplication with Pyflakes.

@yangdanny97 yangdanny97 changed the title Add B041: Duplicate keys in dictionary literals Add B041: Duplicate key-value pairs in dictionary literals Oct 30, 2024
@amyreese
Copy link
Member

Would it be better to build this as a generic "duplicate key" rule, regardless of whether the content is the same, and just advise users to disable any relevant flake8/pyflakes rules to avoid overlapping violations?

@yangdanny97
Copy link
Contributor Author

yangdanny97 commented Oct 30, 2024

The other branch of my fork has a simplified version that's based on the B033 implementation: https://github.com/yangdanny97/flake8-bugbear/blob/main/bugbear.py#L640

But as-implemented it only supports Constant keys, so would have less coverage in some areas compared to pyflakes. If we wanted it to be a superset of the errors caught by pyflakes, we would still need to pull over some of the convert_to_value logic

Copy link
Collaborator

@cooperlees cooperlees left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this covers enough to start. We could enhance it / tune it moving forward.

Tests are pretty good coverage too ...

I will add one more scenence of info into the README tho.

@cooperlees cooperlees merged commit 95f8791 into PyCQA:main Oct 31, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants