Skip to content

Conversation

@gschaffner
Copy link
Contributor

Closes #534.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a new flake8-bugbear check B044 to detect assert <generator_expression> patterns, which are always truthy and likely indicate a bug where the developer forgot to wrap the generator in all() or any().

Key Changes:

  • Adds check B044 to detect generator expressions used directly in assert statements
  • Includes test cases covering generator expressions and other comprehension types

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
tests/eval_files/b044.py Adds test cases for B044 check, including positive case (generator expression) and negative cases (list/set/dict comprehensions)
bugbear.py Implements check_for_b044() method, integrates it into visit_Assert(), and defines the B044 error message
README.rst Documents the new B044 check and adds changelog entry

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +1 to +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}
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
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}
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.
@JelleZijlstra
Copy link
Collaborator

I wonder if it would make sense to broaden this into asserts with other constructs that we can statically detect will always be true, such as nonempty list literals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proposed warning: assert <generator_expression>

2 participants