-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 consider-using-with
in if
/else
block
#4751
Comments
The issue seems to be that when the second assignment is encountered:
the checker thinks it's being reassigned, despite the fact that the program can't possibly hit both assignments in the same run. I've had a quick look at the existing checkers and I'm not sure if this is something that can be addressed easily. It doesn't seem like Pylint / Astroid as it stands has any logic for dealing with the case that something is conditionally defined in one arm of an For example:
doesn't report (possibly) uninitialised variable. Is this correct? If so, then this isn't really specific to the |
You are definitely correct that the Problem is in the else clause. I have an idea how to solve it for this check, but that would not solve the general problem you described. |
The problem with the Now for But that is just a solution to this check and not for others. a = 1
a = 2 # <-- no message here
print(a) Edit: might benefit from results of #4795. |
Bug description
Given a file
a.py
:(Configuration)
No response
Command used
Result of
pylint a.py
:(Expected behavior)
No message
consider-using-with
, asfile_handle
is used in a subsequentwith
block.#4721 implemented changes that already suppress this message for things like:
So naturally the example code above should also not trigger the message.
(Version affected)
(OS / Environment)
macOS BigSur 11.4
(Additional dependencies)
No response
The text was updated successfully, but these errors were encountered: