Skip to content

Commit

Permalink
B018: Find more constants w/o assign (#201)
Browse files Browse the repository at this point in the history
This check now also finds:
* numbers (ints, float, complex)
* lists
* sets
* dicts
* binary strings
* joined strings (f-strings)
* name constants (True, False, None)
  • Loading branch information
kasium authored Oct 29, 2021
1 parent 597dbf6 commit 2ca8d79
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 41 deletions.
14 changes: 13 additions & 1 deletion bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))


Expand Down
35 changes: 0 additions & 35 deletions tests/b018.py

This file was deleted.

31 changes: 31 additions & 0 deletions tests/b018_classes.py
Original file line number Diff line number Diff line change
@@ -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"
30 changes: 30 additions & 0 deletions tests/b018_functions.py
Original file line number Diff line number Diff line change
@@ -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"
20 changes: 15 additions & 5 deletions tests/test_bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 2ca8d79

Please sign in to comment.