Skip to content
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

Getting horizontal style inside list comprehensions #1377

Closed
mastern2k3 opened this issue May 3, 2020 · 2 comments
Closed

Getting horizontal style inside list comprehensions #1377

mastern2k3 opened this issue May 3, 2020 · 2 comments
Labels
T: bug Something isn't working

Comments

@mastern2k3
Copy link

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?

@mastern2k3 mastern2k3 added the T: bug Something isn't working label May 3, 2020
@ichard26
Copy link
Collaborator

ichard26 commented May 6, 2020

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.

@ichard26
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants