Skip to content

B017, B022: check every item of a multi-item with statement - #646

Open
shivangithaker-0510 wants to merge 1 commit into
PyCQA:mainfrom
shivangithaker-0510:fix/b017-b022-all-with-items
Open

shivangithaker-0510 wants to merge 1 commit into
PyCQA:mainfrom
shivangithaker-0510:fix/b017-b022-all-with-items

Conversation

@shivangithaker-0510

Copy link
Copy Markdown

B017 and B022 only look at the first item of a with statement (node.items[0]), so they miss the offending context manager whenever it isn't listed first:

with open(path), pytest.raises(Exception):  # no B017
    ...

with open(path), contextlib.suppress():  # no B022
    ...

B908 already loops over all node.items; this does the same for B017 and B022. Most of the diff is just re-indentation (git diff -w is 4 lines).

Before: neither line above is reported. After: B017 / B022 are reported on them, same as when the context manager is the only item. Added cases to tests/eval_files/b017.py and b022.py (including a with open(...) as f, pytest.raises(Exception) as ex: that should stay clean) and a changelog line under UNRELEASED.

Used Claude to help write this; I reviewed it and ran the tests.

Both checks only looked at node.items[0], so
`with open(path), pytest.raises(Exception):` and
`with open(path), contextlib.suppress():` were not reported.
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 this pull request may close these issues.

1 participant