-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
RUF010
to detect explicit type conversions within f-strin…
…gs (#4387)
- Loading branch information
Showing
9 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
bla = b"bla" | ||
|
||
|
||
def foo(one_arg): | ||
pass | ||
|
||
|
||
f"{str(bla)}, {repr(bla)}, {ascii(bla)}" # RUF010 | ||
|
||
f"{foo(bla)}" # OK | ||
|
||
f"{str(bla, 'ascii')}, {str(bla, encoding='cp1255')}" # OK | ||
|
||
f"{bla!s} {[]!r} {'bar'!a}" # OK | ||
|
||
"Not an f-string {str(bla)}, {repr(bla)}, {ascii(bla)}" # OK | ||
|
||
|
||
def ascii(arg): | ||
pass | ||
|
||
|
||
f"{ascii(bla)}" # OK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
crates/ruff/src/rules/ruff/rules/explicit_f_string_type_conversion.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
use ruff_text_size::TextSize; | ||
use rustpython_parser::ast::{self, Expr, ExprKind}; | ||
|
||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::helpers::unparse_expr; | ||
|
||
use crate::checkers::ast::Checker; | ||
use crate::registry::AsRule; | ||
|
||
/// ## What it does | ||
/// Checks for usages of `str()`, `repr()`, and `ascii()` as explicit type | ||
/// conversions within f-strings. | ||
/// | ||
/// ## Why is this bad? | ||
/// f-strings support dedicated conversion flags for these types, which are | ||
/// more succinct and idiomatic. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// a = "some string" | ||
/// f"{repr(a)}" | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// a = "some string" | ||
/// f"{a!r}" | ||
/// ``` | ||
#[violation] | ||
pub struct ExplicitFStringTypeConversion; | ||
|
||
impl AlwaysAutofixableViolation for ExplicitFStringTypeConversion { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Use conversion in f-string") | ||
} | ||
|
||
fn autofix_title(&self) -> String { | ||
"Replace f-string function call with conversion".to_string() | ||
} | ||
} | ||
|
||
/// RUF010 | ||
pub(crate) fn explicit_f_string_type_conversion( | ||
checker: &mut Checker, | ||
expr: &Expr, | ||
formatted_value: &Expr, | ||
conversion: ast::Int, | ||
) { | ||
// Skip if there's already a conversion flag. | ||
if conversion != ast::ConversionFlag::None as u32 { | ||
return; | ||
} | ||
|
||
let ExprKind::Call(ast::ExprCall { | ||
func, | ||
args, | ||
keywords, | ||
}) = &formatted_value.node else { | ||
return; | ||
}; | ||
|
||
// Can't be a conversion otherwise. | ||
if args.len() != 1 || !keywords.is_empty() { | ||
return; | ||
} | ||
|
||
let ExprKind::Name(ast::ExprName { id, .. }) = &func.node else { | ||
return; | ||
}; | ||
|
||
if !matches!(id.as_str(), "str" | "repr" | "ascii") { | ||
return; | ||
}; | ||
|
||
if !checker.ctx.is_builtin(id) { | ||
return; | ||
} | ||
|
||
let mut diagnostic = Diagnostic::new(ExplicitFStringTypeConversion, formatted_value.range()); | ||
|
||
if checker.patch(diagnostic.kind.rule()) { | ||
// Replace the call node with its argument and a conversion flag. | ||
let mut conv_expr = expr.clone(); | ||
let ExprKind::FormattedValue(ast::ExprFormattedValue { | ||
ref mut conversion, | ||
ref mut value, | ||
.. | ||
}) = conv_expr.node else { | ||
return; | ||
}; | ||
|
||
*conversion = match id.as_str() { | ||
"ascii" => ast::Int::new(ast::ConversionFlag::Ascii as u32), | ||
"str" => ast::Int::new(ast::ConversionFlag::Str as u32), | ||
"repr" => ast::Int::new(ast::ConversionFlag::Repr as u32), | ||
&_ => unreachable!(), | ||
}; | ||
|
||
value.node = args[0].node.clone(); | ||
|
||
diagnostic.set_fix(Fix::automatic(Edit::range_replacement( | ||
unparse_expr(&conv_expr, checker.stylist), | ||
formatted_value | ||
.range() | ||
.sub_start(TextSize::from(1)) | ||
.add_end(TextSize::from(1)), | ||
))); | ||
} | ||
|
||
checker.diagnostics.push(diagnostic); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
crates/ruff/src/rules/ruff/snapshots/ruff__rules__ruff__tests__RUF010_RUF010.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
source: crates/ruff/src/rules/ruff/mod.rs | ||
--- | ||
RUF010.py:8:4: RUF010 [*] Use conversion in f-string | ||
| | ||
8 | f"{str(bla)}, {repr(bla)}, {ascii(bla)}" # RUF010 | ||
| ^^^^^^^^ RUF010 | ||
9 | | ||
10 | f"{foo(bla)}" # OK | ||
| | ||
= help: Replace f-string function call with conversion | ||
|
||
ℹ Fix | ||
5 5 | pass | ||
6 6 | | ||
7 7 | | ||
8 |-f"{str(bla)}, {repr(bla)}, {ascii(bla)}" # RUF010 | ||
8 |+f"{bla!s}, {repr(bla)}, {ascii(bla)}" # RUF010 | ||
9 9 | | ||
10 10 | f"{foo(bla)}" # OK | ||
11 11 | | ||
|
||
RUF010.py:8:16: RUF010 [*] Use conversion in f-string | ||
| | ||
8 | f"{str(bla)}, {repr(bla)}, {ascii(bla)}" # RUF010 | ||
| ^^^^^^^^^ RUF010 | ||
9 | | ||
10 | f"{foo(bla)}" # OK | ||
| | ||
= help: Replace f-string function call with conversion | ||
|
||
ℹ Fix | ||
5 5 | pass | ||
6 6 | | ||
7 7 | | ||
8 |-f"{str(bla)}, {repr(bla)}, {ascii(bla)}" # RUF010 | ||
8 |+f"{str(bla)}, {bla!r}, {ascii(bla)}" # RUF010 | ||
9 9 | | ||
10 10 | f"{foo(bla)}" # OK | ||
11 11 | | ||
|
||
RUF010.py:8:29: RUF010 [*] Use conversion in f-string | ||
| | ||
8 | f"{str(bla)}, {repr(bla)}, {ascii(bla)}" # RUF010 | ||
| ^^^^^^^^^^ RUF010 | ||
9 | | ||
10 | f"{foo(bla)}" # OK | ||
| | ||
= help: Replace f-string function call with conversion | ||
|
||
ℹ Fix | ||
5 5 | pass | ||
6 6 | | ||
7 7 | | ||
8 |-f"{str(bla)}, {repr(bla)}, {ascii(bla)}" # RUF010 | ||
8 |+f"{str(bla)}, {repr(bla)}, {bla!a}" # RUF010 | ||
9 9 | | ||
10 10 | f"{foo(bla)}" # OK | ||
11 11 | | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.