File tree 2 files changed +43
-1
lines changed
2 files changed +43
-1
lines changed Original file line number Diff line number Diff line change @@ -875,6 +875,7 @@ def parse_for_stmt(self) -> ForStmt:
875
875
def parse_for_index_variables (self ) -> Node :
876
876
# Parse index variables of a 'for' statement.
877
877
index_items = List [Node ]()
878
+ force_tuple = False
878
879
879
880
while True :
880
881
v = self .parse_expression (precedence ['in' ],
@@ -883,8 +884,11 @@ def parse_for_index_variables(self) -> Node:
883
884
if self .current_str () != ',' :
884
885
break
885
886
self .skip ()
887
+ if self .current_str () == 'in' :
888
+ force_tuple = True
889
+ break
886
890
887
- if len (index_items ) == 1 :
891
+ if len (index_items ) == 1 and not force_tuple :
888
892
index = index_items [0 ]
889
893
else :
890
894
index = TupleExpr (index_items )
Original file line number Diff line number Diff line change @@ -1503,6 +1503,44 @@ MypyFile:1(
1503
1503
Block:1(
1504
1504
PassStmt:2())))
1505
1505
1506
+ [case testForAndTrailingCommaAfterIndexVar]
1507
+ for i, in x:
1508
+ pass
1509
+ [out]
1510
+ MypyFile:1(
1511
+ ForStmt:1(
1512
+ TupleExpr:1(
1513
+ NameExpr(i))
1514
+ NameExpr(x)
1515
+ Block:1(
1516
+ PassStmt:2())))
1517
+
1518
+ [case testListComprehensionAndTrailingCommaAfterIndexVar]
1519
+ x = [a for b, in c]
1520
+ [out]
1521
+ MypyFile:1(
1522
+ AssignmentStmt:1(
1523
+ NameExpr(x)
1524
+ ListComprehension:1(
1525
+ GeneratorExpr:1(
1526
+ NameExpr(a)
1527
+ TupleExpr:1(
1528
+ NameExpr(b))
1529
+ NameExpr(c)))))
1530
+
1531
+ [case testForAndTrailingCommaAfterIndexVars]
1532
+ for i, j, in x:
1533
+ pass
1534
+ [out]
1535
+ MypyFile:1(
1536
+ ForStmt:1(
1537
+ TupleExpr:1(
1538
+ NameExpr(i)
1539
+ NameExpr(j))
1540
+ NameExpr(x)
1541
+ Block:1(
1542
+ PassStmt:2())))
1543
+
1506
1544
[case testGeneratorWithCondition]
1507
1545
x for y in z if 0
1508
1546
[out]
You can’t perform that action at this time.
0 commit comments