Skip to content

Commit

Permalink
Enable module-level checks for B018 (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
inwaves committed Dec 8, 2022
1 parent 38e7fd8 commit a751ead
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
5 changes: 4 additions & 1 deletion bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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 "
Expand Down
18 changes: 18 additions & 0 deletions tests/b018_modules.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions tests/test_bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit a751ead

Please sign in to comment.