Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 954
gruszczy Avatar asked Aug 31 '20 03:08

gruszczy


People also ask

How do you wrap a long string in Python?

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. If necessary, you can add an extra pair of parentheses around an expression, but sometimes using a backslash looks better.

Does Black format docstrings?

Black also processes docstrings. Firstly the indentation of docstrings is corrected for both quotations and the text within, although relative indentation in the text is preserved.

Is Black pep8 compliant?

Black is a PEP 8 compliant opinionated formatter. Black reformats entire files in place. Style configuration options are deliberately limited and rarely added.

What does black do python?

Black is the uncompromising Python code formatter. By using it, you agree to cede control over minutiae of hand-formatting. In return, Black gives you speed, determinism, and freedom from pycodestyle nagging about formatting. You will save time and mental energy for more important matters.


Video Answer


3 Answers

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
like image 114
Eka Avatar answered Oct 19 '22 07:10

Eka


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.

like image 21
Ronald Pereira Avatar answered Oct 19 '22 07:10

Ronald Pereira


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

like image 7
GG. Avatar answered Oct 19 '22 07:10

GG.