Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiline string formatting. It this intended behavior? #397

Closed
testvinder opened this issue Jul 6, 2018 · 7 comments
Closed

Multiline string formatting. It this intended behavior? #397

testvinder opened this issue Jul 6, 2018 · 7 comments

Comments

@testvinder
Copy link

testvinder commented Jul 6, 2018

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.

@gh640
Copy link

gh640 commented Jul 7, 2018

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

Note: don’t use backslashes for formatting or you’ll 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"
)

@testvinder
Copy link
Author

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.

@zsol
Copy link
Collaborator

zsol commented Jul 7, 2018

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

@zsol zsol closed this as completed Jul 7, 2018
@sealor
Copy link

sealor commented Jan 12, 2023

I have a similar problem with formatting. IMHO black should at least support multi-lines strings where every line is a real line with a new line character.

Example code and expected result after formatting:

a = (
    "@@ -1,2 +1 @@\n"
    "-garbage1\n"
    "-garbage2\n"
    "+Hello World!\n"
)

Not intended and formatted code by black:

a = "@@ -1,2 +1 @@\n" "-garbage1\n" "-garbage2\n" "+Hello World!\n"

@ichard26
Copy link
Collaborator

@sealor Experimental string processing makes it possible to keep your example split (#1540), although ESP's future is still TBD.

@sealor
Copy link

sealor commented Jan 27, 2023

@ichard26
Thanks! This would be the optimal solution.

I found the following solution for my current case:

a = (
    "@@ -1,2 +1 @@\n",
    "-garbage1\n",
    "-garbage2\n",
    "+Hello World!\n",
)

(note the , at the end of each line)

@OlehChyhyryn
Copy link

@ichard26 Thanks! This would be the optimal solution.

I found the following solution for my current case:

a = (
    "@@ -1,2 +1 @@\n",
    "-garbage1\n",
    "-garbage2\n",
    "+Hello World!\n",
)

(note the , at the end of each line)

It is a tuple, not a string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants