66import _ast
77import unparse
88
9- forelse = """\
9+ for_else = """\
1010 def f():
1111 for x in range(10):
1212 break
@@ -15,7 +15,7 @@ def f():
1515 z = 3
1616"""
1717
18- whileelse = """\
18+ while_else = """\
1919 def g():
2020 while True:
2121 break
@@ -24,6 +24,37 @@ def g():
2424 z = 3
2525"""
2626
27+ relative_import = """\
28+ from . import fred
29+ from .. import barney
30+ from .australia import shrimp as prawns
31+ """
32+
33+ nonlocal_ex = """\
34+ def f():
35+ x = 1
36+ def g():
37+ nonlocal x
38+ x = 2
39+ y = 7
40+ def h():
41+ nonlocal x, y
42+ """
43+
44+ # also acts as test for 'except ... as ...'
45+ raise_from = """\
46+ try:
47+ 1 / 0
48+ except ZeroDivisionError as e:
49+ raise ArithmeticError from e
50+ """
51+
52+ class_decorator = """\
53+ @f1(arg)
54+ @f2
55+ class Foo: pass
56+ """
57+
2758class UnparseTestCase (unittest .TestCase ):
2859 # Tests for specific bugs found in earlier versions of unparse
2960
@@ -43,10 +74,10 @@ def test_shifts(self):
4374 self .check_roundtrip ("13 >> 7" )
4475
4576 def test_for_else (self ):
46- self .check_roundtrip (forelse )
77+ self .check_roundtrip (for_else )
4778
4879 def test_while_else (self ):
49- self .check_roundtrip (whileelse )
80+ self .check_roundtrip (while_else )
5081
5182 def test_unary_parens (self ):
5283 self .check_roundtrip ("(-1)**7" )
@@ -57,6 +88,50 @@ def test_chained_comparisons(self):
5788 self .check_roundtrip ("1 < 4 <= 5" )
5889 self .check_roundtrip ("a is b is c is not d" )
5990
91+ def test_function_arguments (self ):
92+ self .check_roundtrip ("def f(): pass" )
93+ self .check_roundtrip ("def f(a): pass" )
94+ self .check_roundtrip ("def f(b = 2): pass" )
95+ self .check_roundtrip ("def f(a, b): pass" )
96+ self .check_roundtrip ("def f(a, b = 2): pass" )
97+ self .check_roundtrip ("def f(a = 5, b = 2): pass" )
98+ self .check_roundtrip ("def f(*, a = 1, b = 2): pass" )
99+ self .check_roundtrip ("def f(*, a = 1, b): pass" )
100+ self .check_roundtrip ("def f(*, a, b = 2): pass" )
101+ self .check_roundtrip ("def f(a, b = None, *, c, **kwds): pass" )
102+ self .check_roundtrip ("def f(a=2, *args, c=5, d, **kwds): pass" )
103+ self .check_roundtrip ("def f(*args, **kwargs): pass" )
104+
105+ def test_relative_import (self ):
106+ self .check_roundtrip (relative_import )
107+
108+ def test_nonlocal (self ):
109+ self .check_roundtrip (nonlocal_ex )
110+
111+ def test_raise_from (self ):
112+ self .check_roundtrip (raise_from )
113+
114+ def test_bytes (self ):
115+ self .check_roundtrip ("b'123'" )
116+
117+ def test_annotations (self ):
118+ self .check_roundtrip ("def f(a : int): pass" )
119+ self .check_roundtrip ("def f(a: int = 5): pass" )
120+ self .check_roundtrip ("def f(*args: [int]): pass" )
121+ self .check_roundtrip ("def f(**kwargs: dict): pass" )
122+ self .check_roundtrip ("def f() -> None: pass" )
123+
124+ def test_set_literal (self ):
125+ self .check_roundtrip ("{'a', 'b', 'c'}" )
126+
127+ def test_set_comprehension (self ):
128+ self .check_roundtrip ("{x for x in range(5)}" )
129+
130+ def test_dict_comprehension (self ):
131+ self .check_roundtrip ("{x: x*x for x in range(10)}" )
132+
133+ def test_class_decorators (self ):
134+ self .check_roundtrip (class_decorator )
60135
61136def test_main ():
62137 test .support .run_unittest (UnparseTestCase )
0 commit comments