PyCQA / PyCQA/flake8-bugbear

B031 flagged on non-itertools groupby

Open
#356 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Recent B031 addition: https://github.com/PyCQA/flake8-bugbear/pull/347

A code that defines a groupby function (not itertools.groupby) will still raise B031 error.

Example:

def groupby(x, y):
    return dict().items()


for _, v in groupby(None, None):
    if v:
        ...
    if v:
        ...

Then:

$ flake8 example.py
example.py:8:8: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage. Save the result to a list, if the result is needed multiple times.

flake8: 6.0.0, flake8-bugbear: 23.2.13

Perhaps we can have a better inspection so the error is not raised in such cases?

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

Reproduce the supplied example with flake8 and flake8-bugbear, then locate the B031 implementation and its existing tests. Compare the handling of a locally defined groupby with itertools.groupby; done means the local function no longer produces B031 while the intended itertools.groupby warning remains covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.