Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ If you define `__str__/__reduce__` in super classes this check is unable to dete
**B043**: Do not call ``delattr(x, 'attr')``, instead use ``del x.attr``.
There is no additional safety in using ``delattr`` if you know the attribute name ahead of time.

.. _B044:

**B044**: `assert <generator_expression>` is always true. Did you forget `all()`?


Opinionated warnings
~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -494,6 +498,11 @@ MIT
Change Log
----------

UNRELEASED
~~~~~~~~~~

* B044: New check for `assert <generator_expression>`, which is always true (#534)

25.11.29
~~~~~~~~

Expand Down
10 changes: 10 additions & 0 deletions bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ def visit_GeneratorExp(self, node: ast.GeneratorExp) -> None:

def visit_Assert(self, node: ast.Assert) -> None:
self.check_for_b011(node)
self.check_for_b044(node)
self.generic_visit(node)

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

def check_for_b044(self, node: ast.Assert) -> None:
if isinstance(node.test, ast.GeneratorExp):
self.add_error("B044", node)

def check_for_b909(self, node: ast.For) -> None:
if isinstance(node.iter, ast.Name):
name = _to_name_str(node.iter)
Expand Down Expand Up @@ -2521,6 +2526,11 @@ def __call__(self, lineno: int, col: int, vars: tuple[object, ...] = ()) -> erro
"it is not any safer than normal property access."
)
),
"B044": Error(
message=(
"B044 `assert <generator_expression>` is always true. Did you forget `all()`?"
)
),
# Warnings disabled by default.
"B901": Error(
message=(
Expand Down
4 changes: 4 additions & 0 deletions tests/eval_files/b044.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
assert (x for x in a) # B044: 0
assert [x for x in a]
assert {x for x in a}
assert {x: y for x, y in a}
Comment on lines +1 to +4
Copy link

Copilot AI Dec 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test file should include a test case for assert <generator_expression>, "message" to ensure the check works correctly when the assert statement includes an error message. This is a common pattern in assertions and should be tested, similar to how B011 tests both assert False and assert False, "message".

Copilot uses AI. Check for mistakes.
Comment on lines +1 to +4
Copy link

Copilot AI Dec 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding test cases for edge cases such as:

  • Nested generator expressions: assert (x for x in (y for y in a))
  • Generator expressions in boolean context with 'and'/'or': assert some_condition and (x for x in a)
  • Generator expressions with conditions: assert (x for x in a if x > 0)

These scenarios would help ensure the check correctly identifies all problematic patterns.

Copilot uses AI. Check for mistakes.