[pyupgrade] Fix false positive when class name is shadowed by local variable (UP008) (#20427)

## Summary

Fixes #20422

---------

Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
This commit is contained in:
Dan Parizher 2025-09-18 10:05:05 -04:00 committed by GitHub
parent 1ebbe73a1d
commit 91995aa516
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 1 deletions

View file

@ -271,3 +271,19 @@ class ChildI9(ParentI):
if False: super if False: super
if False: __class__ if False: __class__
builtins.super(ChildI9, self).f() builtins.super(ChildI9, self).f()
# See: https://github.com/astral-sh/ruff/issues/20422
# UP008 should not apply when the class variable is shadowed
class A:
def f(self):
return 1
class B(A):
def f(self):
return 2
class C(B):
def f(self):
C = B # Local variable C shadows the class name
return super(C, self).f() # Should NOT trigger UP008

View file

@ -139,7 +139,11 @@ pub(crate) fn super_call_with_parameters(checker: &Checker, call: &ast::ExprCall
return; return;
}; };
if !((first_arg_id == "__class__" || first_arg_id == parent_name.as_str()) if !((first_arg_id == "__class__"
|| (first_arg_id == parent_name.as_str()
// If the first argument matches the class name, check if it's a local variable
// that shadows the class name. If so, don't apply UP008.
&& !checker.semantic().current_scope().has(first_arg_id)))
&& second_arg_id == parent_arg.name().as_str()) && second_arg_id == parent_arg.name().as_str())
{ {
return; return;