question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trailing comma for function which enforces keyword arguments (`f(*, a, b, ...)`)

See original GitHub issue

Didn’t initially realise that args had to be on separate lines for functions in order for add-trailing-comma to modify (in original comment). Realised that and added to # edit.


original comment

# requirements.txt
appdirs==1.4.4
cfgv==3.3.0
distlib==0.3.1
filelock==3.0.12
identify==2.2.6
nodeenv==1.6.0
pre-commit==2.13.0
PyYAML==5.4.1
six==1.16.0
toml==0.10.2
virtualenv==20.4.7
#.pre-commit-config.yaml 
minimum_pre_commit_version: 2.10.0
repos:
  - repo: https://github.com/asottile/add-trailing-comma
    rev: v2.1.0
    hooks:
      - id: add-trailing-comma
        args: [--py36-plus]

Running pre-commit as: pre-commit run --all-files I’m expecting (hoping…) with the following test file:

def A(*, a):
    return True

def B(a,b):
    return True

def C(*args):
    return True

x = [1, 2, 3]

def function_call(a, b, c):
    return True 

function_call(
    a=1, b=2, c=3
)

Gives the diff:

diff --git a/test.py b/test.py
index 6bc7d22..edf345f 100644
--- a/test.py
+++ b/test.py
@@ -13,5 +13,5 @@ def function_call(a, b, c):
     return True

 function_call(
-    a=1, b=2, c=3
+    a=1, b=2, c=3,
 )

Though the diff that I was expecting (…hoping?) for was:

diff --git a/test.py b/test.py
index 6bc7d22..768e441 100644
--- a/test.py
+++ b/test.py
@@ -1,17 +1,17 @@
-def A(*, a):
+def A(*, a,):
     return True

-def B(a,b):
+def B(a,b,):
     return True

-def C(*args):
+def C(*args,):
     return True

-x = [1, 2, 3]
+x = [1, 2, 3,]

-def function_call(a, b, c):
+def function_call(a, b, c,):
     return True

 function_call(
-    a=1, b=2, c=3
+    a=1, b=2, c=3,
 )

There’s probably something that I’m missing - but having looked through the readme I’m not sure what.

edit

Given the following file:

def A(*, 
a):
    return True

def B(a,
b):
    return True

def C(*args
):
    return True

x = [1, 2, 3]

def function_call(a, b, 
c):
    return True 

function_call(
    a=1, b=2, c=3
)

the diff is as expected having run : pre-commit run --all-files:

diff --git a/test.py b/test.py
index f37eb5c..361e427 100644
--- a/test.py
+++ b/test.py
@@ -1,21 +1,28 @@
-def A(*,
-a):
+def A(
+    *,
+    a,
+):
     return True

-def B(a,
-b):
+def B(
+    a,
+    b,
+):
     return True

-def C(*args
+def C(
+    *args,
 ):
     return True

 x = [1, 2, 3]

-def function_call(a, b,
-c):
+def function_call(
+    a, b,
+    c,
+):
     return True

 function_call(
-    a=1, b=2, c=3
+    a=1, b=2, c=3,
 )

Seems I didn’t realise that they had to be on separate lines in order for add-trailing-comma to pick up on them.

If there’s a way to set this so that add-trailing-comma added a trailing comma to functions all the time that would be ideal.

If not then this may have shifted from an issue to a feature request and can probably be closed 😄

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
asottilecommented, May 27, 2021

black absolutely does?

def f(
    this,
    function,
    has,
    a,
    whole,
    bunch,
    of,
    parameters,
    but,
    theres,
    no,
    trailing,
    comma
):
    pass
$ venv39/bin/black --diff t2.py 
--- t2.py	2021-05-27 15:45:24.778542 +0000
+++ t2.py	2021-05-27 15:45:26.187483 +0000
@@ -9,8 +9,8 @@
     parameters,
     but,
     theres,
     no,
     trailing,
-    comma
+    comma,
 ):
     pass
would reformat t2.py
All done! ✨ 🍰 ✨
1 file would be reformatted
0reactions
asottilecommented, May 27, 2021

you don’t actually want that, trust me. you’d end up with this ugly nonsense

-class C(A): pass
-
-f(1)
+class C(
+    A,
+): pass
+
+f(
+    1,
+)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Trailing commas - JavaScript - MDN Web Docs
JavaScript allows trailing commas wherever a comma-separated list of values is accepted and more values may be expected after the last item.
Read more >
Should I add a trailing comma after the last argument in a ...
One of the coding principles is that a function should do one thing, and one thing only. So, seeing a trailing comma there,...
Read more >
Allow trailing comma in any function argument list. #53478
A traling comma is allowed in tuples, lists, dicts, sets, the corresponding comprehensions, augmented assignments, and subscripts. It is also allowed in from ......
Read more >
Trailing Commas Are Just The Beginning – Pursuit Of Laziness
Trailing commas, also known as dangling commas, are a formatting strategy that's grown in popularity over time.
Read more >
Style :: RuboCop Docs
In Ruby 2.7, arguments forwarding has been added. This cop identifies places where do_something(*args, &block) can be replaced by do_something(…​) .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found