From 2ca8d79ef840a216b1d9c9c1c9c4f5cb7a6da0c6 Mon Sep 17 00:00:00 2001 From: kasium <15907922+kasium@users.noreply.github.com> Date: Fri, 29 Oct 2021 20:28:02 +0200 Subject: [PATCH] B018: Find more constants w/o assign (#201) This check now also finds: * numbers (ints, float, complex) * lists * sets * dicts * binary strings * joined strings (f-strings) * name constants (True, False, None) --- bugbear.py | 14 +++++++++++++- tests/b018.py | 35 ----------------------------------- tests/b018_classes.py | 31 +++++++++++++++++++++++++++++++ tests/b018_functions.py | 30 ++++++++++++++++++++++++++++++ tests/test_bugbear.py | 20 +++++++++++++++----- 5 files changed, 89 insertions(+), 41 deletions(-) delete mode 100644 tests/b018.py create mode 100644 tests/b018_classes.py create mode 100644 tests/b018_functions.py diff --git a/bugbear.py b/bugbear.py index 581c090..efab5ab 100644 --- a/bugbear.py +++ b/bugbear.py @@ -579,7 +579,19 @@ def check_for_b903(self, node): def check_for_b018(self, node): for subnode in node.body[1:]: - if isinstance(subnode, ast.Expr) and isinstance(subnode.value, ast.Str): + if isinstance(subnode, ast.Expr) and isinstance( + subnode.value, + ( + ast.Str, + ast.Num, + ast.Bytes, + ast.NameConstant, + ast.JoinedStr, + ast.List, + ast.Set, + ast.Dict, + ), + ): self.errors.append(B018(subnode.lineno, subnode.col_offset)) diff --git a/tests/b018.py b/tests/b018.py deleted file mode 100644 index 11c207c..0000000 --- a/tests/b018.py +++ /dev/null @@ -1,35 +0,0 @@ -""" -Should emit: -B018 - on lines 14, 19, 30, 35 -""" - - -def foo1(): - """my docstring""" - - -def foo2(): - """my docstring""" - a = 2 - "str" - - -def foo3(): - a = 2 - "str" - - -class Foo1: - """abc""" - - -class Foo2: - """abc""" - - a = 2 - "str" - - -class Foo3: - a = 2 - "str" diff --git a/tests/b018_classes.py b/tests/b018_classes.py new file mode 100644 index 0000000..8a815d7 --- /dev/null +++ b/tests/b018_classes.py @@ -0,0 +1,31 @@ +""" +Should emit: +B018 - on lines 15-26, 31 +""" + + +class Foo1: + """abc""" + + +class Foo2: + """abc""" + + a = 2 + "str" # Str + 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) + [1, 2] # list + {1, 2} # set + {"foo": "bar"} # dict + + +class Foo3: + a = 2 + "str" diff --git a/tests/b018_functions.py b/tests/b018_functions.py new file mode 100644 index 0000000..afb7d26 --- /dev/null +++ b/tests/b018_functions.py @@ -0,0 +1,30 @@ +""" +Should emit: +B018 - on lines 14-25, 30 +""" + + +def foo1(): + """my docstring""" + + +def foo2(): + """my docstring""" + a = 2 + "str" # Str + 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) + [1, 2] # list + {1, 2} # set + {"foo": "bar"} # dict + + +def foo3(): + a = 2 + "str" diff --git a/tests/test_bugbear.py b/tests/test_bugbear.py index c3e3437..0ec95c4 100644 --- a/tests/test_bugbear.py +++ b/tests/test_bugbear.py @@ -212,13 +212,23 @@ def test_b017(self): expected = self.errors(B017(22, 8)) self.assertEqual(errors, expected) - def test_b018(self): - filename = Path(__file__).absolute().parent / "b018.py" + def test_b018_functions(self): + filename = Path(__file__).absolute().parent / "b018_functions.py" bbc = BugBearChecker(filename=str(filename)) errors = list(bbc.run()) - self.assertEqual( - errors, self.errors(B018(14, 4), B018(19, 4), B018(30, 4), B018(35, 4)) - ) + + expected = [B018(line, 4) for line in range(14, 26)] + expected.append(B018(30, 4)) + 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(15, 27)] + expected.append(B018(31, 4)) + self.assertEqual(errors, self.errors(*expected)) def test_b901(self): filename = Path(__file__).absolute().parent / "b901.py"