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

B909 has several false positives on black codebase #467

Open
hauntsaninja opened this issue Apr 22, 2024 · 7 comments
Open

B909 has several false positives on black codebase #467

hauntsaninja opened this issue Apr 22, 2024 · 7 comments

Comments

@hauntsaninja
Copy link
Contributor

E.g.

def lib2to3_parse():
    grammars = get_grammars()
    errors = {}
    for grammar in grammars:
        errors[grammar.version] = InvalidInput()

See https://github.com/psf/black/actions/runs/8776854301/job/24081105618?pr=4318 for more

cc @mimre25 in case interested in improving the check :-)

KumoLiu added a commit to Project-MONAI/MONAI that referenced this issue Apr 22, 2024
Workaround for #7690

### Description
Updated with flake8-bugbear, causing the B909 error. A workaround fix
was made for the flake8-bugbear version because the 24.4.21 update
contained some false positives.
Fore more information, see
PyCQA/flake8-bugbear#467

### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [ ] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [ ] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [ ] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
@mimre25
Copy link
Contributor

mimre25 commented Apr 23, 2024

Thanks for tagging me!

I'll take a look later this week and see if I can fix it right away, or if it blows up the scope (I doubt the latter one). 🙂

@gothicVI
Copy link

Hi,

we're seeing complaints for code like

lst: list[dict] = [dic1, ..., dicn]
for dic in lst:
    dic["key"] = False

Is this related?

@mimre25
Copy link
Contributor

mimre25 commented Apr 25, 2024

Potentially. I'm actually working on it right now, so I'll also check this.

@mimre25
Copy link
Contributor

mimre25 commented Apr 25, 2024

I've created #469 - the fix was rather easy and simple, just an oversight in the initial implementation 😬

I've ran it on the black code base with the only hit being ./black/src/blib2to3/pgen2/pgen.py:235:21, which is a true positive considering the comment in line 223 (for state in states: # NB states grows while we're iterating).

With Project-MONAI, I could also eliminate the cases pointed out, however, there is a discussion point (hence the PR only being a draft):

Should B909 allow altering an element via it's dictionary key? For example:

some_dict = {...}
for key in some_dict:
    some_dict[key] = 3
    del some_dict[key]

Should this fire B909 or not? What do you think?

cc @cooperlees

@gothicVI
Copy link

gothicVI commented Apr 25, 2024

Should B909 allow altering an element via it's dictionary key? For example:

some_dict = {...}
for key in some_dict:
    some_dict[key] = 3
    del some_dict[key]

I'd argue it should fire due to the del.

EDIT: The following is incorrect and shouldn't be considered - I leave it here for completeness as it was partially quoted:

However, I'd also argue it shouldn't in such a case:

some_dict = {...}
for key in some_dict.keys():  # this creates a new object that no longer depends on the original
    some_dict[key] = 3
    del some_dict[key]

@JelleZijlstra
Copy link
Collaborator

this creates a new object that no longer depends on the original

That's not true in Python 3. Your code will throw RuntimeError: dictionary changed size during iteration.

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

No branches or pull requests

5 participants