Skip to content

Commit

Permalink
Add check for raising a literal (#141)
Browse files Browse the repository at this point in the history
This is invalid and the user likely intended to return instead.
GitHub code search is not great but shows a few instances of this:

https://github.com/search?l=Python&p=1&q=%22raise+False%22&type=Code
  • Loading branch information
gaul committed Oct 19, 2020
1 parent 2e3399f commit 4a6de12
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ Write ``except Exception:``, which catches exactly the same exceptions.
**B015**: Pointless comparison. This comparison does nothing but
wastes CPU instructions. Remove it.

**B016**: Cannot raise a literal. Did you intend to return it or raise
an Exception?


Python 3 compatibility warnings
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -246,6 +249,11 @@ MIT
Change Log
----------

Next version
~~~~~~~~~~~~

* Introduce B016 to check for raising a literal. (#141)

20.1.4
~~~~~~

Expand Down
14 changes: 14 additions & 0 deletions bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ def visit_Try(self, node):
def visit_Compare(self, node):
self.check_for_b015(node)

def visit_Raise(self, node):
self.check_for_b016(node)
self.generic_visit(node)

def compose_call_path(self, node):
if isinstance(node, ast.Attribute):
yield from self.compose_call_path(node.value)
Expand Down Expand Up @@ -387,6 +391,10 @@ def check_for_b015(self, node):
if isinstance(self.node_stack[-2], ast.Expr):
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)):
self.errors.append(B016(node.lineno, node.col_offset))

def walk_function_body(self, node):
def _loop(parent, node):
if isinstance(node, (ast.AsyncFunctionDef, ast.FunctionDef)):
Expand Down Expand Up @@ -680,6 +688,12 @@ def visit(self, node):
"CPU instructions. Remove it."
)
)
B016 = Error(
message=(
"B016 Cannot raise a literal. Did you intend to return it or raise "
"an Exception?"
)
)

# Those could be false positives but it's more dangerous to let them slip
# through if they're not.
Expand Down
11 changes: 11 additions & 0 deletions tests/b016.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""
Should emit:
B016 - on lines 6, 7, and 8
"""

raise False
raise 1
raise "string"
raise Exception(False)
raise Exception(1)
raise Exception("string")
8 changes: 8 additions & 0 deletions tests/test_bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
B013,
B014,
B015,
B016,
B301,
B302,
B303,
Expand Down Expand Up @@ -194,6 +195,13 @@ def test_b015(self):
expected = self.errors(B015(8, 0), B015(12, 0), B015(22, 4), B015(29, 4))
self.assertEqual(errors, expected)

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))
self.assertEqual(errors, expected)

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

0 comments on commit 4a6de12

Please sign in to comment.