I'm currently researching __Black__ as our default formatter, but, I'm having some edge cases that don't format well and I want to know if there's a way to get the result I want.
__Black__'s documentation [partially explores my problem], I have a dictionary expression spread horizontally, and I want to keep it that way since I'm expecting lines to be added, e.g.:
# Black would keep this as-is because of the trailing comma
TRANSLATIONS = {
"en_us": "English (US)",
"pl_pl": "polski",
}
But in my case the dictionary is inside a list comprehension:
res = [
{
'id': item.id,
'name': item.name,
}
for item in items.select()
]
Which Black collapses, regardless of the trailing comma, like so:
res = [
{"id": item.id, "name": item.name,}
for item in items.select()
]
Is there a way of telling Black to retain the horizontal structure in these cases?
Related to #1288. Quoting from the issue:
(issue) 826 introduced the concept of a magic trailing comma... (some text removed for brevity)
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.
As I know of, there isn't an open pull request about fixing this behaviour.
Fixed by #1612 and available too as Black 20.08b1 has been released!
R:\Programming\black>black --version
black, version 20.8b1
R:\Programming\black>type temp.py
res = [
{
'id': item.id,
'name': item.name,
}
for item in items.select()
]
R:\Programming\black>black temp.py --diff --color
--- temp.py 2020-08-26 19:46:11.122937 +0000
+++ temp.py 2020-08-26 19:46:58.548227 +0000
@@ -1,7 +1,7 @@
res = [
{
- 'id': item.id,
- 'name': item.name,
+ "id": item.id,
+ "name": item.name,
}
for item in items.select()
]
would reformat temp.py
All done! ✨ 🍰 ✨
1 file would be reformatted.