-
-
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
False Positive on contextmanager-generator-missing-cleanup / W0135 When Using With Expression in the ContextManager #9625
Comments
Yes I agree, this seems like a false positive. Opinions @Pierre-Sassoulas @rhyn0? |
Yah that seems correct. Wondering if the way to check for this is by recursively tracing Worried about how a poorly written |
We have astroid inference to check that it's an actual open and not something else. We generally don't because shadowing builtin is really bad practice in general and the perf impact to everyone to prevent an edge case that should never happen is not negligible. |
My current understanding is that as long as there is no additional code after the Am I missing something? |
This is my mental model of the situation as well. What orginally triggered this in my code was not the |
Similar case: If I have code that is decorated with Explicitly wrapping it in try/finally with cleanup code consisting of "pass", feels wrong because that's just a pinky promise that cleanup is done.
|
It does no control flow analysis, and upgrading to pylint 3.2.0 produced many incorrect warnings. Also ignore contextmanager-generator-missing-cleanup for now. It has FP when there is no code after a yield (and thus no cleanup needs to be handled), which is what we do. Currently under discussion upstream: pylint-dev/pylint#9625
Opened #9654 which should at least address the most common false-positive case with a singular yield. @eli-schwartz I looked at your example. The fix won't work there unfortunately. Checking all |
Thanks for looking. I ended up adding it to pylintrc instead. |
It does no control flow analysis, and upgrading to pylint 3.2.0 produced many incorrect warnings. Also ignore contextmanager-generator-missing-cleanup for now. It has FP when there is no code after a yield (and thus no cleanup needs to be handled), which is what we do. Currently under discussion upstream: pylint-dev/pylint#9625 (cherry picked from commit e343590)
It does no control flow analysis, and upgrading to pylint 3.2.0 produced many incorrect warnings. Also ignore contextmanager-generator-missing-cleanup for now. It has FP when there is no code after a yield (and thus no cleanup needs to be handled), which is what we do. Currently under discussion upstream: pylint-dev/pylint#9625
Bug description
The
contextmanager-generator-missing-cleanup
warning triggers a false positive (by my understanding of the warning) when ayield
expression is fully cleaned up by a surroundingwith
statement (or multiple with statements).Below is a simplified example of the issue:
The warning can be silenced with the guidance from https://pylint.readthedocs.io/en/latest/user_guide/messages/warning/contextmanager-generator-missing-cleanup.html . But by my understanding this is an unnecessary warning in this case. The
with
statement incm()
does the 'right thing' whenyield
throwsGeneratorExit
and closes the file.Configuration
No response
Command used
Pylint output
Expected behavior
I would expect the
contextmanager-generator-missing-cleanup
to only warn when there is extra cleanup required in acontextmanager
outside ofwith
statements.I think you see the intention of these cases being supported in the
good_cm_yield_none()
example in https://pylint.readthedocs.io/en/latest/user_guide/messages/warning/contextmanager-generator-missing-cleanup.htmlIf there's nothing to clean up: the warning should not be generated.
Pylint version
OS / Environment
Ubuntu 22.04 LTS
Additional dependencies
No response
The text was updated successfully, but these errors were encountered: