[pyupgrade] Fix false positive when class name is shadowed by local variable (UP008)#20427
Merged
ntBre merged 3 commits intoastral-sh:mainfrom Sep 18, 2025
Merged
[pyupgrade] Fix false positive when class name is shadowed by local variable (UP008)#20427ntBre merged 3 commits intoastral-sh:mainfrom
pyupgrade] Fix false positive when class name is shadowed by local variable (UP008)#20427ntBre merged 3 commits intoastral-sh:mainfrom
Conversation
Contributor
|
ntBre
reviewed
Sep 17, 2025
Contributor
ntBre
left a comment
There was a problem hiding this comment.
Thanks! I just had a minor simplification suggestion, but this looks good to me.
Comment on lines
+150
to
+155
| if first_arg_id == parent_name.as_str() { | ||
| let scope = checker.semantic().current_scope(); | ||
| if scope.has(first_arg_id) { | ||
| return; | ||
| } | ||
| } |
Contributor
There was a problem hiding this comment.
Can we combine this with the other first_arg_id == parent_name.as_str() check to avoid having to check that twice?
if !((first_arg_id == "__class__"
|| (first_arg_id == parent_name.as_str()
&& !checker.semantic().current_scope().has(first_arg_id)))
&& second_arg_id == parent_arg.name().as_str())
{
return;
}This condition is definitely getting a bit unwieldy, but I don't think it's too much worse.
An alternative approach here would be to resolve the binding of the first argument name and compare that to the binding of the parent class, but I think this check is okay because any intervening scope should also disable the rule. The current approach is a bit simpler.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #20422