Skip to content

Commit 2ca8d79

Browse files
authored
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)
1 parent 597dbf6 commit 2ca8d79

File tree

5 files changed

+89
-41
lines changed

5 files changed

+89
-41
lines changed

bugbear.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,19 @@ def check_for_b903(self, node):
579579

580580
def check_for_b018(self, node):
581581
for subnode in node.body[1:]:
582-
if isinstance(subnode, ast.Expr) and isinstance(subnode.value, ast.Str):
582+
if isinstance(subnode, ast.Expr) and isinstance(
583+
subnode.value,
584+
(
585+
ast.Str,
586+
ast.Num,
587+
ast.Bytes,
588+
ast.NameConstant,
589+
ast.JoinedStr,
590+
ast.List,
591+
ast.Set,
592+
ast.Dict,
593+
),
594+
):
583595
self.errors.append(B018(subnode.lineno, subnode.col_offset))
584596

585597

tests/b018.py

-35
This file was deleted.

tests/b018_classes.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Should emit:
3+
B018 - on lines 15-26, 31
4+
"""
5+
6+
7+
class Foo1:
8+
"""abc"""
9+
10+
11+
class Foo2:
12+
"""abc"""
13+
14+
a = 2
15+
"str" # Str
16+
1j # Number (complex)
17+
1 # Number (int)
18+
1.0 # Number (float)
19+
b"foo" # Binary
20+
f"{int}" # JoinedStr
21+
True # NameConstant (True)
22+
False # NameConstant (False)
23+
None # NameConstant (None)
24+
[1, 2] # list
25+
{1, 2} # set
26+
{"foo": "bar"} # dict
27+
28+
29+
class Foo3:
30+
a = 2
31+
"str"

tests/b018_functions.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Should emit:
3+
B018 - on lines 14-25, 30
4+
"""
5+
6+
7+
def foo1():
8+
"""my docstring"""
9+
10+
11+
def foo2():
12+
"""my docstring"""
13+
a = 2
14+
"str" # Str
15+
1j # Number (complex)
16+
1 # Number (int)
17+
1.0 # Number (float)
18+
b"foo" # Binary
19+
f"{int}" # JoinedStr
20+
True # NameConstant (True)
21+
False # NameConstant (False)
22+
None # NameConstant (None)
23+
[1, 2] # list
24+
{1, 2} # set
25+
{"foo": "bar"} # dict
26+
27+
28+
def foo3():
29+
a = 2
30+
"str"

tests/test_bugbear.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,23 @@ def test_b017(self):
212212
expected = self.errors(B017(22, 8))
213213
self.assertEqual(errors, expected)
214214

215-
def test_b018(self):
216-
filename = Path(__file__).absolute().parent / "b018.py"
215+
def test_b018_functions(self):
216+
filename = Path(__file__).absolute().parent / "b018_functions.py"
217217
bbc = BugBearChecker(filename=str(filename))
218218
errors = list(bbc.run())
219-
self.assertEqual(
220-
errors, self.errors(B018(14, 4), B018(19, 4), B018(30, 4), B018(35, 4))
221-
)
219+
220+
expected = [B018(line, 4) for line in range(14, 26)]
221+
expected.append(B018(30, 4))
222+
self.assertEqual(errors, self.errors(*expected))
223+
224+
def test_b018_classes(self):
225+
filename = Path(__file__).absolute().parent / "b018_classes.py"
226+
bbc = BugBearChecker(filename=str(filename))
227+
errors = list(bbc.run())
228+
229+
expected = [B018(line, 4) for line in range(15, 27)]
230+
expected.append(B018(31, 4))
231+
self.assertEqual(errors, self.errors(*expected))
222232

223233
def test_b901(self):
224234
filename = Path(__file__).absolute().parent / "b901.py"

0 commit comments

Comments
 (0)