-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 false-positive consider-using-with
when using contextlib.ExitStack
#4665
Fix false-positive consider-using-with
when using contextlib.ExitStack
#4665
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I juste have a minor comment about the documentation, thanks a lot for the fix and for the clean tests and implementation !
…om ``_BaseExitStack`` until Python 3.7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Two small comments
@@ -62,6 +62,7 @@ New checkers | |||
Other Changes | |||
============= | |||
|
|||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty line :)
"contextlib.ExitStack.enter_context", # necessary for Python 3.6 compatibility | ||
) | ||
) | ||
parent = node.parent |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As it's just used two times, it's probably better to write out node.parent
directly. That way you don't have to think about it.
Thank you both for the feedback, the latest commit should cover all requested changes! |
Thank you for this fix, it will be released in 2.9.4 :) |
Steps
doc/whatsnew/<current release.rst>
.Description
The
contextlib.ExitStack
class provides a method to automatically call__exit__
on context managers added to its stack when leaving awith contextlib.ExitStack() as stack
block.If a callable that could be used in a
with
block was added to the stack, no R1732consider-using-with
message should be triggered:While
contextlib.AsyncExitStack
should theoretically also be covered (as it can be used with synchronous context managers as well), it is currently not possible to implement this, as trying to infer thestack.enter_context
returnsUninferable
in this case...I also noticed that the whole
consider-using-with
check does not check asynchronous context managers at all.But I guess this would exceed the scope of this PR, and should probably get its own message (e.g.
consider-using-async-with
).Type of Changes
Related Issue
Closes #4654