File tree Expand file tree Collapse file tree 2 files changed +20
-2
lines changed Expand file tree Collapse file tree 2 files changed +20
-2
lines changed Original file line number Diff line number Diff line change @@ -1356,10 +1356,20 @@ def visit_Call(self, node):
1356
1356
self .traverse (e )
1357
1357
1358
1358
def visit_Subscript (self , node ):
1359
+ def is_simple_tuple (slice_value ):
1360
+ # when unparsing a non-empty tuple, the parantheses can be safely
1361
+ # omitted if there aren't any elements that explicitly requires
1362
+ # parantheses (such as starred expressions).
1363
+ return (
1364
+ isinstance (slice_value , Tuple )
1365
+ and slice_value .elts
1366
+ and not any (isinstance (elt , Starred ) for elt in slice_value .elts )
1367
+ )
1368
+
1359
1369
self .set_precedence (_Precedence .ATOM , node .value )
1360
1370
self .traverse (node .value )
1361
1371
with self .delimit ("[" , "]" ):
1362
- if isinstance (node .slice , Tuple ) and node . slice . elts :
1372
+ if is_simple_tuple (node .slice ) :
1363
1373
self .items_view (self .traverse , node .slice .elts )
1364
1374
else :
1365
1375
self .traverse (node .slice )
Original file line number Diff line number Diff line change @@ -279,10 +279,13 @@ def test_dict_unpacking_in_dict(self):
279
279
self .check_ast_roundtrip (r"""{**{'y': 2}, 'x': 1}""" )
280
280
self .check_ast_roundtrip (r"""{**{'y': 2}, **{'x': 1}}""" )
281
281
282
- def test_ext_slices (self ):
282
+ def test_slices (self ):
283
283
self .check_ast_roundtrip ("a[i]" )
284
284
self .check_ast_roundtrip ("a[i,]" )
285
285
self .check_ast_roundtrip ("a[i, j]" )
286
+ self .check_ast_roundtrip ("a[(*a,)]" )
287
+ self .check_ast_roundtrip ("a[(a:=b)]" )
288
+ self .check_ast_roundtrip ("a[(a:=b,c)]" )
286
289
self .check_ast_roundtrip ("a[()]" )
287
290
self .check_ast_roundtrip ("a[i:j]" )
288
291
self .check_ast_roundtrip ("a[:j]" )
@@ -470,6 +473,11 @@ def test_unary_op_factor(self):
470
473
for prefix in ("not" ,):
471
474
self .check_src_roundtrip (f"{ prefix } 1" )
472
475
476
+ def test_slices (self ):
477
+ self .check_src_roundtrip ("a[1]" )
478
+ self .check_src_roundtrip ("a[1, 2]" )
479
+ self .check_src_roundtrip ("a[(1, *a)]" )
480
+
473
481
class DirectoryTestCase (ASTTestCase ):
474
482
"""Test roundtrip behaviour on all files in Lib and Lib/test."""
475
483
You can’t perform that action at this time.
0 commit comments