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 new message called duplicate-value #5928

Merged
merged 16 commits into from
Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Release date: TBA

* Fix pyreverse diagrams type hinting for classmethods and staticmethods.

* Add new message ``duplicate-value`` for identifying duplicate values inside sets.
Pierre-Sassoulas marked this conversation as resolved.
Show resolved Hide resolved

Closes #5880
Pierre-Sassoulas marked this conversation as resolved.
Show resolved Hide resolved

* Fix matching ``--notes`` options that end in a non-word character.

Closes #5840
Expand Down
4 changes: 4 additions & 0 deletions doc/whatsnew/2.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ New checkers

Closes #5670

* Added new message called ``duplicate-value`` which identifies duplicate values inside sets.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be moved to 2.14.


Closes #5880

Pierre-Sassoulas marked this conversation as resolved.
Show resolved Hide resolved
Removed checkers
================

Expand Down
18 changes: 18 additions & 0 deletions pylint/checkers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,11 @@ class BasicChecker(_BasicChecker):
"Used when an assert statement has a string literal as its first argument, which will "
"cause the assert to always pass.",
),
"W0130": (
"Duplicate value %r in set",
"duplicate-value",
"Used when a set contains the same value multiple times.",
),
}

reports = (("RP0101", "Statistics by type", report_by_type_stats),)
Expand Down Expand Up @@ -1467,6 +1472,19 @@ def visit_dict(self, node: nodes.Dict) -> None:
self.add_message("duplicate-key", node=node, args=key)
keys.add(key)

@utils.check_messages("duplicate-value")
def visit_set(self, node: nodes.Set) -> None:
areveny marked this conversation as resolved.
Show resolved Hide resolved
"""Check duplicate value in set."""
values = set()
for v in node.elts:
if isinstance(v, nodes.Const):
value = v.value
else:
areveny marked this conversation as resolved.
Show resolved Hide resolved
continue
if value in values:
self.add_message("duplicate-value", node=node, args=value)
Copy link
Collaborator

@DanielNoord DanielNoord Mar 29, 2022

Choose a reason for hiding this comment

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

Suggested change
self.add_message("duplicate-value", node=node, args=value)
self.add_message("duplicate-value", node=node, args=value, confidence=HIGH)

You'll probably need to import that.

values.add(value)

def visit_tryfinally(self, node: nodes.TryFinally) -> None:
"""Update try...finally flag."""
self._tryfinallys.append(node)
Expand Down
18 changes: 18 additions & 0 deletions tests/functional/d/duplicate_value.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# pylint: disable = invalid-name, line-too-long
"""Simple test sets for checking duplicate values"""

set1 = {1, 2, 3, 4}
set2 = {1, 1, 2} # [duplicate-value]
set3 = {1, 2, 2} # [duplicate-value]

set4 = {'one', 'two', 'three'}
set5 = {'one', 'two', 'one'} # [duplicate-value]
set6 = {'one', 'two', 'two'} # [duplicate-value]

wrong_set = {12, 23, True, 6, True, 0, 12} # [duplicate-value, duplicate-value]
correct_set = {12, 13, 23, 24, 89}

wrong_set_mixed = {1, 2, 'value', 1} # [duplicate-value]
wrong_set = {'arg1', 'arg2', False, 'arg1', True} # [duplicate-value]

another_wrong_set = {2, 3, 'arg1', True, 'arg1', False, True} # [duplicate-value, duplicate-value]
10 changes: 10 additions & 0 deletions tests/functional/d/duplicate_value.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
duplicate-value:5:7:5:16::Duplicate value 1 in set:UNDEFINED
duplicate-value:6:7:6:16::Duplicate value 2 in set:UNDEFINED
duplicate-value:9:7:9:28::Duplicate value 'one' in set:UNDEFINED
duplicate-value:10:7:10:28::Duplicate value 'two' in set:UNDEFINED
duplicate-value:12:12:12:42::Duplicate value 12 in set:UNDEFINED
duplicate-value:12:12:12:42::Duplicate value True in set:UNDEFINED
duplicate-value:15:18:15:36::Duplicate value 1 in set:UNDEFINED
duplicate-value:16:12:16:49::Duplicate value 'arg1' in set:UNDEFINED
duplicate-value:18:20:18:61::Duplicate value 'arg1' in set:UNDEFINED
duplicate-value:18:20:18:61::Duplicate value True in set:UNDEFINED