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 classname to b018 useless-expression output #433

Merged
merged 2 commits into from Dec 2, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions bugbear.py
Expand Up @@ -1182,7 +1182,13 @@ def check_for_b018(self, node):
or subnode.value.value is None
)
):
self.errors.append(B018(subnode.lineno, subnode.col_offset))
self.errors.append(
B018(
subnode.lineno,
subnode.col_offset,
vars=(subnode.value.__class__.__name__,),
)
)

def check_for_b021(self, node):
if (
Expand Down Expand Up @@ -1835,7 +1841,7 @@ def visit_Lambda(self, node):
)
B018 = Error(
message=(
"B018 Found useless expression. Consider either assigning it to a "
"B018 Found useless {} expression. Consider either assigning it to a "
"variable or removing it."
)
)
Expand Down
59 changes: 48 additions & 11 deletions tests/test_bugbear.py
Expand Up @@ -267,31 +267,68 @@ def test_b018_functions(self):
bbc = BugBearChecker(filename=str(filename))
errors = list(bbc.run())

expected = [B018(line, 4) for line in range(15, 25)]
expected.append(B018(28, 4))
expected.append(B018(31, 4))
expected.append(B018(32, 4))
expected.append(B018(33, 4))
expected = [
B018(15, 4, vars=("Constant",)),
B018(16, 4, vars=("Constant",)),
B018(17, 4, vars=("Constant",)),
B018(18, 4, vars=("Constant",)),
B018(19, 4, vars=("Constant",)),
B018(20, 4, vars=("Constant",)),
B018(21, 4, vars=("Constant",)),
B018(22, 4, vars=("List",)),
B018(23, 4, vars=("Set",)),
B018(24, 4, vars=("Dict",)),
B018(28, 4, vars=("Constant",)),
B018(31, 4, vars=("Constant",)),
B018(32, 4, vars=("Tuple",)),
B018(33, 4, vars=("Tuple",)),
]

self.assertEqual(errors, self.errors(*expected))

def test_b018_classes(self):
filename = Path(__file__).absolute().parent / "b018_classes.py"
bbc = BugBearChecker(filename=str(filename))
errors = list(bbc.run())

expected = [B018(line, 4) for line in range(16, 26)]
expected.append(B018(29, 4))
expected.append(B018(32, 4))
expected.append(B018(33, 4))
expected.append(B018(34, 4))
expected = [
B018(16, 4, vars=("Constant",)),
B018(17, 4, vars=("Constant",)),
B018(18, 4, vars=("Constant",)),
B018(19, 4, vars=("Constant",)),
B018(20, 4, vars=("Constant",)),
B018(21, 4, vars=("Constant",)),
B018(22, 4, vars=("Constant",)),
B018(23, 4, vars=("List",)),
B018(24, 4, vars=("Set",)),
B018(25, 4, vars=("Dict",)),
B018(29, 4, vars=("Constant",)),
B018(32, 4, vars=("Constant",)),
B018(33, 4, vars=("Tuple",)),
B018(34, 4, vars=("Tuple",)),
]

self.assertEqual(errors, self.errors(*expected))

def test_b018_modules(self):
filename = Path(__file__).absolute().parent / "b018_modules.py"
bbc = BugBearChecker(filename=str(filename))
errors = list(bbc.run())

expected = [B018(line, 0) for line in range(9, 21)]
expected = [
B018(9, 0, vars=("Constant",)),
B018(10, 0, vars=("Constant",)),
B018(11, 0, vars=("Constant",)),
B018(12, 0, vars=("Constant",)),
B018(13, 0, vars=("Constant",)),
B018(14, 0, vars=("Constant",)),
B018(15, 0, vars=("Constant",)),
B018(16, 0, vars=("List",)),
B018(17, 0, vars=("Set",)),
B018(18, 0, vars=("Dict",)),
B018(19, 0, vars=("Tuple",)),
B018(20, 0, vars=("Tuple",)),
]
self.assertEqual(errors, self.errors(*expected))

def test_b019(self):
Expand Down