自动为函数调用和字面量添加尾随逗号
项目描述
add-trailing-comma
一个工具(以及pre-commit钩子),用于自动为函数调用和字面量添加尾随逗号。
安装
pip install add-trailing-comma
作为pre-commit钩子
有关说明,请参阅pre-commit
示例.pre-commit-config.yaml
-   repo: https://github.com/asottile/add-trailing-comma
    rev: v3.1.0
    hooks:
    -   id: add-trailing-comma
多行方法调用风格 -- 为什么?
# Sample of *ideal* syntax
function_call(
    argument,
    5 ** 5,
    kwarg=foo,
)
- 初始括号位于行尾
- 每个参数比函数名缩进一个级别
- 最后一个参数(除非调用包含解包(*args/**kwargs))后面有一个尾随逗号
这有以下好处
- 
避免任意缩进 # I hear you like 15 space indents # oh your function name changed? guess you get to reindent :) very_long_call(arg, arg, arg) 
- 
添加/删除参数保留 git blame,并且是最小的diff# with no trailing commas x( - arg + arg, + arg2 ) # with trailing commas x( arg, + arg2, ) 
实现的功能
函数调用中的尾随逗号
 x(
     arg,
-    arg
+    arg,
 )
元组/列表/字典/集合字面量中的尾随逗号
 x = [
-    1, 2, 3
+    1, 2, 3,
 ]
函数定义中的尾随逗号
 def func(
         arg1,
-        arg2
+        arg2,
 ):
 async def func(
         arg1,
-        arg2
+        arg2,
 ):
from导入中的尾随逗号
 from os import (
     path,
-    makedirs
+    makedirs,
 )
类定义中的尾随逗号
 class C(
     Base1,
-    Base2
+    Base2,
 ):
     pass
with语句中的尾随逗号
 with (
         open('f1', 'r') as f1,
-        open('f2', 'w') as f2
+        open('f2', 'w') as f2,
 ):
     pass
match语句中的尾随逗号
 match x:
     case A(
        1,
-       2
+       2,
     ):
        pass
     case (
        1,
-       2
+       2,
     ):
        pass
     case [
        1,
-       2
+       2,
     ]:
        pass
     case {
        'x': 1,
-       'y': 2
+       'y': 2,
     }:
        pass
PEP-695类型别名中的尾随逗号
 def f[
-    T
+    T,
 ](x: T) -> T:
     return x
 class A[
-    K
+    K,
 ]:
     def __init__(self, x: T) -> None:
         self.x = x
 type ListOrSet[
-     T
+     T,
] = list[T] | set[T]
取消紧缩尾括号
 x(
     arg1,
-    arg2)
+    arg2,
+)
取消紧缩首括号
-function_name(arg1,
-              arg2)
+function_name(
+    arg1,
+    arg2,
+)
匹配关闭花括号的缩进
 x = [
     1,
     2,
     3,
-    ]
+]
删除不必要的逗号
是的,我意识到了工具的名称叫做add-trailing-comma :laughing
-[1, 2, 3,]
-[1, 2, 3, ]
+[1, 2, 3]
+[1, 2, 3]
          项目详情
    
       关闭
    
      
        
    
    
  
哈希值 for add_trailing_comma-3.1.0-py2.py3-none-any.whl
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 160207e2ac414a841a71f4f5095f7350f87af460aab3dfe36cfa037992530e5c | |
| MD5 | 0d2364c2aaa9b6268532f8b7971d4a27 | |
| BLAKE2b-256 | ab44d54465acb1c929314408044d5f65dc7f420c6022e8f2027033dc9bb6b7f7 |