Skip to content

Commit cff5adf

Browse files
authored
[pyupgrade] Suppress UP008 diagnostic if super symbol is not builtin (#18688)
<!-- Thank you for contributing to Ruff/ty! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? (Please prefix with `[ty]` for ty pull requests.) - Does this pull request include references to any relevant issues? --> ## Summary Fixes #18684 <!-- What's the purpose of the change? What does it do, and why? --> ## Test Plan Add regression test <!-- How was it tested? -->
1 parent 7880a20 commit cff5adf

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,19 @@ def foo(self):
8989

9090
class B(A):
9191
def bar(self):
92-
super(__class__, self).foo()
92+
super(__class__, self).foo()
93+
94+
95+
# see: https://github.com/astral-sh/ruff/issues/18684
96+
class C:
97+
def f(self):
98+
super = print
99+
super(C, self)
100+
101+
102+
import builtins
103+
104+
105+
class C:
106+
def f(self):
107+
builtins.super(C, self)

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl AlwaysFixableViolation for SuperCallWithParameters {
6666
pub(crate) fn super_call_with_parameters(checker: &Checker, call: &ast::ExprCall) {
6767
// Only bother going through the super check at all if we're in a `super` call.
6868
// (We check this in `super_args` too, so this is just an optimization.)
69-
if !is_super_call_with_arguments(call) {
69+
if !is_super_call_with_arguments(call, checker) {
7070
return;
7171
}
7272
let scope = checker.semantic().current_scope();
@@ -167,10 +167,6 @@ pub(crate) fn super_call_with_parameters(checker: &Checker, call: &ast::ExprCall
167167
}
168168

169169
/// Returns `true` if a call is an argumented `super` invocation.
170-
fn is_super_call_with_arguments(call: &ast::ExprCall) -> bool {
171-
if let Expr::Name(ast::ExprName { id, .. }) = call.func.as_ref() {
172-
id == "super" && !call.arguments.is_empty()
173-
} else {
174-
false
175-
}
170+
fn is_super_call_with_arguments(call: &ast::ExprCall, checker: &Checker) -> bool {
171+
checker.semantic().match_builtin_expr(&call.func, "super") && !call.arguments.is_empty()
176172
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,22 @@ UP008.py:92:14: UP008 [*] Use `super()` instead of `super(__class__, self)`
142142
91 91 | def bar(self):
143143
92 |- super(__class__, self).foo()
144144
92 |+ super().foo()
145+
93 93 |
146+
94 94 |
147+
95 95 | # see: https://github.com/astral-sh/ruff/issues/18684
148+
149+
UP008.py:107:23: UP008 [*] Use `super()` instead of `super(__class__, self)`
150+
|
151+
105 | class C:
152+
106 | def f(self):
153+
107 | builtins.super(C, self)
154+
| ^^^^^^^^^ UP008
155+
|
156+
= help: Remove `__super__` parameters
157+
158+
Unsafe fix
159+
104 104 |
160+
105 105 | class C:
161+
106 106 | def f(self):
162+
107 |- builtins.super(C, self)
163+
107 |+ builtins.super()

0 commit comments

Comments
 (0)