Skip to content

Commit

Permalink
Remove single-value case
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Mar 21, 2023
1 parent 2f1df2f commit caec651
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 84 deletions.
3 changes: 0 additions & 3 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP031_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@
# Single-value expressions
print('Hello %s' % "World")
print('Hello %s' % f"World")
print('Hello %s' % bar)
print('Hello %s' % bar.baz)
print('Hello %s' % bar['bop'])
print('Hello %s (%s)' % bar)
print('Hello %s (%s)' % bar.baz)
print('Hello %s (%s)' % bar['bop'])
Expand Down
6 changes: 6 additions & 0 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP031_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,9 @@
"""
% (x,)
)

'Hello %s' % bar

'Hello %s' % bar.baz

'Hello %s' % bar['bop']
25 changes: 16 additions & 9 deletions crates/ruff/src/rules/pyupgrade/rules/printf_string_formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ pub(crate) fn printf_string_formatting(
}

// Parse each string segment.
let mut num_positional = 0;
let mut num_keyword = 0;
let mut num_positional_arguments = 0;
let mut num_keyword_arguments = 0;
let mut format_strings = Vec::with_capacity(strings.len());
for (start, end) in &strings {
let string = checker.locator.slice(Range::new(*start, *end));
Expand All @@ -359,9 +359,9 @@ pub(crate) fn printf_string_formatting(
continue;
};
if fmt.mapping_key.is_none() {
num_positional += 1;
num_positional_arguments += 1;
} else {
num_keyword += 1;
num_keyword_arguments += 1;
}
}

Expand All @@ -379,17 +379,24 @@ pub(crate) fn printf_string_formatting(
| ExprKind::Attribute { .. }
| ExprKind::Subscript { .. }
| ExprKind::Call { .. } => {
if num_keyword > 0 {
if num_keyword_arguments > 0 {
// If we have _any_ named fields, assume the right-hand side is a mapping.
format!("(**{})", checker.locator.slice(right))
} else if num_positional > 1 {
} else if num_positional_arguments > 1 {
// If we have multiple fields, but no named fields, assume the right-hand side is a
// tuple.
format!("(*{})", checker.locator.slice(right))
} else {
// Otherwise, if we have a single field, assume the right-hand side is a single
// value.
format!("({})", checker.locator.slice(right))
// Otherwise, if we have a single field, we can't make any assumptions about the
// right-hand side. It _could_ be a tuple, but it could also be a single value,
// and we can't differentiate between them.
// For example:
// ```python
// x = (1,)
// print("%s" % x)
// print("{}".format(x))
// ```
return;
}
}
ExprKind::Tuple { .. } => clean_params_tuple(checker, right),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -632,15 +632,15 @@ expression: diagnostics
column: 6
end_location:
row: 80
column: 22
column: 27
fix:
content: "'Hello {}'.format(bar)"
content: "'Hello {} ({})'.format(*bar)"
location:
row: 80
column: 6
end_location:
row: 80
column: 22
column: 27
parent: ~
- kind:
name: PrintfStringFormatting
Expand All @@ -652,15 +652,15 @@ expression: diagnostics
column: 6
end_location:
row: 81
column: 26
column: 31
fix:
content: "'Hello {}'.format(bar.baz)"
content: "'Hello {} ({})'.format(*bar.baz)"
location:
row: 81
column: 6
end_location:
row: 81
column: 26
column: 31
parent: ~
- kind:
name: PrintfStringFormatting
Expand All @@ -672,15 +672,15 @@ expression: diagnostics
column: 6
end_location:
row: 82
column: 29
column: 34
fix:
content: "'Hello {}'.format(bar['bop'])"
content: "'Hello {} ({})'.format(*bar['bop'])"
location:
row: 82
column: 6
end_location:
row: 82
column: 29
column: 34
parent: ~
- kind:
name: PrintfStringFormatting
Expand All @@ -694,7 +694,7 @@ expression: diagnostics
row: 83
column: 27
fix:
content: "'Hello {} ({})'.format(*bar)"
content: "'Hello {arg}'.format(**bar)"
location:
row: 83
column: 6
Expand All @@ -714,7 +714,7 @@ expression: diagnostics
row: 84
column: 31
fix:
content: "'Hello {} ({})'.format(*bar.baz)"
content: "'Hello {arg}'.format(**bar.baz)"
location:
row: 84
column: 6
Expand All @@ -734,72 +734,12 @@ expression: diagnostics
row: 85
column: 34
fix:
content: "'Hello {} ({})'.format(*bar['bop'])"
content: "'Hello {arg}'.format(**bar['bop'])"
location:
row: 85
column: 6
end_location:
row: 85
column: 34
parent: ~
- kind:
name: PrintfStringFormatting
body: Use format specifiers instead of percent format
suggestion: Replace with format specifiers
fixable: true
location:
row: 86
column: 6
end_location:
row: 86
column: 27
fix:
content: "'Hello {arg}'.format(**bar)"
location:
row: 86
column: 6
end_location:
row: 86
column: 27
parent: ~
- kind:
name: PrintfStringFormatting
body: Use format specifiers instead of percent format
suggestion: Replace with format specifiers
fixable: true
location:
row: 87
column: 6
end_location:
row: 87
column: 31
fix:
content: "'Hello {arg}'.format(**bar.baz)"
location:
row: 87
column: 6
end_location:
row: 87
column: 31
parent: ~
- kind:
name: PrintfStringFormatting
body: Use format specifiers instead of percent format
suggestion: Replace with format specifiers
fixable: true
location:
row: 88
column: 6
end_location:
row: 88
column: 34
fix:
content: "'Hello {arg}'.format(**bar['bop'])"
location:
row: 88
column: 6
end_location:
row: 88
column: 34
parent: ~

0 comments on commit caec651

Please sign in to comment.