-
-
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
Fix #85, #2615: Emit used-before-assignment
in final or except blocks where try statements could have failed
#5384
Fix #85, #2615: Emit used-before-assignment
in final or except blocks where try statements could have failed
#5384
Conversation
da2aefa
to
df5c775
Compare
I actually had an interesting discussion about this with one of the maintainers of Basically it comes down to whether we want to assume all Personally I'm inclined to say let's assume it is succeeds. As a user I will be more annoyed by having to add |
Pull Request Test Coverage Report for Build 1570790157
💛 - Coveralls |
Thanks for the speedy reply!
So my suggestion here for the user would be to just define the variables before the try rather than add disables.
I agree this is a design question. I think this approach splits the difference well. We assume the try statements fail for the sake of finally statements relying on names that might not exist, which is easy for users to fix by initializing their variables, but we assume the try statements succeed for the sake of evaluating I know it's not surveying opinions on the other side very well, but there's 13 +1's on the ticket. I tend to agree with the Pylance maintainer that if you're depending on a name in the finally block, then that really is an error--either something should be moved out of the try, or else the variable should be initialized before the try. |
I found this argument from Eric Traut pretty convincing, honestly. By adding a statement in a Try except the user tell us that the code can fail so we should probably treat the code as possibly failing. If something never fail it should be outside the try and the resulting code will be clearer and better. |
Does it make sense to create If not, I think we should go with the majority opinion and continue with this PR! |
df5c775
to
eba3d3c
Compare
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.
LGTM,! Thank you. I think that taking the same decision than pylance is the safe bet here. But I'm putting this in 2.13 so opinion can be heard about the controversial part of the issue if required.
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.
Small changes here and there! Rest looks good! Glad to see such an old issue finally resolved even if I did not initially agree with the solution. I'm must admit I do think this might be the best solution for this problem going forward. Thanks @jacobtylerwalls for putting this to our attention! 😄
Let's not merge until I solve #2615 with a very similar block. I think I can use the same branch. |
used-before-assignment
in final blocks where try statements could have failedused-before-assignment
in final or except blocks where try statements could have failed
@jacobtylerwalls , we merged #5402 (🎉), but I'm not sure how to resolve the conflict, could you rebase on the latest main, please ? |
…final or except blocks where try statements could have failed
e9ffbd5
to
228f56f
Compare
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.
Thanks! Most of this already looks good.
I thought about node.statement
previously but never commented about it. Upon re-review I think it makes sense.
pylint/checkers/variables.py
Outdated
@@ -405,7 +405,8 @@ def _has_locals_call_after_node(stmt, scope): | |||
"E0601": ( | |||
"Using variable %r before assignment", | |||
"used-before-assignment", | |||
"Used when a local variable is accessed before its assignment.", | |||
"Used when a local variable is accessed before its assignment or within " |
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.
Let's merge this in the current state and use #5506 to perfect this description 😄
Type of Changes
Description
Previously
Pylint assumed assignments in the Try block of a Try/Finally or Try/Except pair would succeed. In cases of failure, though, attempting to use such names in a final or except block would raise
UnboundLocalError
.Now
Pylint emits
used-before-assignment
to account for the fact that the statement might have been interrupted by an exception.Closes #85, Closes #2615