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.
MIT License
1.05k stars 103 forks source link

fix(b909): Fix false positive affecting containers of mutables #469

Closed mimre25 closed 2 months ago

mimre25 commented 2 months ago

The false positives occurred when trying to edit a dictionary while iterating over a list of dictionaries:

lst: list[dict] = [{}, {}, {}]
for dic in lst:
    dic["key"] = False # was false positive - fixed now
henzef commented 2 months ago

With this patch flake8 complains about the following construct in my code:

for key in my_dictionary:
    my_dictionary[key] = True

which is safe to do, because it doesn't add or remove any keys. At least as far as I understand, please correct me if I am wrong.

cooperlees commented 2 months ago

Thanks for this. What's the plan, did you want to add this fix or do you have more plans for this PR?

mimre25 commented 2 months ago

I'd be happy to add the fix for

for key in my_dictionary:
    my_dictionary[key] = True

at the time I opened the draft PR I wasn't 100% sure this is desired, but judging from the discussions in #467 and the :+1: on henzef comment it seems like this is something we want.

I'll hopefully get around to adding this later today.

mimre25 commented 2 months ago

This is ready now :slightly_smiling_face:

FYI: tested it with both black and MONAI and there were no false positives. Test were done with py38 and py312

henzef commented 2 months ago

No false positives (or true positives :D) in my codebase either. Thanks for the quick fix!