black icon indicating copy to clipboard operation
black copied to clipboard

string_processing: Handle overloaded operators when splitting long strings

Open bbugyi200 opened this issue 4 years ago • 4 comments

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

bbugyi200 avatar Jun 13 '21 20:06 bbugyi200

Thanks for reporting! I think since these keep coming, we ought to consider the full set of possible operators.

felix-hilden avatar Jun 13 '21 20:06 felix-hilden

@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.

bbugyi200 avatar Jun 13 '21 20:06 bbugyi200

@felix-hilden Beat me by 19 seconds ;).

bbugyi200 avatar Jun 13 '21 20:06 bbugyi200

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.

JelleZijlstra avatar Jan 29 '22 16:01 JelleZijlstra