-
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
Implicit concatenated string formatting #9457
Comments
I am in favor of this proposal. I don't want implicit string concatenation ever, whether it's on a single line or multiple, so anything that helps avoid it is good by me. |
Thank you for this detailed proposal! I'm in In favor as well: I like the feature and I don't see why anyone would want this kind of string staying around in the code. This would satisfy my need reported in #8272. I don't mind if this proposal becomes the default behavior or is behind a config option, but imo the argument of reversibility is not that much of a blocker, for two main reasons:
|
I, too, would prefer making this as the default behavior of the Ruff formatter. |
Two challenges need careful handling:
We don't need to handle multiline strings because they always expand on multiple lines. |
Regarding your first point, what would happen if there's a newline in the second string ( Python is converting it to normal strings by escaping the content in the raw string:
It's more prominent when you print it out:
|
yeah, I just realized that myself. There's also the issue that raw strings don't support escapes. So we might need to convert the other way round but I'm scarred doing this because I remember how confused I was last time when I tried to understand raw strings. |
I believe in you @MichaReiser 😃 |
Me too. |
I remain sceptical ;) |
I view this proposal as a pragmatic step forward for Ruff. |
I'd expect
Those changes should remove the Only (the few?) users with 1:
|
This is now available in preview mode, see #13663 Please give it a try and let me know if you run into any problems. |
This is a proposal for changing our implicit concatenated string formatting to solve #8272
The problem in the mentioned issue is that
ISC001
is incompatible with the formatter because the formatter might format an implicit concatenated string on a single line, which triggersISC001
Which triggers
ISC001
.Today
We either format all parts of an implicit concatenated string or a single line or format each part on its own line, if necessary:
Proposal
I propose to implement a subset of Black's improved string processing by automatically joining implicit concatenated strings into a single string if the joined string fits on a single line:
The motivation behind it is that, to my knowledge, there's no real reason for using an implicit concatenated string if it fits on a single line. The primary use case for implicit concatenated strings is to make a string fit into the desired line length by splitting it over multiple lines.
Considerations
The main downside is that joining the strings is non-reversible. Reversability is a desired property (Ruff's formatting tends to be reversible except for the trailing comma insertion) because it ensures that if you get the same formatting after making changes to the code and then undoing them again (without using revert, but by manually undoing your changes).
For example, let's say you shorten a sentence in a string that makes the implicit concatenated string fit. Ruff would join it. But Ruff wouldn't split it if you extended the sentence by the same number of characters you previously removed, likely resulting in a string that now exceeds the configured line width.
That's probably why Black's improved string processing automatically joins and splits strings.
I'm undecided about whether we should accept the non-reversibility. Still, I think it solves an actual problem and is less involved than implementing Black's entire improved string processing formatting. Meaning it's an opportunity to provide value today. In other words. It's a first step towards automatically splitting strings and we accept non-reversability until we get there.
The text was updated successfully, but these errors were encountered: