yapf
yapf copied to clipboard
A knob to turn on string splitting
It would be nice to have a knob for turning this:
mystr = "A really long string that is going to go over the character limit, but as I'm writing this I forget to create a line break"
into this:
mystr = "A really long string that is going to go over the" \
" character limit, but as I'm writing this I forget " \
"to create a line break"
Especially when the string is surrounded by brackets. These two are semantically equivalent (with the backslash), and the second is much nicer.
This would be nice, though I believe parentheses are preferred over backslashes:
mystr = ("A really long string that is going to go over the"
" character limit, but as I'm writing this I forget "
"to create a line break")
Either one works. Maybe even have it be an option?
I'm not sure how to go about tackling this, is there somewhere in the code that I can start looking at to insert something like this?
It's a design decision that yapf won't add or remove characters from the token stream[*]. I made this decision in order to ensure that no semantic changes would be introduced.
If someone would like to add this though, it could be done as a lib2to3
"fixer". Those fixers make modifying the code rather easy. Much easier than can be done in the formatter. If someone did create a fixer, it could be added in a "contrib" sub-directory that could be called via a command line option.
[*] There is one exception to this rule, which I grudgingly added. It adds fake parentheses to help in formatting dictionary structures in "chromium" style.
I really really would like this feature. I've had code like a)
"really long string and such ..."
b)
("really long string " +
"and such ...")
c)
("really long string "
"and such ...")
And in all cases I'd like the formatter to join all strings both with and without a plus operator, and do optimal splitting.
I think this is a major hole in this formatter. One of the primary reasons I started using this was so that it could handle this kind of tedious manual labor of string formatting. It should really be an option - you can accompany it with whatever warnings you feel necessary.
black
doesn't do this kind of string reformatting either. black
does add parens to import statements (yapf
doesn't based on its design philosophy), although I don't like the format that it uses.
I agree that it would be nice to have a tool that reformats strings and also adds parentheses to import statements, but perhaps that would be best handled by something based on 2to3
?
yes, please add this feature as an option, it could be very useful
Is there any python formatter available that supports this?
[EDIT]: Just found out that black supports this since 2023: https://github.com/psf/black/issues/1802