-
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
format ExprJoinedStr #5932
format ExprJoinedStr #5932
Conversation
PR Check ResultsBenchmarkLinux
Windows
|
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.
Wow. Thanks for being so bold in tackling an as complicated formatting as f-strings.
oh. not sure how i managed to miss this, but black doesn't (yet) format expressions inside f-strings |
83da9a0
to
ef0aaec
Compare
so this approach works.. not sure what we think of it though note that |
a1f19c0
to
40a06c4
Compare
if joined_str.values.iter().any(|value| match value { | ||
Expr::FormattedValue(ast::ExprFormattedValue { range, .. }) => { | ||
let string_content = locator.slice(*range); | ||
string_content.contains('"') || string_content.contains('\'') |
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.
Nit: To avoid iterating over the string twice
string_content.contains('"') || string_content.contains('\'') | |
string_content.contains(['"', '\'']) |
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.
ah thanks, was hoping something like this existed but couldn't find it
enum Quoting { | ||
CanChange, | ||
Preserve, | ||
} |
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.
enum Quoting { | |
CanChange, | |
Preserve, | |
} | |
#[derive(Copy, Clone)] | |
enum Quoting { | |
CanChange, | |
Preserve, | |
} |
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.
open to better names (of the enum) btw, there are lots of related things around quoting behaviour
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 like Preserve
and tried to come up with a better name than CanChange
but failed. I thought about Normalize
to align with normalize_string
but this isn't disabling normalization, it only affects the quotes.
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 meant more to replace Quoting
(since we already have StringQuotes
and QuoteStyle
)
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 think it's fine. The use is very local and it conveys way more information than a plain boolean.
40a06c4
to
32d1060
Compare
yes, sorry, fixed in #6202. Part of the problem is that i forgot to make the formatter ecosystem checks required for merging (but i don't think github properly shows in the PR UI that we could still merge your PR even with that check failing). |
32d1060
to
6f79f32
Compare
This is awesome. Are you interested in looking into "proper" f-string formatting? It isn't as high a priority to us as increasing coverage on other nodes (see August goals), but it would be a real differentiator to black |
sure. do we need some mechanism to opt out of this behaviour for all the tests that are comparing to black? |
Are you expecting many differences? If not, then I don't think it's worth bothering. We can otherwise make it an option on |
i thought i'd seen a bunch but maybe i'm misremembering i started looking (maybe we can make an issue to have these discussions there instead) and came across something to run by you: currently
ie the escaped curly is rendered into the constant. is that what we want? if so i guess we need special handling when rendering to reproduce the escaping. or do we want to parse as something closer to the source and store the string do we do anything corresponding for regular string escapes like |
A discussion would be good because I tend to not look at merged PRs and the discussion also goes beyond of what this PR addressed. Generally, it is very common for ASTs to undo string escapes, because the goal is to represent the semantic of the program and not the representation of the program in the source (this is different to a CST that preserves the code as is). You can see that Ruff's parser removes the escape sequences of quotes too: Playground. I assume this happens inside of |
reuses most of the (constant) string formatting, adding ability to skip changing quote style, if that would require escaping quotes inside formatted values which isn't allowed (in py < 3.12)
fixes #5913