-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pyupgrade: Printf string formatting (#1803)
- Loading branch information
Showing
22 changed files
with
1,249 additions
and
36 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
a, b, x, y = 1, 2, 3, 4 | ||
|
||
# UP031 | ||
print('%s %s' % (a, b)) | ||
|
||
print('%s%s' % (a, b)) | ||
|
||
print("trivial" % ()) | ||
|
||
print("%s" % ("simple",)) | ||
|
||
print("%s" % ("%s" % ("nested",),)) | ||
|
||
print("%s%% percent" % (15,)) | ||
|
||
print("%f" % (15,)) | ||
|
||
print("%.f" % (15,)) | ||
|
||
print("%.3f" % (15,)) | ||
|
||
print("%3f" % (15,)) | ||
|
||
print("%-5f" % (5,)) | ||
|
||
print("%9f" % (5,)) | ||
|
||
print("%#o" % (123,)) | ||
|
||
print("brace {} %s" % (1,)) | ||
|
||
print( | ||
"%s" % ( | ||
"trailing comma", | ||
) | ||
) | ||
|
||
print("foo %s " % (x,)) | ||
|
||
print("%(k)s" % {"k": "v"}) | ||
|
||
print("%(k)s" % { | ||
"k": "v", | ||
"i": "j" | ||
}) | ||
|
||
print("%(to_list)s" % {"to_list": []}) | ||
|
||
print("%(k)s" % {"k": "v", "i": 1, "j": []}) | ||
|
||
print("%(ab)s" % {"a" "b": 1}) | ||
|
||
print("%(a)s" % {"a" : 1}) | ||
|
||
print(( | ||
"foo %s " | ||
"bar %s" % (x, y) | ||
)) | ||
|
||
print( | ||
"foo %(foo)s " | ||
"bar %(bar)s" % {"foo": x, "bar": y} | ||
) | ||
|
||
print("%s \N{snowman}" % (a,)) | ||
|
||
print("%(foo)s \N{snowman}" % {"foo": 1}) | ||
|
||
print(("foo %s " "bar %s") % (x, y)) |
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,59 @@ | ||
# OK | ||
"%s" % unknown_type | ||
|
||
b"%s" % (b"bytestring",) | ||
|
||
"%*s" % (5, "hi") | ||
|
||
"%d" % (flt,) | ||
|
||
"%c" % (some_string,) | ||
|
||
"%4%" % () | ||
|
||
"%.2r" % (1.25) | ||
|
||
i % 3 | ||
|
||
"%.*s" % (5, "hi") | ||
|
||
"%i" % (flt,) | ||
|
||
"%()s" % {"": "empty"} | ||
|
||
"%s" % {"k": "v"} | ||
|
||
"%(1)s" % {"1": "bar"} | ||
|
||
"%(a)s" % {"a": 1, "a": 2} | ||
|
||
pytest.param('"%8s" % (None,)', id="unsafe width-string conversion"), | ||
|
||
"%()s" % {"": "bar"} | ||
|
||
"%(1)s" % {1: 2, "1": 2} | ||
|
||
"%(and)s" % {"and": 2} | ||
|
||
# OK (arguably false negatives) | ||
( | ||
"foo %s " | ||
"bar %s" | ||
) % (x, y) | ||
|
||
( | ||
"foo %(foo)s " | ||
"bar %(bar)s" | ||
) % {"foo": x, "bar": y} | ||
|
||
( | ||
"""foo %s""" | ||
% (x,) | ||
) | ||
|
||
( | ||
""" | ||
foo %s | ||
""" | ||
% (x,) | ||
) |
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 |
---|---|---|
|
@@ -1780,6 +1780,7 @@ | |
"UP029", | ||
"UP03", | ||
"UP030", | ||
"UP031", | ||
"UP032", | ||
"UP033", | ||
"UP034", | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
use once_cell::sync::Lazy; | ||
use regex::Regex; | ||
/// Returns `true` if a string is a valid Python identifier (e.g., variable | ||
/// name). | ||
pub fn is_identifier(s: &str) -> bool { | ||
// Is the first character a letter or underscore? | ||
if !s | ||
.chars() | ||
.next() | ||
.map_or(false, |c| c.is_alphabetic() || c == '_') | ||
{ | ||
return false; | ||
} | ||
|
||
pub static IDENTIFIER_REGEX: Lazy<Regex> = | ||
Lazy::new(|| Regex::new(r"^[A-Za-z_][A-Za-z0-9_]*$").unwrap()); | ||
// Are the rest of the characters letters, digits, or underscores? | ||
s.chars().skip(1).all(|c| c.is_alphanumeric() || c == '_') | ||
} |
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
Oops, something went wrong.