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

Fix crash if a callable returning a context manager was assigned to a list or dict item #4733

Merged
merged 2 commits into from Jul 21, 2021

Conversation

DudeNr33
Copy link
Collaborator

Steps

  • Add a ChangeLog entry describing what your PR does.
  • If it's a new feature or an important bug fix, add a What's New entry in
    doc/whatsnew/<current release.rst>.
  • Write a good description on what the PR does.

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 to consider-using-with to be triggered when visit_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:

import subprocess

job_dict = {}
job_dict["new"] = subprocess.Popen("ls")  # will currently emit consider-using-with

# ... 

for name, job in job_dict.items():
    with job:  # we now actually use the context manager, thus it would not have been necessary to emit consider-using-with earlier
        output = job.communicate()

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

Type
🐛 Bug fix

Related Issue

Closes #4732

@coveralls
Copy link

Coverage Status

Coverage remained the same at 92.09% when pulling 6833c32 on DudeNr33:fix-4732 into 970c5d2 on PyCQA:main.

@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.9.5 milestone Jul 21, 2021
@Pierre-Sassoulas Pierre-Sassoulas added the Crash 💥 A bug that makes pylint crash label Jul 21, 2021
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think not handling the false positive is fine for now as we're aiming to suppress the crash first. Thank you for this fix 👍

@Pierre-Sassoulas Pierre-Sassoulas merged commit a2c166c into pylint-dev:main Jul 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Crash 💥 A bug that makes pylint crash
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pylint 2.9.4 failure for 'Subscript' object has no attribute 'attrname'
3 participants