[pyupgrade] Fix UP008 to not apply when __class__ is a local variable (UP008) (#20497)

## Summary

Fixes #20491
This commit is contained in:
Dan Parizher
2025-09-23 10:56:39 -04:00
committed by GitHub
parent bf38e69870
commit 46decd4feb
2 changed files with 21 additions and 5 deletions

View File

@@ -287,3 +287,19 @@ class C(B):
def f(self):
C = B # Local variable C shadows the class name
return super(C, self).f() # Should NOT trigger UP008
# See: https://github.com/astral-sh/ruff/issues/20491
# UP008 should not apply when __class__ is a local variable
class A:
def f(self):
return 1
class B(A):
def f(self):
return 2
class C(B):
def f(self):
__class__ = B # Local variable __class__ shadows the implicit __class__
return super(__class__, self).f() # Should NOT trigger UP008

View File

@@ -139,11 +139,11 @@ pub(crate) fn super_call_with_parameters(checker: &Checker, call: &ast::ExprCall
return;
};
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)))
// The `super(__class__, self)` and `super(ParentClass, self)` patterns are redundant in Python 3
// when the first argument refers to the implicit `__class__` cell or to the enclosing class.
// Avoid triggering if a local variable shadows either name.
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;