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

multiple function args get put on their own line even with very long line length #1405

Closed
dhalbert opened this issue May 11, 2020 · 1 comment
Labels
R: not a bug This is deliberate behavior of Black.

Comments

@dhalbert
Copy link

dhalbert commented May 11, 2020

Describe the bug
Even when the line length would permit a single-line function definition, the args are separated from their surrounding parentheses

To Reproduce
black -l 1000 on this file always puts the f4 args on their own line:

def f3(this_is_a_very_long_name, this_is_a_very_long_name, this_is_a_very_long_name):
    pass


def f4(
    this_is_a_very_long_name, this_is_a_very_long_name, this_is_a_very_long_name, this_is_a_very_long_name,
):
    pass

Expected behavior

def f3(this_is_a_very_long_name, this_is_a_very_long_name, this_is_a_very_long_name):
    pass


def f4(this_is_a_very_long_name, this_is_a_very_long_name, this_is_a_very_long_name, this_is_a_very_long_name):
    pass

Environment

  • Version: 19.10b0
  • OS and Python version: Ubuntu 20.04, Python 3.8.2

Does this bug also happen on master?
Yes (via a clone/virtualenv test).

I assume this is not really a bug. So I must be missing some rule about arguments I cannot find in PEP8 or the black documentation.

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

ichard26 commented Aug 2, 2020

This is not a bug, in fact this is a feature called the "magic trailing comma". It was introduced via #826... so before the release of 19.10b0. When you want Black to always explode a collection literal even if it doesn't violate the line length limit, put a trailing comma and tada, Black will explode it. (Side-note: this feature is still needs more work to fully usable, e.g. nested collection literals are unsupported ATM). So if you remove the trailing comma, Black will collapse the function signature:

(black) richard-26@ubuntu-laptop:~/programming/oss/black$ diff -u --color original.py test.py
--- original.py	2020-08-02 18:59:34.595331550 -0400
+++ test.py	2020-08-02 18:52:29.429873691 -0400
@@ -3,6 +3,6 @@
 
 
 def f4(
-    this_is_a_very_long_name, this_is_a_very_long_name, this_is_a_very_long_name, this_is_a_very_long_name,
+    this_is_a_very_long_name, this_is_a_very_long_name, this_is_a_very_long_name, this_is_a_very_long_name
 ):
     pass
(black) richard-26@ubuntu-laptop:~/programming/oss/black$ black test.py --diff --color -l 1000
--- test.py	2020-08-02 22:52:29.429874 +0000
+++ test.py	2020-08-02 23:00:07.137429 +0000
@@ -1,8 +1,6 @@
 def f3(this_is_a_very_long_name, this_is_a_very_long_name, this_is_a_very_long_name):
     pass
 
 
-def f4(
-    this_is_a_very_long_name, this_is_a_very_long_name, this_is_a_very_long_name, this_is_a_very_long_name
-):
+def f4(this_is_a_very_long_name, this_is_a_very_long_name, this_is_a_very_long_name, this_is_a_very_long_name):
     pass
would reformat test.py
All done! ✨ 🍰 ✨
1 file would be reformatted.

You can read up it more here in the docs

Hopefully that helps!


Environment:

  • Black version: master (537ea8d)
  • Python version: CPython 3.8.1
  • OS version: Ubuntu 18.04.04 LTS

@ichard26 ichard26 added R: not a bug This is deliberate behavior of Black. and removed T: bug Something isn't working labels Aug 3, 2020
@ichard26 ichard26 closed this as completed Aug 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
R: not a bug This is deliberate behavior of Black.
Projects
None yet
Development

No branches or pull requests

3 participants
@dhalbert @ichard26 and others