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

Splitting of a function across multiple lines doesn't add a trailing comma #346

Closed
hawkowl opened this issue Jun 13, 2018 · 10 comments
Closed
Labels
F: trailing comma Full of magic T: bug Something isn't working

Comments

@hawkowl
Copy link

hawkowl commented Jun 13, 2018

Operating system: Arch Linux
Python version: 3.6.5
Black version: master
Does also happen on master: yes

Consider the following line (unformatted):

return make_deferred_yieldable(threads.deferToThreadPool(self.hs.get_reactor(), self.hs.get_reactor().getThreadPool(), _do_hash))

Black formats it as thus:

return make_deferred_yieldable(
    threads.deferToThreadPool(
        self.hs.get_reactor(), self.hs.get_reactor().getThreadPool(), _do_hash
    )
)

My coworker @richvdh gathers that it should be formatted with a trailing comma after the threads.deferToThreadPool call for consistency with how multi-line lists are formatted, producing the following:

return make_deferred_yieldable(
    threads.deferToThreadPool(
        self.hs.get_reactor(), self.hs.get_reactor().getThreadPool(), _do_hash
    ),
)

Is this something Black should do? PEP8's trailing comma examples don't really cover this specific case, so I'm not sure :)

@zsol
Copy link
Collaborator

zsol commented Jun 13, 2018

This is because black can't detect that the file you're formatting is python3.6. Trailing commas are invalid syntax in some cases in older versions of python. If you run black with --py36, you should get the result you want

@hawkowl
Copy link
Author

hawkowl commented Jun 13, 2018

@zsol This is a function call, not a definition -- I can verify that this syntax is very much Python 2.7 compliant :)

@zsol
Copy link
Collaborator

zsol commented Jun 13, 2018

You're absolutely right, maybe I should read the code we're talking about ;)

@ambv
Copy link
Collaborator

ambv commented Jun 13, 2018

On this kind of split I'm not putting trailing commas because any change to line will result in the following:

  • if you remove an argument, the diff is only touching the changed line;
  • if you add another argument, and it still fits on the same line, the diff is only touching the changed line;
  • if you add another argument, and args no longer fit on the same line, all arguments are exploded so the diff touches the old line anyway.

In other words, the trailing comma doesn't add anything here. Better yet, it does up a character, making it more likely your arg list wouldn't fit in a single line.

Does this explanation make sense?

(note, we currently have a bug where a trailing comma is missing also from single-element collection literals that are exploded: #274; that will be fixed)

@richvdh
Copy link

richvdh commented Jun 13, 2018

Aren't the args to make_deferred_yieldable already exploded?

The goal here is to be able to add new args to that call without touching existing lines.

(I'd also argue that there should be a comma after _do_hash for consistency, but that is a different case and your logic is valid there)

@ambv
Copy link
Collaborator

ambv commented Jun 13, 2018

Aren't the args to make_deferred_yieldable already exploded?

Yes, there is one argument and it doesn't fit in one line, hence there is a trailing comma at the level of this function. However, arguments to threads.deferToThreadPool all fit in one line, hence the rules outlined by me above apply.

@richvdh
Copy link

richvdh commented Jun 13, 2018

Yes, there is one argument and it doesn't fit in one line, hence there is a trailing comma at the level of this function.

I think you are mistaken. Have another look at @hawkowl's report.

@ambv
Copy link
Collaborator

ambv commented Jun 13, 2018

Oh, yes, you're right. A single argument doesn't generate a trailing comma. I guess it should if that argument itself doesn't fit in one line.

Good catch, we'll fix it!

@ambv
Copy link
Collaborator

ambv commented May 7, 2019

This will likely get solved with #826.

@ambv
Copy link
Collaborator

ambv commented Mar 3, 2020

This will be handled as part of #1288.

@ambv ambv closed this as completed Mar 3, 2020
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: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants