Enable printf-string-formatting fix with comments on right-hand side (#9037)

## Summary

This was added in https://github.com/astral-sh/ruff/pull/6364 (as a
follow-on to https://github.com/astral-sh/ruff/pull/6342), but I don't
think it applies in the same way, because we don't _remove_ the
right-hand side when converting from `%`-style formatting to `.format`
calls.

Closes https://github.com/astral-sh/ruff/issues/8107.
This commit is contained in:
Charlie Marsh 2023-12-06 22:43:21 -05:00 committed by GitHub
parent 06c9f625b6
commit acab5f3cf2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 13 deletions

View file

@ -110,3 +110,10 @@ print('Hello %(arg)s' % bar['bop'])
"%s" % (
x, # comment
)
path = "%s-%s-%s.pem" % (
safe_domain_name(cn), # common name, which should be filename safe because it is IDNA-encoded, but in case of a malformed cert make sure it's ok to use as a filename
cert.not_valid_after.date().isoformat().replace("-", ""), # expiration date
hexlify(cert.fingerprint(hashes.SHA256())).decode("ascii")[0:8], # fingerprint prefix
)

View file

@ -490,18 +490,10 @@ pub(crate) fn printf_string_formatting(checker: &mut Checker, expr: &Expr, right
contents.push_str(&format!(".format{params_string}"));
let mut diagnostic = Diagnostic::new(PrintfStringFormatting, expr.range());
// Avoid fix if there are comments within the right-hand side:
// ```
// "%s" % (
// 0, # 0
// )
// ```
if !checker.indexer().comment_ranges().intersects(right.range()) {
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
contents,
expr.range(),
)));
}
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
contents,
expr.range(),
)));
checker.diagnostics.push(diagnostic);
}

View file

@ -896,7 +896,7 @@ UP031_0.py:104:5: UP031 [*] Use format specifiers instead of percent format
109 108 |
110 109 | "%s" % (
UP031_0.py:110:1: UP031 Use format specifiers instead of percent format
UP031_0.py:110:1: UP031 [*] Use format specifiers instead of percent format
|
108 | )
109 |
@ -907,4 +907,36 @@ UP031_0.py:110:1: UP031 Use format specifiers instead of percent format
|
= help: Replace with format specifiers
Unsafe fix
107 107 | % (x,)
108 108 | )
109 109 |
110 |-"%s" % (
110 |+"{}".format(
111 111 | x, # comment
112 112 | )
113 113 |
UP031_0.py:115:8: UP031 [*] Use format specifiers instead of percent format
|
115 | path = "%s-%s-%s.pem" % (
| ________^
116 | | safe_domain_name(cn), # common name, which should be filename safe because it is IDNA-encoded, but in case of a malformed cert make sure it's ok to use as a filename
117 | | cert.not_valid_after.date().isoformat().replace("-", ""), # expiration date
118 | | hexlify(cert.fingerprint(hashes.SHA256())).decode("ascii")[0:8], # fingerprint prefix
119 | | )
| |_^ UP031
|
= help: Replace with format specifiers
Unsafe fix
112 112 | )
113 113 |
114 114 |
115 |-path = "%s-%s-%s.pem" % (
115 |+path = "{}-{}-{}.pem".format(
116 116 | safe_domain_name(cn), # common name, which should be filename safe because it is IDNA-encoded, but in case of a malformed cert make sure it's ok to use as a filename
117 117 | cert.not_valid_after.date().isoformat().replace("-", ""), # expiration date
118 118 | hexlify(cert.fingerprint(hashes.SHA256())).decode("ascii")[0:8], # fingerprint prefix