-
Notifications
You must be signed in to change notification settings - Fork 103
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
Add B031: Warn when using groupby()
result multiple times
#347
Conversation
36ddd64
to
13fe5b5
Compare
Man, I suck @ the web edits :| |
Anyways, that aside, This is pretty standard behavior of generators or am I missing something here? I don't know if we want to cover every generator the standard library returns ... Thoughts there? If I'm missing something just say so ... I think mypy should maybe warn on this if it does not ... |
You're right, I had a very similar thought when making the PR. I made the PR anyways because of how surprised I was when I discovered the bug in my code, and how subtle the bug ended up being. I wasn't able to formulate it then – but thinking about it now –I think the special thing about Since |
From a brief look at the built-in Python functions and |
groupby()
result multiple timegroupby()
result multiple times
8be12ef
to
37a02b8
Compare
37a02b8
to
05a26c7
Compare
I rebased on |
This seems fine to me and "cheap" to add, so I guess it can't hurt to add. I would like a second opinion tho. Have been waiting, but requested another maintainer :) |
Thanks for your careful contribution, @malthejorgensen! Open source is made of this kind of generosity with time and skills, and we deeply appreciate it 😍 I haven't done a really deep review, but this looks fine to me - I think the strongest argument against would be along the lines of "we shouldn't add an incomplete check", and, well... basically every Python analysis is incomplete. Heck, most of the project is from people who had a bug, and said something like "never. again.!" so I find the patchwork nature kinda charming even besides several of the bugbears being mine. So... passes the "improves on status quo" test for me, let's merge 🚀 |
Thank you! 🙏🏻 |
FYI: #356 |
I ran into a somewhat subtle bug by accidentally iterating over the result of
itertools.groupby()
twice:I've written B031 (since there's a PR open for B030) to detect these types of erroneous usages.
There are likely other functions in
itertools
that return generators and should potentially be covered by this check.Also, the check is fairly rudimentary. It only works if the
groupby()
is used in a loop and unpacked directly into a tuple as shown above.Another subtle thing about
groupby()
is that it only groups when the items are sorted (but this doesn't cover that).