Skip to content

Commit

Permalink
Ignore JoinedStr for B018 (#216)
Browse files Browse the repository at this point in the history
* Ignore JoinedStr for B018

* Improve tests

* Fix bug
  • Loading branch information
kasium authored Jan 12, 2022
1 parent c0a9c87 commit 6fb81ee
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 12 deletions.
5 changes: 1 addition & 4 deletions bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,18 +654,15 @@ def check_for_b903(self, node):
self.errors.append(B903(node.lineno, node.col_offset))

def check_for_b018(self, node):
for index, subnode in enumerate(node.body):
for subnode in 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.Num,
ast.Bytes,
ast.NameConstant,
ast.JoinedStr,
ast.List,
ast.Set,
ast.Dict,
Expand Down
6 changes: 3 additions & 3 deletions tests/b018_classes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Should emit:
B018 - on lines 16-26, 30, 33
B018 - on lines 17-26, 30, 33
"""


Expand All @@ -12,12 +12,12 @@ class Foo2:
"""abc"""

a = 2
"str" # Str
"str" # Str (no raise)
f"{int}" # JoinedStr (no raise)
1j # Number (complex)
1 # Number (int)
1.0 # Number (float)
b"foo" # Binary
f"{int}" # JoinedStr
True # NameConstant (True)
False # NameConstant (False)
None # NameConstant (None)
Expand Down
6 changes: 3 additions & 3 deletions tests/b018_functions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Should emit:
B018 - on lines 15-25, 29, 32
B018 - on lines 16-25, 29, 32
"""


Expand All @@ -11,12 +11,12 @@ def foo1():
def foo2():
"""my docstring"""
a = 2
"str" # Str
"str" # Str (no raise)
f"{int}" # JoinedStr (no raise)
1j # Number (complex)
1 # Number (int)
1.0 # Number (float)
b"foo" # Binary
f"{int}" # JoinedStr
True # NameConstant (True)
False # NameConstant (False)
None # NameConstant (None)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def test_b018_functions(self):
bbc = BugBearChecker(filename=str(filename))
errors = list(bbc.run())

expected = [B018(line, 4) for line in range(15, 26)]
expected = [B018(line, 4) for line in range(16, 26)]
expected.append(B018(29, 4))
expected.append(B018(32, 4))
self.assertEqual(errors, self.errors(*expected))
Expand All @@ -244,7 +244,7 @@ def test_b018_classes(self):
bbc = BugBearChecker(filename=str(filename))
errors = list(bbc.run())

expected = [B018(line, 4) for line in range(16, 27)]
expected = [B018(line, 4) for line in range(17, 27)]
expected.append(B018(30, 4))
expected.append(B018(33, 4))
self.assertEqual(errors, self.errors(*expected))
Expand Down

0 comments on commit 6fb81ee

Please sign in to comment.