Skip to content

Commit

Permalink
Use colon rather than dot formatting for integer-only types
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jul 26, 2024
1 parent 1fe4a5f commit 3561dad
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,11 @@
# Not a valid type annotation but this test shouldn't result in a panic.
# Refer: https://github.com/astral-sh/ruff/issues/11736
x: "'%s + %s' % (1, 2)"


# See: https://github.com/astral-sh/ruff/issues/12421
print("%.2X" % 1)
print("%.02X" % 1)
print("%02X" % 1)
print("%.00002X" % 1)
print("%.20X" % 1)
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ fn handle_part(part: &CFormatPart<String>) -> Cow<'_, str> {
CFormatPart::Spec(spec) => {
let mut format_string = String::new();

println!("{:?}", spec);

// TODO(charlie): What case is this?
if spec.format_char == '%' {
format_string.push('%');
Expand Down Expand Up @@ -150,8 +152,19 @@ fn handle_part(part: &CFormatPart<String>) -> Cow<'_, str> {
match precision {
CFormatPrecision::Quantity(quantity) => match quantity {
CFormatQuantity::Amount(amount) => {
format_string.push('.');
format_string.push_str(&amount.to_string());
// Integer-only presentation types.
//
// See: https://docs.python.org/3/library/string.html#format-specification-mini-language
if matches!(
spec.format_char,
'b' | 'c' | 'd' | 'o' | 'x' | 'X' | 'n'
) {
format_string.push('0');
format_string.push_str(&amount.to_string());
} else {
format_string.push('.');
format_string.push_str(&amount.to_string());
}
}
CFormatQuantity::FromValuesTuple => {
unreachable!("Width should be a usize")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1016,3 +1016,101 @@ UP031_0.py:131:5: UP031 [*] Use format specifiers instead of percent format
130 130 | # Refer: https://github.com/astral-sh/ruff/issues/11736
131 |-x: "'%s + %s' % (1, 2)"
131 |+x: "'{} + {}'.format(1, 2)"
132 132 |
133 133 |
134 134 | # See: https://github.com/astral-sh/ruff/issues/12421

UP031_0.py:135:7: UP031 [*] Use format specifiers instead of percent format
|
134 | # See: https://github.com/astral-sh/ruff/issues/12421
135 | print("%.2X" % 1)
| ^^^^^^^^^^ UP031
136 | print("%.02X" % 1)
137 | print("%02X" % 1)
|
= help: Replace with format specifiers

Unsafe fix
132 132 |
133 133 |
134 134 | # See: https://github.com/astral-sh/ruff/issues/12421
135 |-print("%.2X" % 1)
135 |+print("{:02X}".format(1))
136 136 | print("%.02X" % 1)
137 137 | print("%02X" % 1)
138 138 | print("%.00002X" % 1)

UP031_0.py:136:7: UP031 [*] Use format specifiers instead of percent format
|
134 | # See: https://github.com/astral-sh/ruff/issues/12421
135 | print("%.2X" % 1)
136 | print("%.02X" % 1)
| ^^^^^^^^^^^ UP031
137 | print("%02X" % 1)
138 | print("%.00002X" % 1)
|
= help: Replace with format specifiers

Unsafe fix
133 133 |
134 134 | # See: https://github.com/astral-sh/ruff/issues/12421
135 135 | print("%.2X" % 1)
136 |-print("%.02X" % 1)
136 |+print("{:02X}".format(1))
137 137 | print("%02X" % 1)
138 138 | print("%.00002X" % 1)
139 139 | print("%.20X" % 1)

UP031_0.py:137:7: UP031 [*] Use format specifiers instead of percent format
|
135 | print("%.2X" % 1)
136 | print("%.02X" % 1)
137 | print("%02X" % 1)
| ^^^^^^^^^^ UP031
138 | print("%.00002X" % 1)
139 | print("%.20X" % 1)
|
= help: Replace with format specifiers

Unsafe fix
134 134 | # See: https://github.com/astral-sh/ruff/issues/12421
135 135 | print("%.2X" % 1)
136 136 | print("%.02X" % 1)
137 |-print("%02X" % 1)
137 |+print("{:02X}".format(1))
138 138 | print("%.00002X" % 1)
139 139 | print("%.20X" % 1)

UP031_0.py:138:7: UP031 [*] Use format specifiers instead of percent format
|
136 | print("%.02X" % 1)
137 | print("%02X" % 1)
138 | print("%.00002X" % 1)
| ^^^^^^^^^^^^^^ UP031
139 | print("%.20X" % 1)
|
= help: Replace with format specifiers

Unsafe fix
135 135 | print("%.2X" % 1)
136 136 | print("%.02X" % 1)
137 137 | print("%02X" % 1)
138 |-print("%.00002X" % 1)
138 |+print("{:02X}".format(1))
139 139 | print("%.20X" % 1)

UP031_0.py:139:7: UP031 [*] Use format specifiers instead of percent format
|
137 | print("%02X" % 1)
138 | print("%.00002X" % 1)
139 | print("%.20X" % 1)
| ^^^^^^^^^^^ UP031
|
= help: Replace with format specifiers

Unsafe fix
136 136 | print("%.02X" % 1)
137 137 | print("%02X" % 1)
138 138 | print("%.00002X" % 1)
139 |-print("%.20X" % 1)
139 |+print("{:020X}".format(1))

0 comments on commit 3561dad

Please sign in to comment.