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

Consider Enums when checking for duplicate dictionary keys #5155

Merged
merged 5 commits into from
Oct 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -552,3 +552,5 @@ contributors:
* Jaehoon Hwang (jaehoonhwang): contributor

* Samuel Forestier: contributor

* James DesLauriers: contributor
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ Release date: TBA
* Added ``using-f-string-in-unsupported-version`` checker. Issued when ``py-version``
is set to a version that does not support f-strings (< 3.6)

* Properly emit ``duplicate-key`` when Enum members are duplicate dictionary keys

Closes #5150

* Use ``py-version`` setting for alternative union syntax check (PEP 604),
instead of the Python interpreter version.

Expand Down
10 changes: 7 additions & 3 deletions pylint/checkers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1452,9 +1452,13 @@ def visit_dict(self, node: nodes.Dict) -> None:
for k, _ in node.items:
if isinstance(k, nodes.Const):
key = k.value
if key in keys:
self.add_message("duplicate-key", node=node, args=key)
keys.add(key)
elif isinstance(k, nodes.Attribute):
key = k.as_string()
else:
continue
if key in keys:
self.add_message("duplicate-key", node=node, args=key)
keys.add(key)

def visit_tryfinally(self, node: nodes.TryFinally) -> None:
"""update try...finally flag"""
Expand Down
14 changes: 14 additions & 0 deletions tests/functional/d/duplicate_dict_literal_key.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
"""Check multiple key definition"""
# pylint: disable=pointless-statement, redundant-u-string-prefix

from enum import Enum


class MyEnum(Enum):
""" Sample Enum for testing duplicate keys"""
KEY = "key"



correct_dict = {
'tea': 'for two',
'two': 'for tea',
}

wrong_with_enum = { # [duplicate-key]
MyEnum.KEY: "value 1",
MyEnum.KEY: "value 2",
}

wrong_dict = { # [duplicate-key]
'tea': 'for two',
'two': 'for tea',
Expand Down
7 changes: 4 additions & 3 deletions tests/functional/d/duplicate_dict_literal_key.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
duplicate-key:9:13::Duplicate key 'tea' in dictionary
duplicate-key:16:0::Duplicate key 1 in dictionary
duplicate-key:17:0::Duplicate key 1.0 in dictionary
duplicate-key:18:18::Duplicate key 'MyEnum.KEY' in dictionary:HIGH
duplicate-key:23:13::Duplicate key 'tea' in dictionary:HIGH
duplicate-key:30:0::Duplicate key 1 in dictionary:HIGH
duplicate-key:31:0::Duplicate key 1.0 in dictionary:HIGH