Skip to content

Commit dbb0d60

Browse files
authored
[pyupgrade] Fix super(__class__, self) detection in UP008 (super-call-with-parameters) (#18478)
1 parent ef4108a commit dbb0d60

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

crates/ruff_linter/resources/test/fixtures/pyupgrade/UP008.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,14 @@ def normal(self):
7979
def normal(self):
8080
super(DataClass, self).f() # OK
8181
super().f() # OK (`TypeError` in practice)
82+
83+
84+
# see: https://github.com/astral-sh/ruff/issues/18477
85+
class A:
86+
def foo(self):
87+
pass
88+
89+
90+
class B(A):
91+
def bar(self):
92+
super(__class__, self).foo()

crates/ruff_linter/src/rules/pyupgrade/rules/super_call_with_parameters.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ pub(crate) fn super_call_with_parameters(checker: &Checker, call: &ast::ExprCall
121121
return;
122122
};
123123

124-
if !(first_arg_id == parent_name.as_str() && second_arg_id == parent_arg.name().as_str()) {
124+
if !((first_arg_id == "__class__" || first_arg_id == parent_name.as_str())
125+
&& second_arg_id == parent_arg.name().as_str())
126+
{
125127
return;
126128
}
127129

crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP008.py.snap

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
source: crates/ruff_linter/src/rules/pyupgrade/mod.rs
3-
snapshot_kind: text
43
---
54
UP008.py:17:23: UP008 [*] Use `super()` instead of `super(__class__, self)`
65
|
@@ -126,4 +125,20 @@ UP008.py:74:14: UP008 [*] Use `super()` instead of `super(__class__, self)`
126125
74 |+ super().f() # Error
127126
75 75 | super().f() # OK
128127
76 76 |
129-
77 77 |
128+
77 77 |
129+
130+
UP008.py:92:14: UP008 [*] Use `super()` instead of `super(__class__, self)`
131+
|
132+
90 | class B(A):
133+
91 | def bar(self):
134+
92 | super(__class__, self).foo()
135+
| ^^^^^^^^^^^^^^^^^ UP008
136+
|
137+
= help: Remove `__super__` parameters
138+
139+
Unsafe fix
140+
89 89 |
141+
90 90 | class B(A):
142+
91 91 | def bar(self):
143+
92 |- super(__class__, self).foo()
144+
92 |+ super().foo()

0 commit comments

Comments
 (0)