From a2e0c951146300f507c11e4f2bf7d22df7368129 Mon Sep 17 00:00:00 2001 From: George Date: Mon, 30 Jan 2023 15:01:26 +0000 Subject: [PATCH] B016: Warn when raising f-strings (#341) --- README.rst | 1 + bugbear.py | 2 +- tests/b016.py | 5 ++++- tests/test_bugbear.py | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 0bb9789..fe72e3c 100644 --- a/README.rst +++ b/README.rst @@ -319,6 +319,7 @@ Future functions that are ignored by the B906 check. The ``ast.Bytes``, ``ast.Num`` and ``ast.Str`` nodes are all deprecated, but may still be used by some codebases in order to maintain backwards compatibility with Python 3.7. +* B016: Warn when raising f-strings. 23.1.20 ~~~~~~~~~ diff --git a/bugbear.py b/bugbear.py index 6c65f4d..a6b883b 100644 --- a/bugbear.py +++ b/bugbear.py @@ -516,7 +516,7 @@ def check_for_b015(self, node): self.errors.append(B015(node.lineno, node.col_offset)) def check_for_b016(self, node): - if isinstance(node.exc, (ast.NameConstant, ast.Num, ast.Str)): + if isinstance(node.exc, (ast.NameConstant, ast.Num, ast.Str, ast.JoinedStr)): self.errors.append(B016(node.lineno, node.col_offset)) def check_for_b017(self, node): diff --git a/tests/b016.py b/tests/b016.py index cc023a0..f12f468 100644 --- a/tests/b016.py +++ b/tests/b016.py @@ -1,11 +1,14 @@ """ Should emit: -B016 - on lines 6, 7, and 8 +B016 - on lines 6, 7, 8, and 10 """ raise False raise 1 raise "string" +fstring = "fstring" +raise f"fstring {fstring}" raise Exception(False) raise Exception(1) raise Exception("string") +raise Exception(f"fstring {fstring}") diff --git a/tests/test_bugbear.py b/tests/test_bugbear.py index 2e672cb..0b91a06 100644 --- a/tests/test_bugbear.py +++ b/tests/test_bugbear.py @@ -247,7 +247,7 @@ def test_b016(self): filename = Path(__file__).absolute().parent / "b016.py" bbc = BugBearChecker(filename=str(filename)) errors = list(bbc.run()) - expected = self.errors(B016(6, 0), B016(7, 0), B016(8, 0)) + expected = self.errors(B016(6, 0), B016(7, 0), B016(8, 0), B016(10, 0)) self.assertEqual(errors, expected) def test_b017(self):