-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Autofix for UP032 swallows comment #6336
Comments
@charliermarsh Can I work on fixing this? |
Sure. How are you thinking of resolving? What's the expected behavior here? |
diff --git a/crates/ruff/src/rules/pyupgrade/rules/f_strings.rs b/crates/ruff/src/rules/pyupgrade/rules/f_strings.rs
index 1739207cb..2dbf10061 100644
--- a/crates/ruff/src/rules/pyupgrade/rules/f_strings.rs
+++ b/crates/ruff/src/rules/pyupgrade/rules/f_strings.rs
@@ -320,7 +320,7 @@ pub(crate) fn f_strings(
return;
};
- let Expr::Attribute(ast::ExprAttribute { value, .. }) = func.as_ref() else {
+ let Expr::Attribute(ast::ExprAttribute { value, attr, .. }) = func.as_ref() else {
return;
};
@@ -334,6 +334,25 @@ pub(crate) fn f_strings(
return;
};
+ // Exit if comments are present in the argument range:
+ // ```
+ // "{} {}".format(
+ // 1, # 1
+ // 2, # 2
+ // )
+ // ```
+ if lexer::lex(
+ checker
+ .locator()
+ .slice(TextRange::new(attr.start(), expr.end())),
+ Mode::Expression,
+ )
+ .flatten()
+ .any(|(tok, _)| matches!(tok, Tok::Comment(..)))
+ {
+ return;
+ }
+
let Some(mut summary) = FormatSummaryValues::try_from_expr(expr, checker.locator()) else {
return;
}; I think preserving comments is not easy. Can we avoid raising UP032 if comments are present in the argument range like this^? |
That's definitely one approach. Another possible approach is to raise the warning but skip it when auto-fixing. I'll let you decide which one is best :) |
Yeah I'd say raise-but-not-fix if there are comments within the call. |
+1 to raise-but-not-fix. |
That was quick! Thanks
|
Given this snippet:
Ruff autofix produces this output:
Note that the autofixed code "swallows" the comment. See here for original context: WhyNotHugo/django-afip@2974184
This is reproducible with ruff 0.0.281. The relevant pyproject.toml is here.
The text was updated successfully, but these errors were encountered: