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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

B021: f-string used as docstring. #230

Merged
merged 4 commits into from Mar 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions README.rst
Expand Up @@ -134,6 +134,9 @@ data available in ``ex``.

**B020**: Loop control variable overrides iterable it iterates

**B021**: f-string used as docstring. This will be interpreted by python
as a joined string rather than a docstring.


Opinionated warnings
~~~~~~~~~~~~~~~~~~~~
Expand Down
22 changes: 22 additions & 0 deletions bugbear.py
Expand Up @@ -350,11 +350,13 @@ def visit_FunctionDef(self, node):
self.check_for_b902(node)
self.check_for_b006(node)
self.check_for_b018(node)
self.check_for_b021(node)
self.generic_visit(node)

def visit_ClassDef(self, node):
self.check_for_b903(node)
self.check_for_b018(node)
self.check_for_b021(node)
self.generic_visit(node)

def visit_Try(self, node):
Expand Down Expand Up @@ -685,6 +687,20 @@ def check_for_b018(self, node):
):
self.errors.append(B018(subnode.lineno, subnode.col_offset))

def check_for_b021(self, node):
if (
node.body
and isinstance(node.body[0], ast.Expr)
and isinstance(node.body[0].value, ast.JoinedStr)
and ast.get_source_segment(
"".join(self.lines),
node.body[0].value,
).startswith('f"""')
jpy-git marked this conversation as resolved.
Show resolved Hide resolved
):
self.errors.append(
B021(node.body[0].value.lineno, node.body[0].value.col_offset)
)


@attr.s
class NameFinder(ast.NodeVisitor):
Expand Down Expand Up @@ -892,6 +908,12 @@ def visit(self, node):
+ "with each iterable value."
)
)
B021 = Error(
message=(
"B021 f-string used as docstring."
"This will be interpreted by python as a joined string rather than a docstring."
)
)

# Warnings disabled by default.
B901 = Error(
Expand Down
22 changes: 22 additions & 0 deletions tests/b021.py
@@ -0,0 +1,22 @@
"""
Should emit:
B021 - on lines 14, 22
"""

VARIABLE = "world"


def foo1():
"""hello world!"""


def foo2():
f"""hello {VARIABLE}!"""


class bar1:
"""hello world!"""


class bar2:
f"""hello {VARIABLE}!"""
8 changes: 8 additions & 0 deletions tests/test_bugbear.py
Expand Up @@ -30,6 +30,7 @@
B017,
B018,
B020,
B021,
B901,
B902,
B903,
Expand Down Expand Up @@ -262,6 +263,13 @@ def test_b020(self):
),
)

def test_b021_classes(self):
filename = Path(__file__).absolute().parent / "b021.py"
bbc = BugBearChecker(filename=str(filename))
errors = list(bbc.run())
expected = self.errors(B021(14, 4), B021(22, 4))
self.assertEqual(errors, expected)

def test_b901(self):
filename = Path(__file__).absolute().parent / "b901.py"
bbc = BugBearChecker(filename=str(filename))
Expand Down