Skip to content

Commit 9f7c082

Browse files
authored
Merge pull request #45 from gaul/b009-assert-false
Introduce B011
2 parents 29bc9b9 + 074d587 commit 9f7c082

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

README.rst

+3
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ ahead of time.
9090
property access: ``x.attr = val``. There is no additional safety in
9191
using ``setattr`` if you know the attribute name ahead of time.
9292

93+
**B011**: Do not call `assert False` since `python -O` removes these calls.
94+
Instead callers should `raise AssertionError()`.
95+
9396

9497
Python 3 compatibility warnings
9598
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

bugbear.py

+12
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ def visit_For(self, node):
207207
self.check_for_b007(node)
208208
self.generic_visit(node)
209209

210+
def visit_Assert(self, node):
211+
self.check_for_b011(node)
212+
self.generic_visit(node)
213+
210214
def visit_AsyncFunctionDef(self, node):
211215
self.check_for_b902(node)
212216
self.check_for_b006(node)
@@ -272,6 +276,10 @@ def check_for_b007(self, node):
272276
n = targets.names[name][0]
273277
self.errors.append(B007(n.lineno, n.col_offset, vars=(name,)))
274278

279+
def check_for_b011(self, node):
280+
if isinstance(node.test, ast.NameConstant) and node.test.value is False:
281+
self.errors.append(B011(node.lineno, node.col_offset))
282+
275283
def check_for_b901(self, node):
276284
xs = list(node.body)
277285
has_yield = False
@@ -493,6 +501,10 @@ def visit(self, node):
493501
message="B010 Do not call setattr with a constant attribute value, "
494502
"it is not any safer than normal property access."
495503
)
504+
B011 = Error(
505+
message="B011 Do not call assert False since python -O removes these calls. "
506+
"Instead callers should raise AssertionError()."
507+
)
496508

497509

498510
# Those could be false positives but it's more dangerous to let them slip

tests/b011.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""
2+
Should emit:
3+
B011 - on line 8
4+
B011 - on line 10
5+
"""
6+
7+
assert 1 != 2
8+
assert False
9+
assert 1 != 2, "message"
10+
assert False, "message"

tests/test_bugbear.py

+10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
B008,
1515
B009,
1616
B010,
17+
B011,
1718
B301,
1819
B302,
1920
B303,
@@ -110,6 +111,15 @@ def test_b009_b010(self):
110111
errors = list(bbc.run())
111112
self.assertEqual(errors, self.errors(B009(15, 0), B010(22, 0)))
112113

114+
def test_b011(self):
115+
filename = Path(__file__).absolute().parent / 'b011.py'
116+
bbc = BugBearChecker(filename=str(filename))
117+
errors = list(bbc.run())
118+
self.assertEqual(
119+
errors,
120+
self.errors(B011(8, 0, vars=('i',)), B011(10, 0, vars=('k',))),
121+
)
122+
113123
def test_b301_b302_b305(self):
114124
filename = Path(__file__).absolute().parent / "b301_b302_b305.py"
115125
bbc = BugBearChecker(filename=str(filename))

0 commit comments

Comments
 (0)