From 9098c6078551e2d3028fe65537d3b0e407c85724 Mon Sep 17 00:00:00 2001 From: jaydesl <35102795+jaydesl@users.noreply.github.com> Date: Sat, 16 Oct 2021 09:00:50 +0100 Subject: Consider Enums when checking for duplicate dictionary keys (#5155) * detect duplicate-key for enum members Co-authored-by: Pierre Sassoulas --- CONTRIBUTORS.txt | 2 ++ ChangeLog | 4 ++++ pylint/checkers/base.py | 10 +++++++--- tests/functional/d/duplicate_dict_literal_key.py | 14 ++++++++++++++ tests/functional/d/duplicate_dict_literal_key.txt | 7 ++++--- 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 -- cgit v1.2.1