-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Description
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 triggers ISC001
# Input
a = (
"bbbbbbb"
"ccccccc"
)
## Formatted
a = "bbbbbbb" "ccccccc"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:
# If it fits
a = "bbbbbbb" "ccccccc"
# If it exceeds the line width
a = (
"aaaaaaaaa"
"bbbbbbbbbbbbbbbbbbbbb"
"ccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
)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:
# If it fits
a = "bbbbbbbccccccc" # Note that it's now a single string.
# If it exceeds the line width
a = (
"aaaaaaaaa"
"bbbbbbbbbbbbbbbbbbbbb"
"ccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
)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.