Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

B031 flagged on non-itertools groupby #356

Open
eachimei opened this issue Feb 15, 2023 · 3 comments
Open

B031 flagged on non-itertools groupby #356

eachimei opened this issue Feb 15, 2023 · 3 comments

Comments

@eachimei
Copy link

eachimei commented Feb 15, 2023

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?

@malthejorgensen
Copy link
Contributor

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 🤷‍♂️

@eachimei
Copy link
Author

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...

@eachimei
Copy link
Author

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

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

No branches or pull requests

3 participants