PyCQA / flake8-bugbear

A plugin for Flake8 finding likely bugs and design problems in your program. Contains warnings that don't belong in pyflakes and pycodestyle.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

B031 flagged on non-itertools groupby

eachimei opened this issue · comments

Recent B031 addition: #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?

Just noting B905 has the same issue for zip.

If many people define their own groupby I wonder if the rule should be off by default 🤷‍♂️

I guess it's not that common and I was "lucky" enough 😅... funny thing is, I wanted a non-generator variant of groupby (that also doesn't require a sorted input) so created one for my project, and then CI errored out on flake8 and hence reported this issue...

Anyway, I refactored my code (renamed the utility). Not a big problem since it's an internal (private) utility.