Skip to content

Proposed warning: assert <generator_expression> #534

Closed

Description

@gschaffner

For example:

assert (result[i] == expected[i] for i in range(len(expected)))
assert (result[key] == expected[key] for key in result.keys() | expected.keys())

bugbear could warn and suggest adding all().

Note that even an empty generator expression is truey:

assert (False for _ in range(0))

so it's fine that bugbear doesn't statically know the length of the generator.

There are a couple of similar warnings:

Activity

  1. JelleZijlstra commented on Dec 17, 2025

    @JelleZijlstra
    Collaborator

    Seems useful!

    This could even be added to Python itself as a SyntaxWarning.

  2. cooperlees commented on Dec 23, 2025

    @cooperlees
    Collaborator

    Sounds good to me too ... Thanks for the PR too. Will check it out.

  3. cooperlees commented on Sep 9, 2026

    @cooperlees
    Collaborator

    Merged a partial solution - we 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 ...

  4. added a commit that references this issue on Oct 13, 2026
    8c89e23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions