Skip to content
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

Extend UP032 to support repeated format fields #6266

Merged
merged 4 commits into from
Aug 2, 2023
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
6 changes: 4 additions & 2 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP032_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@

"{1} {0}".format(a, b)

"{0} {1} {0}".format(a, b)

"{x.y}".format(x=z)

"{x} {y} {x}".format(x=a, y=b)

"{.x} {.y}".format(a, b)

"{} {}".format(a.b, c.d)
Expand Down Expand Up @@ -85,8 +89,6 @@

"{} {}".format(*a)

"{0} {0}".format(arg)

"{x} {x}".format(arg)

"{x.y} {x.z}".format(arg)
Expand Down
33 changes: 16 additions & 17 deletions crates/ruff/src/rules/pyupgrade/rules/f_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl AlwaysAutofixableViolation for FString {
struct FormatSummaryValues<'a> {
args: Vec<&'a Expr>,
kwargs: FxHashMap<&'a str, &'a Expr>,
auto_index: usize,
}

impl<'a> FormatSummaryValues<'a> {
Expand Down Expand Up @@ -98,27 +99,25 @@ impl<'a> FormatSummaryValues<'a> {
Some(Self {
args: extracted_args,
kwargs: extracted_kwargs,
auto_index: 0,
})
}

fn consume_next(&mut self) -> Option<&Expr> {
if self.args.is_empty() {
None
} else {
Some(self.args.remove(0))
}
/// Return the next positional argument.
fn arg_auto(&mut self) -> Option<&Expr> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i know they have been missing previously, but could you please add some docstrings to these methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

let idx = self.auto_index;
self.auto_index += 1;
self.arg_positional(idx)
}

fn consume_arg(&mut self, index: usize) -> Option<&Expr> {
if self.args.len() > index {
Some(self.args.remove(index))
} else {
None
}
/// Return the positional argument at the given index.
fn arg_positional(&self, index: usize) -> Option<&Expr> {
self.args.get(index).copied()
}

fn consume_kwarg(&mut self, key: &str) -> Option<&Expr> {
self.kwargs.remove(key)
/// Return the keyword argument with the given name.
fn arg_keyword(&self, key: &str) -> Option<&Expr> {
self.kwargs.get(key).copied()
}
}

Expand Down Expand Up @@ -250,9 +249,9 @@ fn try_convert_to_f_string(expr: &Expr, locator: &Locator) -> Option<String> {

let field = FieldName::parse(&field_name).ok()?;
let arg = match field.field_type {
FieldType::Auto => summary.consume_next(),
FieldType::Index(index) => summary.consume_arg(index),
FieldType::Keyword(name) => summary.consume_kwarg(&name),
FieldType::Auto => summary.arg_auto(),
FieldType::Index(index) => summary.arg_positional(index),
FieldType::Keyword(name) => summary.arg_keyword(&name),
}?;
converted.push_str(&formatted_expr(
arg,
Expand Down
Loading