Black: Multiline string formatting. It this intended behavior?

Created on 6 Jul 2018  路  3Comments  路  Source: psf/black

I was surprised to see Black change this:

a = "&SortByValue=priority&SortByOrder=asc" \
    "&IndexToStartPaging={3}" \
    "&NumberOfElementsToShow={2}" \
    "&CacheDurationMinutes=0" \
    "&AllowAlternativeResults=false"

to this:

a = "&SortByValue=priority&SortByOrder=asc" "&IndexToStartPaging={3}" "&NumberOfElementsToShow={2}" "&CacheDurationMinutes=0" "&AllowAlternativeResults=false"
I could understand joining it one string. Both are valid but second one looks weird to me. Is this intended? The output also breaks line length rule.

Most helpful comment

I'm not the maintainer and not confident but it seems to be intended.

Note: don鈥檛 use backslashes for formatting or you鈥檒l lose your voting rights.

https://black.readthedocs.io/en/stable/reference/reference_functions.html?highlight=prefix#black.normalize_prefix

PEP 8 says:

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.

https://www.python.org/dev/peps/pep-0008/#maximum-line-length

So either of the following patterns are recommended for your case, I believe:

a = (
    "&SortByValue=priority&SortByOrder=asc"
    + "&IndexToStartPaging={3}"
    + "&NumberOfElementsToShow={2}"
    + "&CacheDurationMinutes=0"
    + "&AllowAlternativeResults=false"
)
a = (
    "&SortByValue=priority&SortByOrder=asc"
    "&IndexToStartPaging={3}"
    "&NumberOfElementsToShow={2}"
    "&CacheDurationMinutes=0"
    "&AllowAlternativeResults=false"
)

All 3 comments

I'm not the maintainer and not confident but it seems to be intended.

Note: don鈥檛 use backslashes for formatting or you鈥檒l lose your voting rights.

https://black.readthedocs.io/en/stable/reference/reference_functions.html?highlight=prefix#black.normalize_prefix

PEP 8 says:

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.

https://www.python.org/dev/peps/pep-0008/#maximum-line-length

So either of the following patterns are recommended for your case, I believe:

a = (
    "&SortByValue=priority&SortByOrder=asc"
    + "&IndexToStartPaging={3}"
    + "&NumberOfElementsToShow={2}"
    + "&CacheDurationMinutes=0"
    + "&AllowAlternativeResults=false"
)
a = (
    "&SortByValue=priority&SortByOrder=asc"
    "&IndexToStartPaging={3}"
    "&NumberOfElementsToShow={2}"
    "&CacheDurationMinutes=0"
    "&AllowAlternativeResults=false"
)

Thanks a lot for your response @gh640 . My surprise was not that Black corrects the use of backslashes. My surprise was with the output. Your two suggestions seem much better than what Black suggests.

Thanks for reporting, this is already tracked under #330
We're working on it :)

Was this page helpful?
0 / 5 - 0 ratings