From 068ea5a8e72127209927cacadb9ee261df3beec2 Mon Sep 17 00:00:00 2001 From: Kai Mueller <15907922+kasium@users.noreply.github.com> Date: Mon, 10 Jan 2022 15:26:26 +0000 Subject: [PATCH 1/3] Ignore JoinedStr for B018 --- bugbear.py | 2 +- tests/b018_classes.py | 1 + tests/b018_functions.py | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/bugbear.py b/bugbear.py index 78c2ca3..d2a366e 100644 --- a/bugbear.py +++ b/bugbear.py @@ -657,7 +657,7 @@ def check_for_b018(self, node): for index, subnode in enumerate(node.body): if not isinstance(subnode, ast.Expr): continue - if index == 0 and isinstance(subnode.value, ast.Str): + if index == 0 and isinstance(subnode.value, (ast.Str, ast.JoinedStr)): continue # most likely a docstring if isinstance( subnode.value, diff --git a/tests/b018_classes.py b/tests/b018_classes.py index 551b14e..abc3ce4 100644 --- a/tests/b018_classes.py +++ b/tests/b018_classes.py @@ -31,3 +31,4 @@ class Foo3: a = 2 "str" 1 + f"should not raise" diff --git a/tests/b018_functions.py b/tests/b018_functions.py index f8c3f77..2792a65 100644 --- a/tests/b018_functions.py +++ b/tests/b018_functions.py @@ -30,3 +30,4 @@ def foo3(): a = 2 "str" 3 + f"should not raise" From 2d1baa68d517532f922a163b4dff16f02b13c2d7 Mon Sep 17 00:00:00 2001 From: Kai Mueller <15907922+kasium@users.noreply.github.com> Date: Mon, 10 Jan 2022 15:29:45 +0000 Subject: [PATCH 2/3] Improve tests --- bugbear.py | 3 --- tests/b018_classes.py | 7 +++---- tests/b018_functions.py | 7 +++---- tests/test_bugbear.py | 4 ++-- 4 files changed, 8 insertions(+), 13 deletions(-) diff --git a/bugbear.py b/bugbear.py index d2a366e..ee6a15f 100644 --- a/bugbear.py +++ b/bugbear.py @@ -657,15 +657,12 @@ def check_for_b018(self, node): for index, subnode in enumerate(node.body): if not isinstance(subnode, ast.Expr): continue - if index == 0 and isinstance(subnode.value, (ast.Str, ast.JoinedStr)): - continue # most likely a docstring if isinstance( subnode.value, ( ast.Num, ast.Bytes, ast.NameConstant, - ast.JoinedStr, ast.List, ast.Set, ast.Dict, diff --git a/tests/b018_classes.py b/tests/b018_classes.py index abc3ce4..7992f15 100644 --- a/tests/b018_classes.py +++ b/tests/b018_classes.py @@ -1,6 +1,6 @@ """ Should emit: -B018 - on lines 16-26, 30, 33 +B018 - on lines 17-26, 30, 33 """ @@ -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) @@ -31,4 +31,3 @@ class Foo3: a = 2 "str" 1 - f"should not raise" diff --git a/tests/b018_functions.py b/tests/b018_functions.py index 2792a65..763d6b8 100644 --- a/tests/b018_functions.py +++ b/tests/b018_functions.py @@ -1,6 +1,6 @@ """ Should emit: -B018 - on lines 15-25, 29, 32 +B018 - on lines 16-25, 29, 32 """ @@ -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) @@ -30,4 +30,3 @@ def foo3(): a = 2 "str" 3 - f"should not raise" diff --git a/tests/test_bugbear.py b/tests/test_bugbear.py index d1af992..d02b637 100644 --- a/tests/test_bugbear.py +++ b/tests/test_bugbear.py @@ -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)) @@ -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)) From 5165805e22950e78c4a3133af801fea9e9f7e0ea Mon Sep 17 00:00:00 2001 From: Kai Mueller <15907922+kasium@users.noreply.github.com> Date: Mon, 10 Jan 2022 15:31:50 +0000 Subject: [PATCH 3/3] Fix bug --- bugbear.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bugbear.py b/bugbear.py index ee6a15f..b8ecbd5 100644 --- a/bugbear.py +++ b/bugbear.py @@ -654,7 +654,7 @@ 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 isinstance(