Trailing comma for function which enforces keyword arguments (`f(*, a, b, ...)`)
See original GitHub issueDidn’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:
- Created 2 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
black absolutely does?
you don’t actually want that, trust me. you’d end up with this ugly nonsense