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

B038 false positives #455

Closed
xqt opened this issue Jan 19, 2024 · 5 comments · Fixed by #456
Closed

B038 false positives #455

xqt opened this issue Jan 19, 2024 · 5 comments · Fixed by #456

Comments

@xqt
Copy link

xqt commented Jan 19, 2024

having the following code;

 mylist = list(range(5))
 for item in mylist:
    if item == 2:
        mylist.remove(item)  # <- badly detects B038 here
        break

bugbear badly detects B038.

Sample: https://doc.wikimedia.org/pywikibot/master/_modules/proofreadpage.html#ProofreadPage
in index property (search for def index). As B038 is descibed as Found a mutation of a mutable loop iterable inside the loop body. Changes to the iterable of a loop such as calls to list.remove() or via del can cause unintended bugs. This rule should be an optionated warning instead of a general rule.

@JelleZijlstra
Copy link
Collaborator

cc @mimre25.

I think we can fix this by making the rule not trigger if the mutation is unconditionally followed by a break (or return or raise).

@mimre25
Copy link
Contributor

mimre25 commented Jan 20, 2024

Oh this is good one.

I think excluding mutations that are followed by a break (unconditionally) sounds like a good fix.

Should I loop this into #454 ?

@xqt
Copy link
Author

xqt commented Jan 20, 2024

I also found an issue when iterating over a CookieJar instance where __iter__ already uses a copy with deepvalues method. And a third false positive when iterating over a DequeGenerator within our repository:

Therefore I propose to make this rule optional.

@mimre25
Copy link
Contributor

mimre25 commented Jan 21, 2024

Oh my... with all those false positives stemming from the standard lib, I also think it's best to make the rule optional.

What do you think @JelleZijlstra @cooperlees?

@cooperlees
Copy link
Collaborator

I'll accept the PR if we all feel we should move this to a B9XX optional due to our learning here. Thanks all!

mimre25 added a commit to mimre25/flake8-bugbear that referenced this issue Jan 22, 2024
B038 lead to some false positives that stem from methods defined in the
standard library that have the same name as mutating functions for
container types like lists and dicts.
Thus we decided to make this rule optional.
See PyCQA#455 for the related
discussion.
cooperlees pushed a commit that referenced this issue Feb 7, 2024
B038 lead to some false positives that stem from methods defined in the
standard library that have the same name as mutating functions for
container types like lists and dicts.
Thus we decided to make this rule optional.
See #455 for the related
discussion.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants