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

Improve B033 (duplicate set items) #385

Merged
merged 4 commits into from May 22, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 12 additions & 8 deletions bugbear.py
Expand Up @@ -1351,12 +1351,16 @@ def check_for_b032(self, node):
self.errors.append(B032(node.lineno, node.col_offset))

def check_for_b033(self, node):
constants = [
item.value
for item in filter(lambda x: isinstance(x, ast.Constant), node.elts)
]
if len(constants) != len(set(constants)):
self.errors.append(B033(node.lineno, node.col_offset))
seen = set()
for elt in node.elts:
if not isinstance(elt, ast.Constant):
continue
if elt.value in seen:
self.errors.append(
B033(elt.lineno, elt.col_offset, vars=(repr(elt.value),))
)
else:
seen.add(elt.value)


def compose_call_path(node):
Expand Down Expand Up @@ -1757,8 +1761,8 @@ def visit_Lambda(self, node):

B033 = Error(
message=(
"B033 Sets should not contain duplicate items. Duplicate items will be replaced"
" with a single item at runtime."
"B033 Set should not contain duplicate item {}. Duplicate items will be"
" replaced with a single item at runtime."
)
)

Expand Down
7 changes: 6 additions & 1 deletion tests/b033.py
@@ -1,6 +1,6 @@
"""
Should emit:
B033 - on lines 6-12
B033 - on lines 6-12 and 16
"""

test = {1, 2, 3, 3, 5}
Expand All @@ -10,6 +10,11 @@
test = {3, 3.0}
test = {1, True}
test = {0, False}
multi_line = {
"alongvalueeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
1,
True,
}

test = {1, 2, 3, 3.5, 5}
test = {"a", "b", "c", "d", "e"}
Expand Down
15 changes: 8 additions & 7 deletions tests/test_bugbear.py
Expand Up @@ -493,13 +493,14 @@ def test_b033(self):
bbc = BugBearChecker(filename=str(filename))
errors = list(bbc.run())
expected = self.errors(
B033(6, 7),
B033(7, 7),
B033(8, 7),
B033(9, 7),
B033(10, 7),
B033(11, 7),
B033(12, 7),
B033(6, 17, vars=("3",)),
B033(7, 23, vars=("'c'",)),
B033(8, 21, vars=("True",)),
B033(9, 20, vars=("None",)),
B033(10, 11, vars=("3.0",)),
B033(11, 11, vars=("True",)),
B033(12, 11, vars=("False",)),
B033(16, 4, vars=("True",)),
)
self.assertEqual(errors, expected)

Expand Down