Skip to content

Commit

Permalink
Improve B018 further (#202)
Browse files Browse the repository at this point in the history
Catch also useless expressions on the first node in a body if the node is not a string (docstring).
  • Loading branch information
kasium authored Nov 22, 2021
1 parent 2ca8d79 commit 98829c3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
8 changes: 6 additions & 2 deletions bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,8 +578,12 @@ def check_for_b903(self, node):
self.errors.append(B903(node.lineno, node.col_offset))

def check_for_b018(self, node):
for subnode in node.body[1:]:
if isinstance(subnode, ast.Expr) and isinstance(
for index, subnode in enumerate(node.body):
if not isinstance(subnode, ast.Expr):
continue
if index == 0 and isinstance(subnode.value, ast.Str):
continue # most likely a docstring
if isinstance(
subnode.value,
(
ast.Str,
Expand Down
3 changes: 2 additions & 1 deletion tests/b018_classes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Should emit:
B018 - on lines 15-26, 31
B018 - on lines 15-26, 30, 32
"""


Expand All @@ -27,5 +27,6 @@ class Foo2:


class Foo3:
123
a = 2
"str"
3 changes: 2 additions & 1 deletion tests/b018_functions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Should emit:
B018 - on lines 14-25, 30
B018 - on lines 14-25, 29, 31
"""


Expand All @@ -26,5 +26,6 @@ def foo2():


def foo3():
123
a = 2
"str"
6 changes: 4 additions & 2 deletions tests/test_bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ def test_b018_functions(self):
errors = list(bbc.run())

expected = [B018(line, 4) for line in range(14, 26)]
expected.append(B018(30, 4))
expected.append(B018(29, 4))
expected.append(B018(31, 4))
self.assertEqual(errors, self.errors(*expected))

def test_b018_classes(self):
Expand All @@ -227,7 +228,8 @@ def test_b018_classes(self):
errors = list(bbc.run())

expected = [B018(line, 4) for line in range(15, 27)]
expected.append(B018(31, 4))
expected.append(B018(30, 4))
expected.append(B018(32, 4))
self.assertEqual(errors, self.errors(*expected))

def test_b901(self):
Expand Down

0 comments on commit 98829c3

Please sign in to comment.