PyCQA / PyCQA/flake8-bugbear

Proposed warning: `assert <generator_expression>`

Open
#534 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.1k
Forks
123
Avg merge
2d 5h
Merged PRs (30d)
5

Description

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:

  • F631 is a similar rule for assert <tuple>.
  • Proposal (2) in #440 is a similar rule for assert <constant>.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review the existing F631 rule and the related proposal in #440 first, then trace how comparable assertion warnings are implemented. Done means adding a warning for assertions containing generator expressions and covering the suggested all() guidance, including empty generators, with tests for the examples described here.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.