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: Printf string formatting #1803

Merged
merged 77 commits into from
Jan 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
bf7e246
Got some basic stuff in
colin99d Jan 11, 2023
bdb3b43
Merge branch 'main' into PrintfStringFormatting
colin99d Jan 11, 2023
84717ea
GOt ONE unit test passing, LETS GO
colin99d Jan 12, 2023
7efb4ec
Better handle empty strings
colin99d Jan 12, 2023
d4fabbb
Fix how i handle string_end
colin99d Jan 12, 2023
a32ce74
Fixed another bug
colin99d Jan 12, 2023
37069f7
Finished unit tests
colin99d Jan 12, 2023
a4bd9a0
Merge branch 'main' into PrintfStringFormatting
colin99d Jan 12, 2023
c240f43
About to actually get this thing linting
colin99d Jan 13, 2023
99ce56e
Finished the body of the printf_string_formatting function
colin99d Jan 13, 2023
5e6939d
Fixed the regex
colin99d Jan 13, 2023
58a1d7d
Finished unit tests (2 failing, and also simplified them BIG time)
colin99d Jan 13, 2023
0756ffa
Minor wording change
colin99d Jan 13, 2023
dadb075
Fixed one bug
colin99d Jan 13, 2023
25be7ae
Fixed last issue with unit tests
colin99d Jan 13, 2023
d1c9d26
Cleaned prints
colin99d Jan 13, 2023
d863ce5
Going through errors
colin99d Jan 13, 2023
bfa6ad2
better handling
colin99d Jan 14, 2023
3265eb7
Added integration tests
colin99d Jan 14, 2023
edcbb5d
Fixed one issue
colin99d Jan 14, 2023
757c71d
Fixed the nested issue
colin99d Jan 14, 2023
92d5503
Increase comments
colin99d Jan 14, 2023
abb9276
Merged with new main
colin99d Jan 14, 2023
daf5da8
Two edge cases left on tuple
colin99d Jan 14, 2023
0fa0d8c
Refactored check_statement outside of main function
colin99d Jan 14, 2023
bb9c4c4
Better handling
colin99d Jan 14, 2023
dd88455
Git rid of nesting, cleaner code now
colin99d Jan 14, 2023
e520d83
Fixed one of two tuple errors, just need to fix regex one now
colin99d Jan 14, 2023
3212e67
In progress, but need to switch
colin99d Jan 15, 2023
ca40881
Moving towards a complete dict implementation
colin99d Jan 15, 2023
b36d8c3
Final testing for dict
colin99d Jan 15, 2023
c7c626d
Updated test cases
colin99d Jan 15, 2023
9e5ac12
Ran new tests
colin99d Jan 15, 2023
c952500
Fixed formatting, removed prints
colin99d Jan 15, 2023
e738b4a
linted clippy and added docs
colin99d Jan 15, 2023
e1647cd
Fixed typos and fmt
colin99d Jan 15, 2023
9d41062
Added correct level of visibility
colin99d Jan 15, 2023
3f5148f
Replaced string with Option<String> to increase readability
colin99d Jan 15, 2023
5f89844
Merge branch 'main' into PrintfStringFormatting
charliermarsh Jan 17, 2023
07688c0
Minor tweaks
charliermarsh Jan 17, 2023
8526f11
Fix reference
charliermarsh Jan 17, 2023
3e9f728
Simplified to a broken curly_escape
colin99d Jan 17, 2023
420f934
Fixed dict tests
colin99d Jan 17, 2023
0793afd
Fixed embarassing mistake in docstring
colin99d Jan 17, 2023
e7390be
Added unit tests for the issues Charlie found
colin99d Jan 17, 2023
0bee8af
Merge branch 'main' into PrintfStringFormatting
charliermarsh Jan 17, 2023
b042fd8
Merged
colin99d Jan 18, 2023
e571735
Merged
colin99d Jan 18, 2023
de90d4d
Fixed glitch from merge
colin99d Jan 18, 2023
ebee722
Began conversion to cformatstring
colin99d Jan 18, 2023
32aec6b
Progressed
colin99d Jan 18, 2023
6056ebc
Merge branch 'main' into PrintfStringFormatting
colin99d Jan 18, 2023
92872b6
Got a little further
colin99d Jan 19, 2023
7d8d14c
Merge branch 'main' into PrintfStringFormatting
charliermarsh Jan 19, 2023
923b43d
Merge remote-tracking branch 'refs/remotes/origin/main' into PrintfSt…
charliermarsh Jan 20, 2023
b06be38
Fixed two tests
colin99d Jan 20, 2023
acfdb51
Merged
colin99d Jan 20, 2023
675f6fc
working towards a better solution
colin99d Jan 20, 2023
e8702e5
Fixed tests
colin99d Jan 20, 2023
522eb70
Merge branch 'main' into PrintfStringFormatting
charliermarsh Jan 20, 2023
6d89c87
Upgrade RustPython parser
charliermarsh Jan 20, 2023
837b487
Fix Clippy
charliermarsh Jan 20, 2023
8f54ebd
Mid refactor
charliermarsh Jan 20, 2023
d805dc7
Merge branch 'main' into PrintfStringFormatting
charliermarsh Jan 20, 2023
e8e6912
Misc changes
charliermarsh Jan 20, 2023
4660713
Mid-way through conversion
charliermarsh Jan 20, 2023
1c7179d
Merge branch 'main' into PrintfStringFormatting
charliermarsh Jan 20, 2023
dac86ab
Merge branch 'main' into PrintfStringFormatting
charliermarsh Jan 20, 2023
76ddc62
Try to move tests
charliermarsh Jan 21, 2023
012144b
Add prints
charliermarsh Jan 21, 2023
3cfc93a
Add some restrictions
charliermarsh Jan 21, 2023
60e73d5
Try out a lexer
charliermarsh Jan 21, 2023
31ae316
Support emoji
charliermarsh Jan 21, 2023
39fd5bc
Merge branch 'main' into PrintfStringFormatting
charliermarsh Jan 21, 2023
a5e5c1d
Fix ) case
charliermarsh Jan 21, 2023
b179026
Merge branch 'main' into PrintfStringFormatting
charliermarsh Jan 21, 2023
85d75e8
Point to latest RustPython
charliermarsh Jan 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
better handling
  • Loading branch information
colin99d committed Jan 14, 2023
commit bfa6ad28e8f7226d33f73e2e661fbd6bb0fbdc62
3 changes: 1 addition & 2 deletions resources/test/fixtures/pyupgrade/UP031.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
"brace {} %s" % (1,)

"%s" % (
"trailing comma"
"trailing comma",
)

"%s \N{snowman}" % (a,)

# Make sure to include assignment and inside a call, also multi-line
51 changes: 43 additions & 8 deletions src/pyupgrade/rules/printf_string_formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ fn parse_percent_format(string: &str) -> Vec<PercentFormat> {
}

/// Removes the first instance of a given element from a vector
fn remove(vec: &mut Vec<char>, item: char) {
fn remove<T: std::cmp::PartialEq + std::marker::Copy>(vec: &mut Vec<T>, item: T) {
let index = vec.iter().position(|&x| x == item).unwrap();
vec.remove(index);
}
Expand Down Expand Up @@ -247,18 +247,53 @@ fn percent_to_format(string: &str) -> String {
final_string
}

/// If the tuple has one argument it removes the comma, otherwise it returns the tuple as is
fn clean_right_tuple(checker: &Checker, right: &Expr) -> String {
// FOR REVIEWER: Let me know if you want this redone in libcst, the reason I didnt is because
// it starts as a Tuple, but ends as a Call
let right_range = Range::new(right.location, right.end_location.unwrap());
let mut base_string = checker
.locator
.slice_source_code_range(&right_range)
.to_string();
// let is_multi_line = base_string.contains('\n');
if let ExprKind::Tuple { elts, .. } = &right.node {
if elts.len() == 1 {
/*
let mut string = String::from('(');
if is_multi_line {
string.push('\n');
let indent = leading_space()
string.push_str(indent);
}
let sub_range = Range::from_located(elts.get(0).unwrap());
let sub_str = checker.locator.slice_source_code_range(&sub_range).to_string();
string.push_str(&sub_str);
if is_multi_line {
string.push('\n');
}
string.push(')');
return string
*/
// FOR REVIEWER: This replaces ALL commas when there is only one item, I couldn't think
// of any instanced where this would cause an issue, but let me know if you can and I
// will fix
base_string = base_string.replace(',', "");
}
}
base_string
}

fn fix_percent_format_tuple(checker: &mut Checker, right: &Expr, left_string: &str) -> String {
// Pyupgrade explicitly checks for ' % (' before running, but I am not sure the value of this
// (pyupgrade itself says it is overly timid). The one edge case I considered was a multi line
// format statement, but worst-case scenario we go over the limit and black fixes it. Let me
// know if you want this check implemented
let right_range = Range::new(right.location, right.end_location.unwrap());
let right_string = checker.locator.slice_source_code_range(&right_range);
let right_string = clean_right_tuple(checker, right);
let mut cleaned_string = percent_to_format(left_string);
cleaned_string.push_str(".format");
cleaned_string.push_str(&right_string);
cleaned_string

}

fn fix_percent_format_dict(checker: &mut Checker, left: &Expr, right: &Expr) -> String {
Expand Down Expand Up @@ -342,8 +377,8 @@ pub fn printf_string_formatting(checker: &mut Checker, left: &Expr, right: &Expr
}
// all dict substitutions must be named
if let ExprKind::Dict { .. } = &right.node {
// Technically a value of "" would also count as `not key`, BUT we already have a check
// above for this
// Technically a value of "" would also count as `not key`, (which is what the python
// code uses) BUT we already have a check above for this
if fmt.key.is_none() {
no_breaks = false;
break;
Expand All @@ -364,9 +399,9 @@ pub fn printf_string_formatting(checker: &mut Checker, left: &Expr, right: &Expr
if !new_string.is_empty() {
let replace_range = Range::new(left.location, right.end_location.unwrap());
let old_string = checker.locator.slice_source_code_range(&replace_range);
println!("{} => {}", old_string, new_string);
if new_string != old_string {
let mut diagnostic = Diagnostic::new(violations::PrintfStringFormatting, replace_range);
let mut diagnostic =
Diagnostic::new(violations::PrintfStringFormatting, replace_range);
if checker.patch(diagnostic.kind.code()) {
diagnostic.amend(Fix::replacement(
new_string,
Expand Down