From a751eade152404af333bed42e2477d5599eccfb3 Mon Sep 17 00:00:00 2001 From: Andrei Alexandru Date: Wed, 7 Dec 2022 21:29:01 -0800 Subject: [PATCH] Enable module-level checks for B018 (#315) --- bugbear.py | 5 ++++- tests/b018_modules.py | 18 ++++++++++++++++++ tests/test_bugbear.py | 8 ++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 tests/b018_modules.py diff --git a/bugbear.py b/bugbear.py index 90d238a..4e31763 100644 --- a/bugbear.py +++ b/bugbear.py @@ -359,6 +359,10 @@ def visit_Call(self, node): self.check_for_b905(node) self.generic_visit(node) + def visit_Module(self, node): + self.check_for_b018(node) + self.generic_visit(node) + def visit_Assign(self, node): if len(node.targets) == 1: t = node.targets[0] @@ -1117,7 +1121,6 @@ def visit_Lambda(self, node): error = namedtuple("error", "lineno col message type vars") Error = partial(partial, error, type=BugBearChecker, vars=()) - B001 = Error( message=( "B001 Do not use {}, it also catches unexpected " diff --git a/tests/b018_modules.py b/tests/b018_modules.py new file mode 100644 index 0000000..70d6e59 --- /dev/null +++ b/tests/b018_modules.py @@ -0,0 +1,18 @@ +""" +Should emit: +B018 - on lines 9-18 +""" + +a = 2 +"str" # Str (no raise) +f"{int}" # JoinedStr (no raise) +1j # Number (complex) +1 # Number (int) +1.0 # Number (float) +b"foo" # Binary +True # NameConstant (True) +False # NameConstant (False) +None # NameConstant (None) +[1, 2] # list +{1, 2} # set +{"foo": "bar"} # dict diff --git a/tests/test_bugbear.py b/tests/test_bugbear.py index e3ee667..1948428 100644 --- a/tests/test_bugbear.py +++ b/tests/test_bugbear.py @@ -275,6 +275,14 @@ def test_b018_classes(self): expected.append(B018(33, 4)) 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, 19)] + self.assertEqual(errors, self.errors(*expected)) + def test_b019(self): filename = Path(__file__).absolute().parent / "b019.py" bbc = BugBearChecker(filename=str(filename))