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
bc57cb44
Unverified
Kaydet (Commit)
bc57cb44
authored
Eyl 02, 2018
tarafından
Anthony Sottile
Kaydeden (comit)
GitHub
Eyl 02, 2018
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Sade Fark
Merge pull request #53 from asottile/from_import_trailing_comma
Add trailing commas for from imports
üst
35ce9905
88867768
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
98 additions
and
0 deletions
+98
-0
README.md
README.md
+10
-0
add_trailing_comma.py
add_trailing_comma.py
+22
-0
add_trailing_comma_test.py
tests/add_trailing_comma_test.py
+66
-0
No files found.
README.md
Dosyayı görüntüle @
bc57cb44
...
...
@@ -148,6 +148,16 @@ following change:
Note that this would cause a
**`SyntaxError`**
in earlier python versions.
### trailing commas for `from` imports
```
diff
from os import (
path,
- makedirs
+ makedirs,
)
```
### unhug trailing paren
```
diff
...
...
add_trailing_comma.py
Dosyayı görüntüle @
bc57cb44
...
...
@@ -64,6 +64,7 @@ class FindNodes(ast.NodeVisitor):
self
.
funcs
=
{}
self
.
literals
=
{}
self
.
tuples
=
{}
self
.
imports
=
set
()
def
_visit_literal
(
self
,
node
,
key
=
'elts'
):
if
getattr
(
node
,
key
):
...
...
@@ -143,6 +144,10 @@ class FindNodes(ast.NodeVisitor):
self
.
generic_visit
(
node
)
def
visit_ImportFrom
(
self
,
node
):
self
.
imports
.
add
(
Offset
(
node
.
lineno
,
node
.
col_offset
))
self
.
generic_visit
(
node
)
def
_find_simple
(
first_brace
,
tokens
):
brace_stack
=
[
first_brace
]
...
...
@@ -227,6 +232,18 @@ def _find_tuple(i, tokens):
return
_find_simple
(
i
,
tokens
)
def
_find_import
(
i
,
tokens
):
# progress forwards until we find either a `(` or a newline
for
i
in
range
(
i
,
len
(
tokens
)):
token
=
tokens
[
i
]
if
token
.
name
==
'NEWLINE'
:
return
elif
token
.
name
==
'OP'
and
token
.
src
==
'('
:
return
_find_simple
(
i
,
tokens
)
else
:
raise
AssertionError
(
'Past end?'
)
def
_fix_brace
(
fix_data
,
add_comma
,
tokens
):
first_brace
,
last_brace
=
fix_data
.
braces
...
...
@@ -346,6 +363,11 @@ def _fix_src(contents_text, py35_plus, py36_plus):
# Handle parenthesized things, unhug of tuples, and comprehensions
elif
token
.
src
in
START_BRACES
:
fixes
.
append
((
False
,
_find_simple
(
i
,
tokens
)))
elif
key
in
visitor
.
imports
:
# some imports do not have parens
fix
=
_find_import
(
i
,
tokens
)
if
fix
:
fixes
.
append
((
True
,
fix
))
for
add_comma
,
fix_data
in
fixes
:
if
fix_data
is
not
None
:
...
...
tests/add_trailing_comma_test.py
Dosyayı görüntüle @
bc57cb44
...
...
@@ -684,6 +684,72 @@ def test_fix_trailing_brace(src, expected):
assert
_fix_src
(
src
,
py35_plus
=
False
,
py36_plus
=
False
)
==
expected
@pytest.mark.parametrize
(
'src'
,
(
'from os import path, makedirs
\n
'
,
'from os import (path, makedirs)
\n
'
,
'from os import (
\n
'
' path,
\n
'
' makedirs,
\n
'
')'
,
),
)
def
test_fix_from_import_noop
(
src
):
assert
_fix_src
(
src
,
py35_plus
=
False
,
py36_plus
=
False
)
==
src
@pytest.mark.parametrize
(
(
'src'
,
'expected'
),
(
(
'from os import (
\n
'
' makedirs,
\n
'
' path
\n
'
')'
,
'from os import (
\n
'
' makedirs,
\n
'
' path,
\n
'
')'
,
),
(
'from os import
\\\n
'
' (
\n
'
' path,
\n
'
' makedirs
\n
'
' )
\n
'
,
'from os import
\\\n
'
' (
\n
'
' path,
\n
'
' makedirs,
\n
'
' )
\n
'
,
),
(
'from os import (
\n
'
' makedirs,
\n
'
' path,
\n
'
' )'
,
'from os import (
\n
'
' makedirs,
\n
'
' path,
\n
'
')'
,
),
(
'if True:
\n
'
' from os import (
\n
'
' makedirs
\n
'
' )'
,
'if True:
\n
'
' from os import (
\n
'
' makedirs,
\n
'
' )'
,
),
),
)
def
test_fix_from_import
(
src
,
expected
):
assert
_fix_src
(
src
,
py35_plus
=
False
,
py36_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