black
black copied to clipboard
string_processing: Handle overloaded operators when splitting long strings
I'm not sure how often pathlib.Path is used with the augmented assignment operator rather than the regular one (/) nor do I know how often the string would be long enough that it would need to be split but all that aside, /= can definitely appear with strings on the right-hand of the assignment operator too:
from pathlib import Path
x = Path.cwd()
x /= "directory/subdirectory/file.txt"
Originally posted by @jack1142 in https://github.com/psf/black/issues/2312#issuecomment-855959706
Thanks for reporting! I think since these keep coming, we ought to consider the full set of possible operators.
@jack1142 Raises a good point here. This issue of no current support for long string splitting with the /= operator extends to many other operators that are not traditionally supported by strings. In other words, since essentially any operator can be overloaded to support strings, we need to support them all.
@felix-hilden Beat me by 19 seconds ;).
To be more concrete, the problem here is:
% black --line-length=20 --preview -c 'x /= "directory subdirectory file.txt"'
x /= "directory subdirectory file.txt"
This should split up the string literal to make it fit in 20 characters.