summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjaydesl <35102795+jaydesl@users.noreply.github.com>2021-10-16 09:00:50 +0100
committerGitHub <noreply@github.com>2021-10-16 10:00:50 +0200
commit9098c6078551e2d3028fe65537d3b0e407c85724 (patch)
treece6faf420cf69a40ed0f7fb858add734764e9167
parentab775f6f4507a1f0cd9dab0fd027c2f08be06928 (diff)
downloadpylint-git-9098c6078551e2d3028fe65537d3b0e407c85724.tar.gz
Consider Enums when checking for duplicate dictionary keys (#5155)
* detect duplicate-key for enum members Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
-rw-r--r--CONTRIBUTORS.txt2
-rw-r--r--ChangeLog4
-rw-r--r--pylint/checkers/base.py10
-rw-r--r--tests/functional/d/duplicate_dict_literal_key.py14
-rw-r--r--tests/functional/d/duplicate_dict_literal_key.txt7
5 files changed, 31 insertions, 6 deletions
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt
index 31c47ac77..bd6183040 100644
--- a/CONTRIBUTORS.txt
+++ b/CONTRIBUTORS.txt
@@ -552,3 +552,5 @@ contributors:
* Jaehoon Hwang (jaehoonhwang): contributor
* Samuel Forestier: contributor
+
+* James DesLauriers: contributor
diff --git a/ChangeLog b/ChangeLog
index e4f6270d3..10af89334 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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.
diff --git a/pylint/checkers/base.py b/pylint/checkers/base.py
index 124881b77..36b50186b 100644
--- a/pylint/checkers/base.py
+++ b/pylint/checkers/base.py
@@ -1452,9 +1452,13 @@ class BasicChecker(_BasicChecker):
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"""
diff --git a/tests/functional/d/duplicate_dict_literal_key.py b/tests/functional/d/duplicate_dict_literal_key.py
index 6d359ff0a..82e98d23e 100644
--- a/tests/functional/d/duplicate_dict_literal_key.py
+++ b/tests/functional/d/duplicate_dict_literal_key.py
@@ -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',
diff --git a/tests/functional/d/duplicate_dict_literal_key.txt b/tests/functional/d/duplicate_dict_literal_key.txt
index 756f15818..83941b988 100644
--- a/tests/functional/d/duplicate_dict_literal_key.txt
+++ b/tests/functional/d/duplicate_dict_literal_key.txt
@@ -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