Black: Multi-line string separated by \ results in line exceeding line length

Created on 9 Aug 2018  路  2Comments  路  Source: psf/black

Operating system: macOS Sierra (10.12.6)
Python version: 3.7.0
Black version: 18.6b4
Does also happen on master: yes (but I also see one test failure on master so I might be doing this wrong)

Input file:

def myfn():
    val = "string"
    my_str_w_long_name = f"This is a string string string string string {val}"\
                         f"string string string string string {val}"

Output file:

def myfn():
    val = "string"
    my_str_w_long_name = f"This is a string string string string string {val}" f"string string string string string {val}"

Unexpected outcomes:

  • Two separate f-strings in one line
  • Resulting line exceeds max line length
  • Additional empty line added making flake8 say: W391: blank line at end of file

Other observations:

  • Does not happen when the second line of the string assignment also terminates in \
  • Does not happen when the \ isn't there
  • Does happen when there is a space character before the \
bug

Most helpful comment

Yep, black should put those parenthesis there. We're going to fix this

All 2 comments

Just adding my observation here.
I had a program equivalent to the following;

a = "<xml>"\
    "<a>hello</a>"\
    "<a>hello</a>"\
    "<a>hello</a>"\
    "<a>hello</a>"\
    "<a>hello</a>"\
    "<a>hello</a>"\
    "<a>hello</a>"\
    "<a>hello</a>"\
    "<a>hello</a>"\
    "<a>hello</a>"\
    "<a>hello</a>"\
    "<a>hello</a>"\
    "<a>hello</a>"\
    "<a>hello</a>"\
    "<a>hello</a>"\
    "<a>hello</a>"\
    "<a>hello</a>"\
    "<a>hello</a>"\
    "<a>hello</a>"\
    "<a>hello</a>"\
    "<a>hello</a>"\
    "</xml>"

when I ran black on it it produced this output;

a = "<xml>" "<a>hello</a>" "<a>hello</a>" "<a>hello</a>" "<a>hello</a>" "<a>hello</a>" "<a>hello</a>" "<a>hello</a>" "<a>hello</a>" "<a>hello</a>" "<a>hello</a>" "<a>hello</a>" "<a>hello</a>" "<a>hello</a>" "<a>hello</a>" "<a>hello</a>" "<a>hello</a>" "<a>hello</a>" "<a>hello</a>" "<a>hello</a>" "<a>hello</a>" "<a>hello</a>" "</xml>"

The output is 336 lines which is longer than pep8's 80 char limit or blacks' default 88 char limit.
And it looks really ugly.

For comparison:
When I ran autopep8 on the original, it left the file as is without any change.
When I ran yapf on the original, it left the file as is without any change.

I think black should also either;

  1. leave the file as is without any change, or
  2. change the output to;
a = ("<xml>"
     "<a>hello</a>"
     "<a>hello</a>"
     "<a>hello</a>"
     "<a>hello</a>"
     "<a>hello</a>"
     "<a>hello</a>"
     "<a>hello</a>"
     "<a>hello</a>"
     "<a>hello</a>"
     "<a>hello</a>"
     "<a>hello</a>"
     "<a>hello</a>"
     "<a>hello</a>"
     "<a>hello</a>"
     "<a>hello</a>"
     "<a>hello</a>"
     "<a>hello</a>"
     "<a>hello</a>"
     "<a>hello</a>"
     "<a>hello</a>"
     "<a>hello</a>"
     "</xml>")

Yep, black should put those parenthesis there. We're going to fix this

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kissgyorgy picture kissgyorgy  路  3Comments

Curly-Mo picture Curly-Mo  路  3Comments

decibyte picture decibyte  路  3Comments

dgnsrekt picture dgnsrekt  路  3Comments

bhearsum picture bhearsum  路  3Comments