question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

Black compromises on commas in nested dict literals? šŸ˜±

See original GitHub issue
D = {
    "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:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
Pequecommented, Nov 12, 2019

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?

2reactions
durin42commented, Nov 5, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found