-
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
Black compromises on commas in nested dict literals? 😱 #1129
Comments
Bisected to 9854d4b. |
From my perspective this is WAI, because black has to preserve trailing commas in order to key collection-exploding behavior off them. |
Could you retype or rephrase that? I’m not in the know and I can’t parse the last half... |
In my change (#826) the goal was to preserve a collection across multiple lines unconditionally if-and-only-if it has a trailing comma. In order to get that behavior, black had to stop throwing out trailing commas, which it used to do. |
The collection with the comma, |
That kind of has two parts to it, so I'm going to write out two questions and answer them: Q) Why isn't the trailing comma deleted? A) Because in order to implement #826 we had to stop deleting trailing commas. Black used to do that uncondtionally and then reinsert them if a collection was split onto multiple lines. Now it simply no longer deletes trailing commas. Q) If that collection has a trailing comma, why isn't it exploded onto multiple lines? A) Because I couldn't figure out how to make the logic work coherently for "inner" collection literals, the explode-if-trailing-comma logic only applies on the outermost collection literal. Hopefully that clarifies? In any event I'm not a black maintainer, so I can't speak to whether or not they consider either of these behaviors a defect. |
I'm not sure if this is related but I'm facing the same issue about a comma left in dictionary. code.py:
returns:
|
As @dimaqq pointed out, Black is okay with either formatting, and that should not be the case. If I had to choose, I would prefer not having a trailing comma when the collection is on a single line (for simplicity and aesthetics). If I had to be more more objective, then I would say that is still a better choice since it would be |
I think this also applies to tuple unpacking. Black is happy with both: first_item, second_item, third_item = [1, 2, 3]
(first_item, second_item, third_item,) = [1, 2, 3] I think black should always use the first. |
Whatever choice you make, the It currently says:
|
This is covered by #1288. |
Oddly
black
is happy with and without trailing comma in the inner dict literal.And there I thought it was an
uncompromising code formatter
!;-)The text was updated successfully, but these errors were encountered: