[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? -->
This commit is contained in:
Victor Hugo Gomes
2025-06-16 16:09:31 -03:00
committed by GitHub
parent 7880a20794
commit cff5adf324
3 changed files with 38 additions and 8 deletions

View File

@@ -89,4 +89,19 @@ class A:
class B(A):
def bar(self):
super(__class__, self).foo()
super(__class__, self).foo()
# see: https://github.com/astral-sh/ruff/issues/18684
class C:
def f(self):
super = print
super(C, self)
import builtins
class C:
def f(self):
builtins.super(C, self)

View File

@@ -66,7 +66,7 @@ impl AlwaysFixableViolation for SuperCallWithParameters {
pub(crate) fn super_call_with_parameters(checker: &Checker, call: &ast::ExprCall) {
// Only bother going through the super check at all if we're in a `super` call.
// (We check this in `super_args` too, so this is just an optimization.)
if !is_super_call_with_arguments(call) {
if !is_super_call_with_arguments(call, checker) {
return;
}
let scope = checker.semantic().current_scope();
@@ -167,10 +167,6 @@ pub(crate) fn super_call_with_parameters(checker: &Checker, call: &ast::ExprCall
}
/// Returns `true` if a call is an argumented `super` invocation.
fn is_super_call_with_arguments(call: &ast::ExprCall) -> bool {
if let Expr::Name(ast::ExprName { id, .. }) = call.func.as_ref() {
id == "super" && !call.arguments.is_empty()
} else {
false
}
fn is_super_call_with_arguments(call: &ast::ExprCall, checker: &Checker) -> bool {
checker.semantic().match_builtin_expr(&call.func, "super") && !call.arguments.is_empty()
}

View File

@@ -142,3 +142,22 @@ UP008.py:92:14: UP008 [*] Use `super()` instead of `super(__class__, self)`
91 91 | def bar(self):
92 |- super(__class__, self).foo()
92 |+ super().foo()
93 93 |
94 94 |
95 95 | # see: https://github.com/astral-sh/ruff/issues/18684
UP008.py:107:23: UP008 [*] Use `super()` instead of `super(__class__, self)`
|
105 | class C:
106 | def f(self):
107 | builtins.super(C, self)
| ^^^^^^^^^ UP008
|
= help: Remove `__super__` parameters
Unsafe fix
104 104 |
105 105 | class C:
106 106 | def f(self):
107 |- builtins.super(C, self)
107 |+ builtins.super()