Skip to content

Commit 8823634

Browse files
committed
Add B044: assert <generator_expression>
1 parent 2f39911 commit 8823634

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

README.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,10 @@ If you define `__str__/__reduce__` in super classes this check is unable to dete
304304
**B043**: Do not call ``delattr(x, 'attr')``, instead use ``del x.attr``.
305305
There is no additional safety in using ``delattr`` if you know the attribute name ahead of time.
306306

307+
.. _B044:
308+
309+
**B044**: `assert <generator_expression>` is always true. Did you forget `all()`?
310+
307311

308312
Opinionated warnings
309313
~~~~~~~~~~~~~~~~~~~~
@@ -494,6 +498,11 @@ MIT
494498
Change Log
495499
----------
496500

501+
UNRELEASED
502+
~~~~~~~~~~
503+
504+
* B044: New check for `assert <generator_expression>`, which is always true (#534)
505+
497506
25.11.29
498507
~~~~~~~~
499508

bugbear.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@ def visit_GeneratorExp(self, node: ast.GeneratorExp) -> None:
622622

623623
def visit_Assert(self, node: ast.Assert) -> None:
624624
self.check_for_b011(node)
625+
self.check_for_b044(node)
625626
self.generic_visit(node)
626627

627628
def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef) -> None:
@@ -1887,6 +1888,10 @@ def is_exception(s: str):
18871888
return
18881889
# no `def __init__` found, which is fine
18891890

1891+
def check_for_b044(self, node: ast.Assert) -> None:
1892+
if isinstance(node.test, ast.GeneratorExp):
1893+
self.add_error("B044", node)
1894+
18901895
def check_for_b909(self, node: ast.For) -> None:
18911896
if isinstance(node.iter, ast.Name):
18921897
name = _to_name_str(node.iter)
@@ -2521,6 +2526,11 @@ def __call__(self, lineno: int, col: int, vars: tuple[object, ...] = ()) -> erro
25212526
"it is not any safer than normal property access."
25222527
)
25232528
),
2529+
"B044": Error(
2530+
message=(
2531+
"B044 `assert <generator_expression>` is always true. Did you forget `all()`?"
2532+
)
2533+
),
25242534
# Warnings disabled by default.
25252535
"B901": Error(
25262536
message=(

tests/eval_files/b044.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
assert (x for x in a) # B044: 0
2+
assert [x for x in a]
3+
assert {x for x in a}
4+
assert {x: y for x, y in a}

0 commit comments

Comments
 (0)