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

Trailing comma is not deleted in a one-line dict literal #2052

Closed
Wim-De-Clercq opened this issue Mar 19, 2021 · 5 comments
Closed

Trailing comma is not deleted in a one-line dict literal #2052

Wim-De-Clercq opened this issue Mar 19, 2021 · 5 comments
Labels
F: trailing comma Full of magic T: style What do we want Blackened code to look like?

Comments

@Wim-De-Clercq
Copy link

Describe the style change
When a specific dict with callables gets the key looked up and the callable called in one go, the formatting is both uglier than the original and also invalid for flake8.

Examples in the current Black style
Before formatting:

value = "1"
key = "integer"

x = {
    "string": str,
    "integer": int,
}[key](value)

After formatting:

value = "1"
key = "integer"

x = {"string": str, "integer": int,}[
    key
](value)

This is not valid for flake8, and looks worse than what it was.

Desired style

value = "1"
key = "integer"

x = {
    "string": str,
    "integer": int,
}[key](value)

Or perhaps

value = "1"
key = "integer"

x = {"string": str, "integer": int}[key](value)
@Wim-De-Clercq Wim-De-Clercq added the T: style What do we want Blackened code to look like? label Mar 19, 2021
@JelleZijlstra
Copy link
Collaborator

This looks like we're mishandling the magic trailing comma.

@haxsaw
Copy link

haxsaw commented Mar 20, 2021

I'm also getting trailing comma problems in other circumstances. Here is some code pre-black, formatted by PyCharm:

Deployment(apiVersion='apps/v1beta1', kind='Deployment',
           metadata=ObjectMeta(name='nginx-app-2', labels={'app': 'nginx'}),
           spec=DeploymentSpec(selector=LabelSelector(matchLabels={'app': 'nginx'}),
                               template=PodTemplateSpec(
                                   metadata=ObjectMeta(labels={'app': 'nginx'}),
                                   spec=PodSpec(containers=[
                                       Container(name='nginx', image='nginx:1.15.4',
                                                 ports=[
                                                     ContainerPort(containerPort=80)])])),
                               replicas=3))

And here's how black leaves it:

Deployment(
    apiVersion="apps/v1beta1",
    kind="Deployment",
    metadata=ObjectMeta(name="nginx-app-2", labels={"app": "nginx"}),
    spec=DeploymentSpec(
        selector=LabelSelector(matchLabels={"app": "nginx"}),
        template=PodTemplateSpec(
            metadata=ObjectMeta(labels={"app": "nginx"}),
            spec=PodSpec(
                containers=[
                    Container(
                        name="nginx",
                        image="nginx:1.15.4",
                        ports=[ContainerPort(containerPort=80)],
                    )
                ]
            ),
        ),
        replicas=3,
    ),
)

To be fair, the black code is correct and is much nicer to read, but those unnecessary trailing commas are unfortunate.

@Wim-De-Clercq
Copy link
Author

The trailing commas you have there are intentional to have a cleaner history in git.
If you have

settings = {
   "one": 1
}

For commit 1 and for another issue you have to add a new setting in commit 2:

settings = {
   "one": 1,
   "two": 2
}

Then your history of the line "one": 1 will show as latest commit number 2 in the history.
Unless you use the trailing commas.

@haxsaw
Copy link

haxsaw commented Mar 21, 2021

If that's the case, it doesn't seem to be an evenly applied rule. For instance from the fragment I published, why doesn't:

                containers=[
                    Container(
                        name="nginx",
                        image="nginx:1.15.4",
                        ports=[ContainerPort(containerPort=80)],
                    )
                ]

have a comma after the ')' that comes before the closing ']'? The last parameter to Container has a comma after it, but the Container() creation itself doesn't. For this rule, shouldn't it be every dict or list?

Additionally, for this larger fragment:

    spec=DeploymentSpec(
        selector=LabelSelector(matchLabels={"app": "nginx"}),
        template=PodTemplateSpec(
            metadata=ObjectMeta(labels={"app": "nginx"}),
            spec=PodSpec(
                containers=[
                    Container(
                        name="nginx",
                        image="nginx:1.15.4",
                        ports=[ContainerPort(containerPort=80)],
                    )
                ]
            ),
        ),
        replicas=3,
    ),

Putting a comma after the 'replicas=3' makes the assumption that there are other params that might be added that would benefit from the rule you cited above. While true in this case, there's no way for Black to know whether params have be exhausted or not, and so it adds a bit of visual noise for a small git optimization that may never be taken advantage of.

It strikes me that the trade-off to consider here is whether reading an extra line in a git diff happens more or less than reading and understanding code with a superfluous comma. I would expect that reading the code happens more than the git diff, but that's just me.

@hauntsaninja
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: trailing comma Full of magic T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

5 participants