Skip to content

Commit

Permalink
add classname to b018 useless-expression output (#433)
Browse files Browse the repository at this point in the history
* add classname to b018 useless-expression output

* update unittests
  • Loading branch information
r-downing authored Dec 2, 2023
1 parent 6686e52 commit 4ca0e6b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
10 changes: 8 additions & 2 deletions bugbear.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 4ca0e6b

Please sign in to comment.