Skip to content

Commit c5223e0

Browse files
jpy-gitZac-HD
andauthored
B021: f-string used as docstring. (#230)
* B021: f-string used as docstring. * Update README.rst * Update bugbear.py Co-authored-by: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com> * There are more docstrings than just triple double quotes! Co-authored-by: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com>
1 parent 3206da7 commit c5223e0

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

README.rst

+3
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ data available in ``ex``.
134134

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

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

138141
Opinionated warnings
139142
~~~~~~~~~~~~~~~~~~~~

bugbear.py

+18
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,13 @@ def visit_FunctionDef(self, node):
350350
self.check_for_b902(node)
351351
self.check_for_b006(node)
352352
self.check_for_b018(node)
353+
self.check_for_b021(node)
353354
self.generic_visit(node)
354355

355356
def visit_ClassDef(self, node):
356357
self.check_for_b903(node)
357358
self.check_for_b018(node)
359+
self.check_for_b021(node)
358360
self.generic_visit(node)
359361

360362
def visit_Try(self, node):
@@ -685,6 +687,16 @@ def check_for_b018(self, node):
685687
):
686688
self.errors.append(B018(subnode.lineno, subnode.col_offset))
687689

690+
def check_for_b021(self, node):
691+
if (
692+
node.body
693+
and isinstance(node.body[0], ast.Expr)
694+
and isinstance(node.body[0].value, ast.JoinedStr)
695+
):
696+
self.errors.append(
697+
B021(node.body[0].value.lineno, node.body[0].value.col_offset)
698+
)
699+
688700

689701
@attr.s
690702
class NameFinder(ast.NodeVisitor):
@@ -892,6 +904,12 @@ def visit(self, node):
892904
+ "with each iterable value."
893905
)
894906
)
907+
B021 = Error(
908+
message=(
909+
"B021 f-string used as docstring."
910+
"This will be interpreted by python as a joined string rather than a docstring."
911+
)
912+
)
895913

896914
# Warnings disabled by default.
897915
B901 = Error(

tests/b021.py

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""
2+
Should emit:
3+
B021 - on lines 14, 22, 30, 38, 46, 54, 62, 70, 73
4+
"""
5+
6+
VARIABLE = "world"
7+
8+
9+
def foo1():
10+
"""hello world!"""
11+
12+
13+
def foo2():
14+
f"""hello {VARIABLE}!"""
15+
16+
17+
class bar1:
18+
"""hello world!"""
19+
20+
21+
class bar2:
22+
f"""hello {VARIABLE}!"""
23+
24+
25+
def foo1():
26+
"""hello world!"""
27+
28+
29+
def foo2():
30+
f"""hello {VARIABLE}!"""
31+
32+
33+
class bar1:
34+
"""hello world!"""
35+
36+
37+
class bar2:
38+
f"""hello {VARIABLE}!"""
39+
40+
41+
def foo1():
42+
"hello world!"
43+
44+
45+
def foo2():
46+
f"hello {VARIABLE}!"
47+
48+
49+
class bar1:
50+
"hello world!"
51+
52+
53+
class bar2:
54+
f"hello {VARIABLE}!"
55+
56+
57+
def foo1():
58+
"hello world!"
59+
60+
61+
def foo2():
62+
f"hello {VARIABLE}!"
63+
64+
65+
class bar1:
66+
"hello world!"
67+
68+
69+
class bar2:
70+
f"hello {VARIABLE}!"
71+
72+
73+
def baz():
74+
f"""I'm probably a docstring: {VARIABLE}!"""
75+
print(f"""I'm a normal string""")
76+
f"""Don't detect me!"""

tests/test_bugbear.py

+18
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
B017,
3131
B018,
3232
B020,
33+
B021,
3334
B901,
3435
B902,
3536
B903,
@@ -262,6 +263,23 @@ def test_b020(self):
262263
),
263264
)
264265

266+
def test_b021_classes(self):
267+
filename = Path(__file__).absolute().parent / "b021.py"
268+
bbc = BugBearChecker(filename=str(filename))
269+
errors = list(bbc.run())
270+
expected = self.errors(
271+
B021(14, 4),
272+
B021(22, 4),
273+
B021(30, 4),
274+
B021(38, 4),
275+
B021(46, 4),
276+
B021(54, 4),
277+
B021(62, 4),
278+
B021(70, 4),
279+
B021(74, 4),
280+
)
281+
self.assertEqual(errors, expected)
282+
265283
def test_b901(self):
266284
filename = Path(__file__).absolute().parent / "b901.py"
267285
bbc = BugBearChecker(filename=str(filename))

0 commit comments

Comments
 (0)