-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Formatter quoting for f-strings with triple quotes #7826
Conversation
Current dependencies on/for this PR:
This comment was auto-generated by Graphite. |
Self::FString(f_string) => { | ||
let unprefixed = locator | ||
.slice(f_string.range) | ||
.trim_start_matches(|c| c != '"' && c != '\''); | ||
let triple_quotes = | ||
unprefixed.starts_with(r#"""""#) || unprefixed.starts_with(r#"'''"#); | ||
if f_string.values.iter().any(|value| match value { | ||
Expr::FormattedValue(ast::ExprFormattedValue { range, .. }) => { | ||
let string_content = locator.slice(*range); | ||
string_content.contains(['"', '\'']) | ||
if triple_quotes { | ||
string_content.contains(r#"""""#) || string_content.contains(r#"'''"#) | ||
} else { | ||
string_content.contains(['"', '\'']) | ||
} | ||
} | ||
_ => false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this logic belongs here (it should be part of normalize), but i'll wait for dhruv's f-string changes first
let unprefixed = locator | ||
.slice(f_string.range) | ||
.trim_start_matches(|c| c != '"' && c != '\''); | ||
let triple_quotes = | ||
unprefixed.starts_with(r#"""""#) || unprefixed.starts_with(r#"'''"#); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand this correctly, we need to figure out if this f-string is triple-quoted or not. If so, could we use ruff_python_ast::str::is_triple_quote
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That method only takes a prefix, so i'd need to extract the prefix here, which is surprisingly complex (iterators don't work because we don't want to allocate a new String when collecting)
**Summary** Quoting of f-strings can change if they are triple quoted and only contain single quotes inside. Fixes #6841 **Test Plan** New fixtures
Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>
Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>
d88f317
to
c5fe59a
Compare
CodSpeed Performance ReportMerging #7826 will improve performances by 4.06%Comparing Summary
Benchmarks breakdown
|
Summary Quoting of f-strings can change if they are triple quoted and only contain single quotes inside.
Fixes #6841
Test Plan New fixtures