black: Black Fails to Format Single String Longer Than Line Length Limit

Describe the bug A Python file containing a single string assignment longer than the line length limit is not reformatted.

To Reproduce Take long_line.py:

long = "This is a long line that is longer than 88 characters. I expect Black to shorten this line length."

Run Black:

$ black ./long_line.py 
All done! ✨ 🍰 ✨
1 file left unchanged.

Expected behavior I expect black to shorten the line length to less than the length limit. One possible solution is to break on the <line length limit minus 1> character and parenthesize if it is not already inside parentheses:

long = (
    "This is a long line that is longer than 88 characters. I expect Black to shorten "
    "this line length."
)

Environment (please complete the following information):

$ black --version
black, version 20.8b1

OS: macOS Catalina 10.15.6

$ python --version
Python 3.8.5

Does this bug also happen on master? Yes

Additional context None

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 28 (1 by maintainers)

Commits related to this issue

Most upvoted comments

This is currently implemented under the hidden --experimental-string-processing flag, which we’ll hopefully be able to enable soon.

If wanting to specify --experimental-string-processing in pyproject.toml use this format:

[tool.black]
experimental-string-processing = true

Same as @Mausy5043 and @pheetah ! Docstrings are not taken into account. Can provide example if needed!|

Tried --preview and the deprecated --experimental-string-processing.

$ black --version
black, 22.10.0 (compiled: yes)
Python (CPython) 3.10.11

Use of the switch --experimental-string-processing is nolonger supported and returns:

<string>:13: Deprecated: `experimental string processing` has been included in `preview` and deprecated. Use `preview` instead.

Using the --preview switch, apart from not knowing what else is being changed, does not result in strings getting processed correctly.

I have the same issue with @Mausy5043 , when I try to format with black -l 80 --preview it works for code lines but not for strings (I have a string has 416 characters left untouched on formatting), when I use black --experimental-string-processing I get the same error.

Hello would somebody help me how I can integrate this into vim? So executing :Black will be use the flag --experimental-string-processing. Thanks

@FantasticMrFux Did you ever find the answer to this?

For anyone who still hasn’t been able to use this, I have gotten this working on my VSCode with the following settings.json

"python.formatting.blackArgs": [
    "--line-length=80",
    "--experimental-string-processing"
],

Or using the gui:

image

I think that the line length argument may be an issue with many people inputting the actual number as an additional argument after the flag rather than using the = operator.

For example, see this stackoverflow answer.

If I wanted to configure this flag on VScode should I just add --experimental-string-processing as an argument?

the below settings work .

/usr/local/bin/black --version black, version 21.9b0

"python.formatting.provider": "black",
 "python.formatting.blackArgs": [
        "--line-length",
        "80",
        "--experimental-string-processing"
    ],
  "python.formatting.blackPath": "/usr/local/bin/black",