Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pyupgrade/UP008.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,23 @@ def f(self):
class C:
def f(self):
builtins.super(C, self)


# see: https://github.com/astral-sh/ruff/issues/18533
class ClassForCommentEnthusiasts(BaseClass):
def with_comments(self):
super(
# super helpful comment
ClassForCommentEnthusiasts,
self
).f()
super(
ClassForCommentEnthusiasts,
# even more helpful comment
self
).f()
super(
ClassForCommentEnthusiasts,
self
# also a comment
).f()
7 changes: 7 additions & 0 deletions crates/ruff_linter/src/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,10 @@ pub(crate) const fn is_invalid_async_mock_access_check_enabled(settings: &Linter
pub(crate) const fn is_raise_exception_byte_string_enabled(settings: &LinterSettings) -> bool {
settings.preview.is_enabled()
}

// https://github.com/astral-sh/ruff/pull/18683
pub(crate) const fn is_safe_super_call_with_parameters_fix_enabled(
settings: &LinterSettings,
) -> bool {
settings.preview.is_enabled()
}
16 changes: 15 additions & 1 deletion crates/ruff_linter/src/rules/pyupgrade/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod tests {

use crate::registry::Rule;
use crate::rules::pyupgrade;

use crate::settings::types::PreviewMode;
use crate::test::test_path;
use crate::{assert_diagnostics, settings};

Expand Down Expand Up @@ -122,6 +122,20 @@ mod tests {
Ok(())
}

#[test_case(Rule::SuperCallWithParameters, Path::new("UP008.py"))]
fn rules_preview(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}__preview", path.to_string_lossy());
let diagnostics = test_path(
Path::new("pyupgrade").join(path).as_path(),
&settings::LinterSettings {
preview: PreviewMode::Enabled,
..settings::LinterSettings::for_rule(rule_code)
},
)?;
assert_diagnostics!(snapshot, diagnostics);
Ok(())
}

#[test]
fn async_timeout_error_alias_not_applied_py310() -> Result<()> {
let diagnostics = test_path(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use ruff_diagnostics::Applicability;
use ruff_macros::{ViolationMetadata, derive_message_formats};
use ruff_python_ast::{self as ast, Expr, Stmt};
use ruff_text_size::{Ranged, TextSize};

use crate::checkers::ast::Checker;
use crate::preview::is_safe_super_call_with_parameters_fix_enabled;
use crate::{AlwaysFixableViolation, Edit, Fix};

/// ## What it does
Expand Down Expand Up @@ -45,6 +47,10 @@ use crate::{AlwaysFixableViolation, Edit, Fix};
/// This rule's fix is marked as unsafe because removing the arguments from a call
/// may delete comments that are attached to the arguments.
///
/// In [preview], the fix is marked safe if no comments are present.
///
/// [preview]: https://docs.astral.sh/ruff/preview/
///
/// ## References
/// - [Python documentation: `super`](https://docs.python.org/3/library/functions.html#super)
/// - [super/MRO, Python's most misunderstood feature.](https://www.youtube.com/watch?v=X1PQ7zzltz4)
Expand Down Expand Up @@ -159,11 +165,22 @@ pub(crate) fn super_call_with_parameters(checker: &Checker, call: &ast::ExprCall
return;
}

let applicability = if !checker.comment_ranges().intersects(call.arguments.range())
&& is_safe_super_call_with_parameters_fix_enabled(checker.settings())
{
Applicability::Safe
} else {
Applicability::Unsafe
};

let mut diagnostic = checker.report_diagnostic(SuperCallWithParameters, call.arguments.range());
diagnostic.set_fix(Fix::unsafe_edit(Edit::deletion(
call.arguments.start() + TextSize::new(1),
call.arguments.end() - TextSize::new(1),
)));
diagnostic.set_fix(Fix::applicable_edit(
Edit::deletion(
call.arguments.start() + TextSize::new(1),
call.arguments.end() - TextSize::new(1),
),
applicability,
));
}

/// Returns `true` if a call is an argumented `super` invocation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,91 @@ UP008.py:107:23: UP008 [*] Use `super()` instead of `super(__class__, self)`
106 106 | def f(self):
107 |- builtins.super(C, self)
107 |+ builtins.super()
108 108 |
109 109 |
110 110 | # see: https://github.com/astral-sh/ruff/issues/18533

UP008.py:113:14: UP008 [*] Use `super()` instead of `super(__class__, self)`
|
111 | class ClassForCommentEnthusiasts(BaseClass):
112 | def with_comments(self):
113 | super(
| ______________^
114 | | # super helpful comment
115 | | ClassForCommentEnthusiasts,
116 | | self
117 | | ).f()
| |_________^ UP008
118 | super(
119 | ClassForCommentEnthusiasts,
|
= help: Remove `super()` parameters

ℹ Unsafe fix
110 110 | # see: https://github.com/astral-sh/ruff/issues/18533
111 111 | class ClassForCommentEnthusiasts(BaseClass):
112 112 | def with_comments(self):
113 |- super(
114 |- # super helpful comment
115 |- ClassForCommentEnthusiasts,
116 |- self
117 |- ).f()
113 |+ super().f()
118 114 | super(
119 115 | ClassForCommentEnthusiasts,
120 116 | # even more helpful comment

UP008.py:118:14: UP008 [*] Use `super()` instead of `super(__class__, self)`
|
116 | self
117 | ).f()
118 | super(
| ______________^
119 | | ClassForCommentEnthusiasts,
120 | | # even more helpful comment
121 | | self
122 | | ).f()
| |_________^ UP008
123 | super(
124 | ClassForCommentEnthusiasts,
|
= help: Remove `super()` parameters

ℹ Unsafe fix
115 115 | ClassForCommentEnthusiasts,
116 116 | self
117 117 | ).f()
118 |- super(
119 |- ClassForCommentEnthusiasts,
120 |- # even more helpful comment
121 |- self
122 |- ).f()
118 |+ super().f()
123 119 | super(
124 120 | ClassForCommentEnthusiasts,
125 121 | self

UP008.py:123:14: UP008 [*] Use `super()` instead of `super(__class__, self)`
|
121 | self
122 | ).f()
123 | super(
| ______________^
124 | | ClassForCommentEnthusiasts,
125 | | self
126 | | # also a comment
127 | | ).f()
| |_________^ UP008
|
= help: Remove `super()` parameters

ℹ Unsafe fix
120 120 | # even more helpful comment
121 121 | self
122 122 | ).f()
123 |- super(
124 |- ClassForCommentEnthusiasts,
125 |- self
126 |- # also a comment
127 |- ).f()
123 |+ super().f()
Loading
Loading