Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

B018: Find more constants w/o assign #201

Merged
merged 1 commit into from Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion bugbear.py
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
@@ -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
@@ -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
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