Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upMerge implicitly concatenated string literals that fit on one line #26
Comments
This comment has been minimized.
This comment has been minimized.
|
In this case Black is suggesting that you should merge the two strings into one and the result is more readable that way. I don't do this automatically (yet?) because it gets complicated if the two strings don't share the same prefix (for example |
This comment has been minimized.
This comment has been minimized.
|
Right, that makes sense. I think whether the strings on the same line are merge-able is clear (i.e. do they have the same prefix), but yes it's a rare case; feel free to close And changing beyond that may require discretion (e.g. turning 6 lines of 2/3-long lines into 4 full length lines) |
This comment has been minimized.
This comment has been minimized.
|
There's another related problem: if I merged string literals, I am now making semantic changes to the AST. I'm not opposed to those but this will make safety checks after reformatting trickier. Let's leave this open for the time being, it's an interesting problem. |
ambv
changed the title
Handling long strings
Merge implicitly concatenated string literals that fit on one line
Mar 15, 2018
ambv
added
the
enhancement
label
Mar 16, 2018
This comment has been minimized.
This comment has been minimized.
|
I'm not even sure what I would expect black to do with code that implicit-concatenates two differently prefixed strings to be honest. I think the path of least surprise is just leaving them alone. |
This comment has been minimized.
This comment has been minimized.
|
Yeah, it they are different prefixes, leave them alone. If they share a prefix and they end up on the same line, they should be merged. If you really want to be correct here the implementation is going to be hard in the following edge case:
I'm inclined not to touch this edge case since that makes it tricky where to perform the merge. Another small edge case which I'm inclined to avoid is this:
Technically Black could implement the "fill" algorithm for this case that Prettier also has for JSX. But I think what it currently does is fine for simplicity and obvious for users to recognize. |
This comment has been minimized.
This comment has been minimized.
aldanor
commented
Jul 15, 2018
|
Another related case I've managed to hit with black is when it joins $ black -S -l30 --diff long_str.py
--- long_str.py 2018-07-15 12:24:14 +0000
+++ long_str.py 2018-07-15 12:24:41.221434 +0000
@@ -1,5 +1,2 @@
-s = '111111111111111111111' \
- '222222222222222222222' \
- '333333333333333333333' \
- '444444444444444444444'
+s = '111111111111111111111' '222222222222222222222' '333333333333333333333' '444444444444444444444' |
This comment has been minimized.
This comment has been minimized.
e3krisztian
commented
Aug 1, 2018
|
@aldanor line length violation should be a bug (I have also seen it with black==18.6b4). I think it deserves a separate issue. |
This comment has been minimized.
This comment has been minimized.
graingert
commented
Aug 20, 2018
•
|
@ambv Python 3.7 has AST level constant folding: https://bugs.python.org/issue29469 so implicit string concatenation - or lack thereof - would be invisible to the AST check |
This comment has been minimized.
This comment has been minimized.
|
I know, @graingert, but we can't require Python 3.7+ for all Black users. Not yet at least. What I'm pondering is if we should rather switch to do the AST post-check using typed-ast which would make it work exactly the same on both Python 2 and Python 3. And the same on all Python 3 versions. |
This comment has been minimized.
This comment has been minimized.
|
By the way, here's the pip installs for Black from PyPI for July 2018:
Source: |
This comment has been minimized.
This comment has been minimized.
|
Haha, one of those 3.8 downloads is me :-) |
This comment has been minimized.
This comment has been minimized.
bertjwregeer
commented
Oct 16, 2018
|
Having Black complete the concatenation would be great. |
This comment has been minimized.
This comment has been minimized.
|
In the time being can we add a warning when this happens so we can manually resolve it? |
This comment has been minimized.
This comment has been minimized.
davidism
commented
Feb 18, 2019
|
I'd definitely like to see a warning for this, maybe something like "Implicit string concatenation in line N not merged." An issue I've run into is that someone writing multiple similar strings, in tests for example, will add continuations for all the strings so they look the same, even though some would fit on a single line. Then Black moves them back to a single line but leaves the continuation sitting in the middle. The user was trying to satisfy the formatting rules, but ended up producing less ideal formatting without knowing it. |
This comment has been minimized.
This comment has been minimized.
luxcem
commented
Mar 28, 2019
|
What's your view on this example? Black left it unchanged. def foo():
return "Some long string cut in half," " this is really a long string"
def bar(text):
return text
bar(("Some long string cut in half," " this is really a long string")) |
sirosen
added a commit
to sirosen/globus-cli
that referenced
this issue
Jul 19, 2019
sirosen
added a commit
to sirosen/globus-cli
that referenced
this issue
Jul 19, 2019
This comment has been minimized.
This comment has been minimized.
peterjc
commented
Aug 9, 2019
•
|
Is there an open issue for the doing the opposite? I've found when black has left long lines in my code, it is usually overly long strings (mostly error messages, and when defining command line arguments). Black could break long strings over multiple lines with implicit continuation (e.g. at spaces, or hyphens). I appreciate this would mean black having to set a convention for if the break point space should be trailing at the end of a truncated line, or leading at the start of a continued line. Found it: See #182 |
max-sixty commentedMar 15, 2018
Black could make single-line strings over multiple lines (i.e. a number of single quotes strings on multiple lines surrounded by parentheses) more efficient, by resizing them to the full length of the line.
Even if that was overreach, there's a peculiar situation where you end up with multiple strings on the same line, like below: