gruszczy
gruszczy

Reputation: 42198

Python: Black doesn't wrap long lines

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

Upvotes: 39

Views: 29476

Answers (4)

billibub
billibub

Reputation: 55

Seems like Black updated what they consider to be "preview". I got it to run using the options --preview --enable-unstable-feature string_processing:

black -l 80 -t py38 --preview --enable-unstable-feature string_processing -c 'long = "This is a long line that is longer than 88 characters. I expect Black to shorten this line length."'

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

NOTE: The last comment/answer to https://github.com/psf/black/issues/1802 (referenced in @eka's answer) doesn't seem work with Black version 24.8.0.

Upvotes: 1

Eka
Eka

Reputation: 15002

Use --preview option to trigger this behaviour.

Previously we had to add --experimental-string-processing option. This option still works but issues a depreciation warning.

I think in future versions it will be made default.

black -l 80 --preview file.py

Upvotes: 35

GG.
GG.

Reputation: 21854

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

Upvotes: 19

Ronald Pereira
Ronald Pereira

Reputation: 407

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.

Upvotes: 14

Related Questions