Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
A
add-trailing-comma
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
add-trailing-comma
Commits
816714ba
Kaydet (Commit)
816714ba
authored
Tem 12, 2017
tarafından
Anthony Sottile
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Also add trailing commas for FunctionDef
üst
d4463454
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
77 additions
and
3 deletions
+77
-3
README.md
README.md
+7
-3
add_trailing_comma.py
add_trailing_comma.py
+26
-0
add_trailing_comma_test.py
tests/add_trailing_comma_test.py
+44
-0
No files found.
README.md
Dosyayı görüntüle @
816714ba
...
...
@@ -111,9 +111,6 @@ Note that this would cause a **`SyntaxError`** in earlier python versions.
]
```
## Planned features
### trailing commas for function definitions
```
diff
...
...
@@ -124,6 +121,13 @@ Note that this would cause a **`SyntaxError`** in earlier python versions.
):
```
Note that functions with starargs (
`*args`
), kwargs (
`**kwargs`
), or python 3
keyword-only arguments (
`..., *, ...`
) cannot have a trailing comma due to it
being a syntax error.
## Planned features
### unhug trailing paren
```
diff
...
...
add_trailing_comma.py
Dosyayı görüntüle @
816714ba
...
...
@@ -15,6 +15,7 @@ from tokenize_rt import UNIMPORTANT_WS
Offset
=
collections
.
namedtuple
(
'Offset'
,
(
'line'
,
'utf8_byte_offset'
))
Call
=
collections
.
namedtuple
(
'Call'
,
(
'node'
,
'star_args'
,
'arg_offsets'
))
Func
=
collections
.
namedtuple
(
'Func'
,
(
'node'
,
'arg_offsets'
))
Literal
=
collections
.
namedtuple
(
'Literal'
,
(
'node'
,
'braces'
,
'backtrack'
))
Literal
.
__new__
.
__defaults__
=
(
False
,)
...
...
@@ -53,6 +54,7 @@ def _is_star_star_kwarg(node):
class
FindNodes
(
ast
.
NodeVisitor
):
def
__init__
(
self
):
self
.
calls
=
{}
self
.
funcs
=
{}
self
.
literals
=
{}
self
.
has_new_syntax
=
False
...
...
@@ -135,6 +137,27 @@ class FindNodes(ast.NodeVisitor):
self
.
generic_visit
(
node
)
def
visit_FunctionDef
(
self
,
node
):
has_starargs
=
(
node
.
args
.
vararg
or
node
.
args
.
kwarg
or
# python 3 only
getattr
(
node
.
args
,
'kwonlyargs'
,
None
)
)
orig
=
node
.
lineno
is_multiline
=
False
offsets
=
set
()
for
argnode
in
node
.
args
.
args
:
offset
=
_to_offset
(
argnode
)
if
offset
.
line
>
orig
:
is_multiline
=
True
offsets
.
add
(
offset
)
if
is_multiline
and
not
has_starargs
:
key
=
Offset
(
node
.
lineno
,
node
.
col_offset
)
self
.
funcs
[
key
]
=
Func
(
node
,
offsets
)
self
.
generic_visit
(
node
)
def
_fix_inner
(
brace_start
,
brace_end
,
first_paren
,
tokens
):
i
=
first_paren
...
...
@@ -235,6 +258,9 @@ def _fix_commas(contents_text, py35_plus):
_fix_call
(
call
,
i
,
tokens
)
elif
key
in
visitor
.
literals
:
_fix_literal
(
visitor
.
literals
[
key
],
i
,
tokens
)
elif
key
in
visitor
.
funcs
:
# functions can be treated as calls
_fix_call
(
visitor
.
funcs
[
key
],
i
,
tokens
)
return
tokens_to_src
(
tokens
)
...
...
tests/add_trailing_comma_test.py
Dosyayı görüntüle @
816714ba
...
...
@@ -253,6 +253,50 @@ def test_noop_tuple_literal_without_braces():
assert
_fix_commas
(
src
,
py35_plus
=
False
)
==
src
@pytest.mark.parametrize
(
'src'
,
(
'def f(arg1, arg2): pass'
,
'def f(
\n
'
' arg1,
\n
'
' arg2,
\n
'
'): pass'
,
# *args forbid trailing commas
'def f(
\n
'
' *args
\n
'
'): pass'
# **kwargs forbid trailing commas
'def f(
\n
'
' **kwargs
\n
'
'): pass'
,
# keyword-only args forbid trailing commas (or are py2 syntax error)
'def f(
\n
'
' *, arg=1
\n
'
'): pass'
,
),
)
def
test_noop_function_defs
(
src
):
assert
_fix_commas
(
src
,
py35_plus
=
False
)
==
src
@pytest.mark.parametrize
(
(
'src'
,
'expected'
),
(
(
'def f(
\n
'
' x
\n
'
'): pass'
,
'def f(
\n
'
' x,
\n
'
'): pass'
,
),
),
)
def
test_fixes_defs
(
src
,
expected
):
assert
_fix_commas
(
src
,
py35_plus
=
False
)
==
expected
def
test_main_trivial
():
assert
main
(())
==
0
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment