Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 62c35a8

Browse files
authored
bpo-35814: Allow same r.h.s. in annotated assignments as in normal ones (GH-11667)
1 parent 1396d8f commit 62c35a8

6 files changed

Lines changed: 23 additions & 6 deletions

File tree

Grammar/Grammar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt |
4040
import_stmt | global_stmt | nonlocal_stmt | assert_stmt)
4141
expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
4242
('=' (yield_expr|testlist_star_expr))*)
43-
annassign: ':' test ['=' test]
43+
annassign: ':' test ['=' (yield_expr|testlist)]
4444
testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
4545
augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
4646
'<<=' | '>>=' | '**=' | '//=')

Lib/test/test_grammar.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,15 @@ def __getitem__(self, item):
445445
exec('X: str', {}, CNS2())
446446
self.assertEqual(nonloc_ns['__annotations__']['x'], str)
447447

448+
def test_var_annot_rhs(self):
449+
ns = {}
450+
exec('x: tuple = 1, 2', ns)
451+
self.assertEqual(ns['x'], (1, 2))
452+
stmt = ('def f():\n'
453+
' x: int = yield')
454+
exec(stmt, ns)
455+
self.assertEqual(list(ns['f']()), [None])
456+
448457
def test_funcdef(self):
449458
### [decorators] 'def' NAME parameters ['->' test] ':' suite
450459
### decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE

Lib/test/test_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def test_var_annot(self):
166166
with self.assertRaises(SyntaxError):
167167
exec("x, *y, z: int = range(5)", {}, {})
168168
with self.assertRaises(SyntaxError):
169-
exec("t: tuple = 1, 2", {}, {})
169+
exec("x: int = 1, y = 2", {}, {})
170170
with self.assertRaises(SyntaxError):
171171
exec("u = v: int", {}, {})
172172
with self.assertRaises(SyntaxError):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Allow same right hand side expressions in annotated assignments as in normal ones.
2+
In particular, ``x: Tuple[int, int] = 1, 2`` (without parentheses on the right) is now allowed.

Python/ast.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3163,7 +3163,12 @@ ast_for_expr_stmt(struct compiling *c, const node *n)
31633163
}
31643164
else {
31653165
ch = CHILD(ann, 3);
3166-
expr3 = ast_for_expr(c, ch);
3166+
if (TYPE(ch) == testlist) {
3167+
expr3 = ast_for_testlist(c, ch);
3168+
}
3169+
else {
3170+
expr3 = ast_for_expr(c, ch);
3171+
}
31673172
if (!expr3) {
31683173
return NULL;
31693174
}

Python/graminit.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,9 @@ static arc arcs_17_2[2] = {
498498
{31, 3},
499499
{0, 2},
500500
};
501-
static arc arcs_17_3[1] = {
502-
{26, 4},
501+
static arc arcs_17_3[2] = {
502+
{50, 4},
503+
{9, 4},
503504
};
504505
static arc arcs_17_4[1] = {
505506
{0, 4},
@@ -508,7 +509,7 @@ static state states_17[5] = {
508509
{1, arcs_17_0},
509510
{1, arcs_17_1},
510511
{2, arcs_17_2},
511-
{1, arcs_17_3},
512+
{2, arcs_17_3},
512513
{1, arcs_17_4},
513514
};
514515
static arc arcs_18_0[2] = {

0 commit comments

Comments
 (0)