mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 06:11:21 +00:00
[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:
parent
1ebbe73a1d
commit
91995aa516
2 changed files with 21 additions and 1 deletions
|
@ -271,3 +271,19 @@ class ChildI9(ParentI):
|
|||
if False: super
|
||||
if False: __class__
|
||||
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
|
||||
|
|
|
@ -139,7 +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 !((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())
|
||||
{
|
||||
return;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue