32

I am using black==20.8b1.

I have a long string like:

return f"{self.name}, a {adjective.to_name()} {kin_string}{self._type.to_name()} who works for the {target.get_relationship_target_string()}."

I run:

$ black -l 80 . -t py38
All done! ✨  ✨
2 files left unchanged.

Why is the string not wrapped? I thought that black supports wrapping strings now (based on issues in github).

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
gruszczy
  • 40,948
  • 31
  • 128
  • 181

3 Answers3

29

Currently we have to add --experimental-string-processing tag to it. I think in future versions it will be made default.

black -l 80 --preview file.py
develmusa
  • 47
  • 11
Eka
  • 14,170
  • 38
  • 128
  • 212
  • 8
    Looks like we have a new flag to replace `--experimental-string-processing now`: `--preview`. When running with `--experimental-string-processing`: `:12: Deprecated: `experimental string processing` has been included in `preview` and deprecated. Use `preview` instead.` – Taylor D. Edmiston Apr 06 '22 at 18:17
16

Since 22.1.0 (Jan 29, 2022), you have to run Black with --preview.

GG.
  • 21,083
  • 14
  • 84
  • 130
  • Thank you! This works for me when I do `black --preview file.py`. How can this work in VScode? I have included `"python.formatting.blackArgs": [ "--preview" ]` in my project's settings.json, but line wrapping is not enforced. – KLaz Oct 20 '22 at 11:38
  • @KLaz not really sure. These are my settings in VSCode: `"python.formatting.provider": "black", "python.formatting.blackArgs": ["--preview"]` (on MacOS). Looks like we have the same. It works for me. Does your VSCode succeed to format a file in Python (I mean, other than the line wrapping)? Maybe it can't find the Black binary on your system. – GG. Oct 20 '22 at 16:06
13

Currently, Black doesn't wrap long strings or long comments. You can see an open issue in their project GitHub saying:

Black currently doesn't wrap long string literals or merge string literals that happen to be on the same line. [...] It would require modifying the AST which isn't 100% safe and has a bunch of edge cases to be dealt with.

Ronald Pereira
  • 397
  • 1
  • 3
  • 12
  • 1
    Do you know why this issue is closed https://github.com/psf/black/issues/182 I thought it was already done? – gruszczy Aug 31 '20 at 15:38