Fix crash if a callable returning a context manager was assigned to a list or dict item #4733
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Steps
doc/whatsnew/<current release.rst>
.Description
#4721 introduced a crash if a context manager was not assigned to a variable or attribute, but instead to something like a list or dictionary item.
_append_context_managers_to_stack()
now ignores assignments where the left hand side is not assigned to something with a name. This will lead toconsider-using-with
to be triggered whenvisit_call()
is called.Note that this would have been the same behaviour as before #4721, so this is not a step back.
Of course it would be desirable if it is possible to properly detect if the list/dict item is extracted later on and used in a
with
block, like so:If anybody has an idea how to detect this, I'll be happy to implement this.
I'm also guessing that this would probably go in a 2.9.5? If not I'll change the ChangeLog and whatsnew entry again.
Type of Changes
Related Issue
Closes #4732