-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Finish "magic trailing comma" handling #1288
Comments
can we have a "black --classic" that ignores trailing commas already in the code? |
Do you have a case where this would be useful? The behavior described here is already the case since the last release. If you remove your trailing comma by hand and re-format, it will behave like you expect. |
I'd like an option where black will remove the trailing commas for me and add them back in if needed |
I've taken to running |
If you can implement this as |
@ambv great to see this issue crop up. We've so far avoided using black on the team I work on because we prioritise diff noise (and ease of code review) very highly. Being able to control this is a pragmatic solution to that problem that I think our team will enjoy, and hopefully gets us one step closer to implementing black. I'm not sure if this is related or a separate issue, but we feel that the 3 forms of expansion (one line, 3-line, and n-line) also contributes to the noise – we only use 2 forms in our style guide (one line, n-line). Do you see this issue as changing that behaviour as well, or do you think that's a separate issue? Expansions being: # one line
foo = [bar, baz]
def foo(bar, baz): ...
# 3-line
foo = [
bar, baz
]
def foo(
bar, baz
): ...
# n-line
foo = [
bar,
baz,
]
def foo(
bar,
baz,
): ... |
@danpalmer, magic trailing commas are a way for you to avoid Black formatting into the 3-line form. We won't always do this unless you use the trailing comma because it's important for Black to minimize vertical space usage unless you find it important to explode some structure. |
@ambv perfect. Sounds sensible. Looking forward to these changes! We happen to not prioritise vertical space at all, but I realise that’s particular to us. |
Could I add: explain how the magic comma work in end-user documentation |
Since there is quite some variety in the referenced issues, I just wanted to ask if/make sure that this will also apply to method calls. E.g., it seems #826 does not apply to those yet, either:
|
Yes, it will also apply to method calls. |
By the way, magical trailing comma works also with multiline imports which would otherwise be collapsed: This is perfectly acceptable for black 19.10b0
Which also works perfectly fine with following
(force splitting into multiline import when you import at least This produces smaller diffs in case when you change from
to
Maybe you could hint in readme that "A compatible |
Hi all, I have an issue with flake8 and black doing some pre-commit checks.
black remove space after last
If I add space then black gives me an error, if I remove it then flak8 gives me an error. I'm new to Python, but looking for a solution to my problem I found this issue. Hopefully, this will be solved. Currently, I have no workarounds and my PR fails. here are links to Azure pipelines reports: |
Tomasz, just remove the trailing comma manually for now. Black will not re-add it. |
Łukasz thanks for the help. |
Yes, it does. It probably breaks other stuff too and changes the text buffer quite a bit (if you've it set to format on save) which can be jarring. |
This commit simply runs "black" on all our Python files except those in testsuites/tests/. Note that I ran into one small difficulty when reformatting setup.py, where black was reformatting a couple of lines in the code in a way that it triggered the following E231 flake8 violations: setup.py:23:69: E231 missing whitespace after ',' setup.py:25:74: E231 missing whitespace after ',' This is a known issue with black... psf/black#1288 ... which is fortunately easily worked around by simply removing the offending comma (black does not add it back). Change-Id: I492eb1ca5fa371d063e691f7fe06c47daae60d4c
Used this hack from psf/black#1288 $ pip install black==19.3b0 && black . && pip install black==19.10b && black . I then manually reverted changes to a handful of explicit data structures where the magic trailing comma should be retained (indicates to black not to squash into one line). Doing this dramatically improves the changes from trying black version 21.7b0 (right now just four minor changes).
Used this hack from psf/black#1288 $ pip install black==19.3b0 && black . && pip install black==19.10b && black . I then manually reverted changes to a handful of explicit data structures where the magic trailing comma should be retained (indicates to black not to squash into one line). Doing this dramatically improves the changes from trying black version 21.7b0 (right now just four minor changes).
Used this hack from psf/black#1288 $ pip install black==19.3b0 && black . && pip install black==19.10b && black . I then manually reverted changes to a handful of explicit data structures where the magic trailing comma should be retained (indicates to black not to squash into one line). Doing this dramatically improves the changes from trying black version 21.7b0 (right now just four minor changes).
Used this hack from psf/black#1288 $ pip install black==19.3b0 && black . && pip install black==19.10b && black . I then manually reverted changes to a handful of explicit data structures where the magic trailing comma should be retained (indicates to black not to squash into one line). Doing this dramatically improves the changes from trying black version 21.7b0 (right now just four minor changes).
Is there the possibility to set that the magic final comma must always be used? I mean, is there a way to say to black that: l = [1, 2] must be formatted like this always, and not only if I put the comma? l = [
1,
2,
] |
AFAIK not without manually adding a trailing comma: |
Maybe this hack I shared earlier can be modified for your use case:
|
#826 introduced the concept of a magic trailing comma: if you, the programmer, explicitly put a trailing comma in a collection, it signals to Black that you want a given collection exploded into multiple lines, always. While this flies a bit in the way of "doesn't look at pre-existing formatting" and "non-configurability", it is extremely pragmatic and consistent with "make diffs as small as possible".
However, there's issues with it so far. It doesn't yet work on nested collections. Worse yet, in those cases it leaves trailing commas behind when a collection ends up compressed into one line. We need to fix those edge cases to make it generally dependable.
The text was updated successfully, but these errors were encountered: