-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Keep lists etc split across multiple lines #601
Comments
For me this is more about readability as well as consistency. One case I find it especially taxing to read mixed formatting is when using click decorators. This can require the eyes of a reader to bounce back and forth significantly, making the code more difficult to read. Another case is comprehensions. I find it is always easier for programmers (especially those new/unfamiliar with Python) to read a comprehensions written like: [
expression
for item in iterable
if condition
] I try to write my code so it's as easy as possible for someone to understand and, hopefully, contribute. This is one of the lowest hanging fruits toward that goal. Having this syntax formatted based on line length also creates inconsistency that can confuse people new to Python. Why is it written this way here, but not there? |
I think need add of the inline parametrize. |
considering this is on the very top of the documentation:
i agree. it also hurts readability, like in this example of a logging configuration:
|
It seems to me that this would break a symmetry. Code formatted with |
Yes, as @jebob says, the proposal from this issue would break the larger principle that Black-formatted code looks the same no matter what it originally looked like. Another alternative is to always split collection displays into multiple lines, but that seems undesirable too (we'd split code like |
Perhaps I'm missing something... but if this isn't controlled strictly by line length as proposed, it shouldn't be affected by roundtripping changing the line length as described.
That is what has been suggested by the OP, myself, and others in this issue as being desirable. Black tries for uniformity, specifically for horizontal whitespace. While I didn't post code, my description of click decorators can easily exhibit a lack of uniformity that is distracting and harder to read. I don't see why changing your given example into 4 lines is undesirable; it would then be consistent with other pieces of code like it within a codebase that gets reformatted onto multiple lines. And arguments given why black splits pieces of code like it still apply (e.g. smaller diffs). As it is now, I can only possibly use black as a pre-formatter followed by my manually re-formatting the black-formatted code. If it's preferable for the splitting behavior to be controlled by an option rather than hard-coded one way or another, I'm definitely fine with that. |
The way |
Wouldn't it make more sense to do so iff there's no trailing comma? |
Shoot, I inverted the logic halfway through. Yes, what you said is what clang-format does. Sorry for the confusion. |
We'll keep the current behavior for now. |
This issue also bothers me. When defining many lists (or in our case, Django models), it really hurts readability and diffs that some are multi-line and some are single-line. My thought would be to add a Would the project be willing to consider a PR that adds this functionality? |
We're hoping the approach in #826 would fix this pain point. |
This brings psf/black#826, which helps with psf/black#601.
This brings psf/black#826, which helps with psf/black#601.
This brings psf/black#826, which helps with psf/black#601.
Black's wrapping of lists to fit a single line causes diffs to become bigger,
and often makes the code harder to read.
When removing the 3rd line from the list used in the for loop, it will become
short enough to fit a single line and black re-formats it:
Orig:
New:
Black:
I think black should instead keep the original list style (which matches what
black would do with more entries), and only remove the single line.
This would not need an option, but could be respected in general: if a list (or
dictionary etc) is wrapped already like black would wrap it with more entries
than would fit on a single line it should keep it like this.
The list itself should still get re-formatted, but should just be considered to not fit a single line.
The text was updated successfully, but these errors were encountered: