Skip to content

Commit 91af5a4

Browse files
[pyupgrade] Allow fixes for f-string rule regardless of line length (UP032) (#10263)
## Summary This is a follow-up to #10238 to offer fixes for the f-string rule regardless of the line length of the resulting fix. To quote Alex in the linked PR: > Yes, from the user's perspective I'd rather have a fix that may lead to line length issues than have to fix them myself :-) Cleaning up line lengths is easier than changing from `"".format()` to `f""` I agree with this position, which is that if we're going to offer a diagnostic, we should really be offering the user the ability to fix it -- otherwise, we're just inconveniencing them.
1 parent 461cdad commit 91af5a4

File tree

4 files changed

+53
-44
lines changed

4 files changed

+53
-44
lines changed

crates/ruff_linter/src/checkers/ast/analyze/expression.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
427427
pyupgrade::rules::format_literals(checker, call, &summary);
428428
}
429429
if checker.enabled(Rule::FString) {
430-
pyupgrade::rules::f_strings(checker, call, &summary, value);
430+
pyupgrade::rules::f_strings(checker, call, &summary);
431431
}
432432
}
433433
}

crates/ruff_linter/src/fix/edits.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -418,29 +418,6 @@ pub(crate) fn fits(
418418
all_lines_fit(fix, node, locator, line_length.value() as usize, tab_size)
419419
}
420420

421-
/// Returns `true` if the fix fits within the maximum configured line length, or produces lines that
422-
/// are shorter than the maximum length of the existing AST node.
423-
pub(crate) fn fits_or_shrinks(
424-
fix: &str,
425-
node: AnyNodeRef,
426-
locator: &Locator,
427-
line_length: LineLength,
428-
tab_size: IndentWidth,
429-
) -> bool {
430-
// Use the larger of the line length limit, or the longest line in the existing AST node.
431-
let line_length = std::iter::once(line_length.value() as usize)
432-
.chain(
433-
locator
434-
.slice(locator.lines_range(node.range()))
435-
.universal_newlines()
436-
.map(|line| LineWidthBuilder::new(tab_size).add_str(&line).get()),
437-
)
438-
.max()
439-
.unwrap_or(line_length.value() as usize);
440-
441-
all_lines_fit(fix, node, locator, line_length, tab_size)
442-
}
443-
444421
/// Returns `true` if all lines in the fix are shorter than the given line length.
445422
fn all_lines_fit(
446423
fix: &str,

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

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use ruff_source_file::Locator;
1616
use ruff_text_size::{Ranged, TextRange};
1717

1818
use crate::checkers::ast::Checker;
19-
use crate::fix::edits::fits_or_shrinks;
2019

2120
use crate::rules::pyflakes::format::FormatSummary;
2221
use crate::rules::pyupgrade::helpers::{curly_escape, curly_unescape};
@@ -392,12 +391,7 @@ impl FStringConversion {
392391
}
393392

394393
/// UP032
395-
pub(crate) fn f_strings(
396-
checker: &mut Checker,
397-
call: &ast::ExprCall,
398-
summary: &FormatSummary,
399-
template: &Expr,
400-
) {
394+
pub(crate) fn f_strings(checker: &mut Checker, call: &ast::ExprCall, summary: &FormatSummary) {
401395
if summary.has_nested_parts {
402396
return;
403397
}
@@ -520,15 +514,6 @@ pub(crate) fn f_strings(
520514

521515
let mut diagnostic = Diagnostic::new(FString, call.range());
522516

523-
// Avoid refactors that exceed the line length limit or make it exceed by more.
524-
let f_string_fits_or_shrinks = fits_or_shrinks(
525-
&contents,
526-
template.into(),
527-
checker.locator(),
528-
checker.settings.pycodestyle.max_line_length,
529-
checker.settings.tab_size,
530-
);
531-
532517
// Avoid fix if there are comments within the call:
533518
// ```
534519
// "{}".format(
@@ -540,7 +525,7 @@ pub(crate) fn f_strings(
540525
.comment_ranges()
541526
.intersects(call.arguments.range());
542527

543-
if f_string_fits_or_shrinks && !has_comments {
528+
if !has_comments {
544529
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
545530
contents,
546531
call.range(),

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

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ UP032_0.py:129:1: UP032 [*] Use f-string instead of `format` call
945945
131 131 | ###
946946
132 132 | # Non-errors
947947

948-
UP032_0.py:160:1: UP032 Use f-string instead of `format` call
948+
UP032_0.py:160:1: UP032 [*] Use f-string instead of `format` call
949949
|
950950
158 | r'"\N{snowman} {}".format(a)'
951951
159 |
@@ -958,7 +958,19 @@ UP032_0.py:160:1: UP032 Use f-string instead of `format` call
958958
|
959959
= help: Convert to f-string
960960

961-
UP032_0.py:164:1: UP032 Use f-string instead of `format` call
961+
Safe fix
962+
157 157 |
963+
158 158 | r'"\N{snowman} {}".format(a)'
964+
159 159 |
965+
160 |-"123456789 {}".format(
966+
161 |- 11111111111111111111111111111111111111111111111111111111111111111111111111,
967+
162 |-)
968+
160 |+f"123456789 {11111111111111111111111111111111111111111111111111111111111111111111111111}"
969+
163 161 |
970+
164 162 | """
971+
165 163 | {}
972+
973+
UP032_0.py:164:1: UP032 [*] Use f-string instead of `format` call
962974
|
963975
162 | )
964976
163 |
@@ -977,7 +989,28 @@ UP032_0.py:164:1: UP032 Use f-string instead of `format` call
977989
|
978990
= help: Convert to f-string
979991

980-
UP032_0.py:174:84: UP032 Use f-string instead of `format` call
992+
Safe fix
993+
161 161 | 11111111111111111111111111111111111111111111111111111111111111111111111111,
994+
162 162 | )
995+
163 163 |
996+
164 |+f"""
997+
165 |+{1}
998+
166 |+{2}
999+
167 |+{111111111111111111111111111111111111111111111111111111111111111111111111111111111111111}
1000+
164 168 | """
1001+
165 |-{}
1002+
166 |-{}
1003+
167 |-{}
1004+
168 |-""".format(
1005+
169 |-1,
1006+
170 |-2,
1007+
171 |-111111111111111111111111111111111111111111111111111111111111111111111111111111111111111,
1008+
172 |-)
1009+
173 169 |
1010+
174 170 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = """{}
1011+
175 171 | """.format(
1012+
1013+
UP032_0.py:174:84: UP032 [*] Use f-string instead of `format` call
9811014
|
9821015
172 | )
9831016
173 |
@@ -992,6 +1025,20 @@ UP032_0.py:174:84: UP032 Use f-string instead of `format` call
9921025
|
9931026
= help: Convert to f-string
9941027

1028+
Safe fix
1029+
171 171 | 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111,
1030+
172 172 | )
1031+
173 173 |
1032+
174 |-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = """{}
1033+
175 |-""".format(
1034+
176 |- 111111
1035+
177 |-)
1036+
174 |+aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = f"""{111111}
1037+
175 |+"""
1038+
178 176 |
1039+
179 177 | "{}".format(
1040+
180 178 | [
1041+
9951042
UP032_0.py:202:1: UP032 Use f-string instead of `format` call
9961043
|
9971044
200 | "{}".format(**c)

0 commit comments

Comments
 (0)