Comments (17)
Haha, one of those 3.8 downloads is me :-)
from black.
In the time being can we add a warning when this happens so we can manually resolve it?
from black.
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.
from black.
By the way, here's the pip installs for Black from PyPI for July 2018:
python_version | percent | download_count |
---|---|---|
3.6 | 86.07% | 15,408 |
3.7 | 13.21% | 2,364 |
3.5 | 0.41% | 73 |
2.7 | 0.22% | 39 |
3.4 | 0.08% | 14 |
3.8 | 0.01% | 2 |
3.3 | 0.01% | 1 |
2.6 | 0.01% | 1 |
Total | 17,902 |
Source: pypinfo --start-date 2018-07-01 --end-date 2018-07-31 --percent --markdown black pyversion
from black.
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.
from black.
I wrote a flake8 plugin to forbid these aberrant constructs: https://pypi.org/project/flake8_implicit_str_concat/
from black.
Another related case I've managed to hit with black is when it joins \
-split string into one, and by doing so it violates line length limit. In this case (at least for now, while it's not implemented still), it'd be probably better if it did nothing? E.g.:
$ 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'
from black.
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"))
from black.
@aldanor line length violation should be a bug (I have also seen it with black==18.6b4). I think it deserves a separate issue.
from black.
@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
from black.
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.
from black.
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 r'something' f'another thing'
). This is where user action after the formatting is probably best.
from black.
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)
from black.
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.
from black.
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:
- two strings like
"STR1" "STR2"
don't fit on one line because the closing quote of STR1, the space, and the opening quote of STR2 are the 3 characters that cause the entire thing to not fit in a single line. So you will keep them on two lines. - but if you knew that it's safe to concatenate them, it would fit in a single line (without those 3 extra characters).
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:
a = (
"a"
"bb"
"ccc"
"dddd"
"eeeee"
"ffffff"
"ggggggg"
"hhhhhhhh"
"iiiiiiiii"
"jjjjjjjjjj"
"kkkkkkkkkkk"
"llllllllllll"
"mmmmmmmmmmmmmmmm"
)
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.
from black.
Having Black complete the concatenation would be great.
from black.
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
from black.
Related Issues (20)
- Black removes blank lines in between a function and a decorated class in a stub file HOT 3
- Create URL links to actual PRs rather than simply referencing the PR number HOT 1
- Sort `import` statements in lexicographical order HOT 2
- Fails to parse backslash on line by itself HOT 4
- Don't wrap `with` statement with multiple context managers in parentheses when the line only overflows because of the last one HOT 2
- Add pipx installation in documentation
- `--line-ranges` formats entire file when ranges are at EOF HOT 2
- It is my code
- The most recent example is the following table showing the number
- Imports inside functions should not have an empty line after them HOT 1
- AST safety check fails to catch incorrect f-string change HOT 6
- Failure to parse string templates with similar quotes HOT 4
- AttributeError: module 'black' has no attribute 'Mode'. Did you mean: 'mode'? HOT 1
- Unrelated testcases fail when total number of tests is increased beyond 367 HOT 3
- problematic formatting: indentation change in docstring HOT 7
- How to skip reformatting of Heroku files? HOT 4
- Crash on formatting `case case` syntax when line length set to be small HOT 2
- Style suggestiong: allow aligned dict values HOT 1
- Black cannot parse valid code HOT 2
- INTERNAL ERROR: Black produced invalid code: f-string: expecting '}' HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from black.