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

Pyupgrade: Format specifiers #1594

Merged
merged 31 commits into from
Jan 11, 2023
Merged
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
672c984
Began work on the parser
colin99d Jan 2, 2023
c127480
Continued progress
colin99d Jan 2, 2023
5e686c4
Further along but broken
colin99d Jan 3, 2023
e599734
Fixed tests
colin99d Jan 5, 2023
c68a9da
Added more checks
colin99d Jan 5, 2023
b8c06e7
Added all should pass edge cases
colin99d Jan 5, 2023
6f3c4e5
Fixing small mistakes
colin99d Jan 5, 2023
37d25dd
Added fixes
colin99d Jan 5, 2023
287f90f
Replaced lazy_static with lazy
colin99d Jan 6, 2023
cb836f9
Fixed merge conflicts
colin99d Jan 6, 2023
7645bc5
Fixed typos
colin99d Jan 6, 2023
dfee10f
Fixed incorrect import
colin99d Jan 6, 2023
aa714a8
Hunting down error with: cargo run resources/test/fixtures/pyupgrade/…
colin99d Jan 7, 2023
ec15215
Merged
colin99d Jan 9, 2023
257c828
For a multiline print statement just add a check and not a fix
colin99d Jan 9, 2023
782fce3
Added fix for helper functions, and column for SDK type
colin99d Jan 9, 2023
db92e9b
Updated mod
colin99d Jan 9, 2023
5adbe05
Aded fixes
colin99d Jan 9, 2023
1fd88cd
Added negative cases, fixed one negative edge case
colin99d Jan 9, 2023
fc6af20
Handled one more negative edge case
colin99d Jan 9, 2023
7920747
Made progress in testing
colin99d Jan 9, 2023
aa0c2cb
Clippy and fmt
colin99d Jan 9, 2023
29241ef
Fixed merge conflicts
colin99d Jan 9, 2023
ef493b2
Added new tests
colin99d Jan 9, 2023
0d395a3
Fixed linters
colin99d Jan 9, 2023
76fbc56
Fixed last bug
colin99d Jan 9, 2023
5121008
Fixed clippy and docs
colin99d Jan 9, 2023
c237e4e
Merge branch 'main' into formatspecifiers
colin99d Jan 10, 2023
75ca3ba
Merge branch 'main' into formatspecifiers
charliermarsh Jan 10, 2023
4e84dd6
Use ? in lieu of match, rename to format_literals
charliermarsh Jan 11, 2023
14a01ec
Use format.rs
charliermarsh Jan 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/pyupgrade/plugins/format_specififiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::cst::matchers::{match_call, match_expression};
// The regex documentation says to do this because creating regexs is expensive:
// https://docs.rs/regex/latest/regex/#example-avoid-compiling-the-same-regex-in-a-loop
lazy_static! {
colin99d marked this conversation as resolved.
Show resolved Hide resolved
static ref RE: Regex = Regex::new(r"\{(\d+)\}").unwrap();
static ref FORMAT_SPECIFIER: Regex = Regex::new(r"\{(?P<int>\d+)?(?P<fmt>.*?)\}").unwrap();
}

/// Convert a python integer to a unsigned 32 but integer. We are assuming this
Expand Down Expand Up @@ -107,14 +107,17 @@ fn get_specifier_order(value_str: &str) -> Vec<u32> {
/// Returns a string without the format specifiers. Ex. "Hello {0} {1}" ->
/// "Hello {} {}"
fn remove_specifiers(raw_specifiers: &str) -> String {
RE.replace_all(raw_specifiers, "{}").to_string()
println!("BEFORE: {}", raw_specifiers);
let new_str = FORMAT_SPECIFIER.replace_all(raw_specifiers, "{$fmt}").to_string();
println!("AFTER: {}\n", new_str);
new_str
}

/// Checks if there is a single specifier in the string. The string must either
/// have all formatterts or no formatters (or else an error will be thrown), so
/// this will work as long as the python code is valid
fn has_specifiers(raw_specifiers: &str) -> bool {
RE.is_match(raw_specifiers)
FORMAT_SPECIFIER.is_match(raw_specifiers)
}

/// UP029
Expand All @@ -133,7 +136,6 @@ pub fn format_specifiers(checker: &mut Checker, expr: &Expr, func: &Expr) {
None => return,
Some(item) => item,
};
println!("{}", new_call);
let mut check =
Check::new(CheckKind::FormatSpecifiers, Range::from_located(expr));
if checker.patch(check.kind.code()) {
Expand Down