Black compromises on commas in nested dict literals? š±
See original GitHub issueD = {
"dummy": None,
**({"has comma": 123,} if some_global else {}),
}
D = {
"dummy": None,
**({"no comma?": 123} if some_global else {}),
}
Oddly black
is happy with and without trailing comma in the inner dict literal.
And there I thought it was an uncompromising code formatter
!š
> black t.py
All done! āØ š° āØ
1 file left unchanged.
> black --version
black, version 19.10b0
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:11 (5 by maintainers)
Top Results From Across the Web
How can I break a line of very long nested dictionaries?
Use black Python code formatter (note: by default, ... By adding more keys, you may find out how black will split even longer...
Read more >Python Nested Dictionary (With Examples) - Programiz
In Python, a nested dictionary is a dictionary inside a dictionary. It's a collection of dictionaries into one single dictionary. nested_dict = {...
Read more >Softpanorama Bookshelf
Barack Obama had just been elected the first black president. ... "$250 sneakers to black kids in the inner city who can't afford...
Read more >Python: Update Nested Dictionary - GeeksforGeeks
A Dictionary in Python works similar to the Dictionary in the real world. Keys of a Dictionary must be unique and of immutable...
Read more >Python - Nested Dictionaries - W3Schools
A dictionary can contain dictionaries, this is called nested dictionaries. Example. Create a dictionary that contain three dictionaries: myfamily = { "child1" :Ā ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
flake8
compatible. So why further differ from other widely used linters/formatters?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.