Skip to content

Commit a2e0c95

Browse files
authored
B016: Warn when raising f-strings (#341)
1 parent 37e7b28 commit a2e0c95

File tree

4 files changed

+7
-3
lines changed

4 files changed

+7
-3
lines changed

README.rst

+1
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ Future
319319
functions that are ignored by the B906 check. The ``ast.Bytes``, ``ast.Num`` and
320320
``ast.Str`` nodes are all deprecated, but may still be used by some codebases in
321321
order to maintain backwards compatibility with Python 3.7.
322+
* B016: Warn when raising f-strings.
322323

323324
23.1.20
324325
~~~~~~~~~

bugbear.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ def check_for_b015(self, node):
516516
self.errors.append(B015(node.lineno, node.col_offset))
517517

518518
def check_for_b016(self, node):
519-
if isinstance(node.exc, (ast.NameConstant, ast.Num, ast.Str)):
519+
if isinstance(node.exc, (ast.NameConstant, ast.Num, ast.Str, ast.JoinedStr)):
520520
self.errors.append(B016(node.lineno, node.col_offset))
521521

522522
def check_for_b017(self, node):

tests/b016.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
"""
22
Should emit:
3-
B016 - on lines 6, 7, and 8
3+
B016 - on lines 6, 7, 8, and 10
44
"""
55

66
raise False
77
raise 1
88
raise "string"
9+
fstring = "fstring"
10+
raise f"fstring {fstring}"
911
raise Exception(False)
1012
raise Exception(1)
1113
raise Exception("string")
14+
raise Exception(f"fstring {fstring}")

tests/test_bugbear.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def test_b016(self):
247247
filename = Path(__file__).absolute().parent / "b016.py"
248248
bbc = BugBearChecker(filename=str(filename))
249249
errors = list(bbc.run())
250-
expected = self.errors(B016(6, 0), B016(7, 0), B016(8, 0))
250+
expected = self.errors(B016(6, 0), B016(7, 0), B016(8, 0), B016(10, 0))
251251
self.assertEqual(errors, expected)
252252

253253
def test_b017(self):

0 commit comments

Comments
 (0)