[Python-checkins] bpo-35814: Allow unpacking in r.h.s of annotated assignment expressions (GH-13760)

Pablo Galindo webhook-mailer at python.org
Mon Jun 3 03:34:34 EDT 2019


https://github.com/python/cpython/commit/8565f6b6db0fa9f65449b532a5056a98bad3dc37
commit: 8565f6b6db0fa9f65449b532a5056a98bad3dc37
branch: master
author: Pablo Galindo <Pablogsal at gmail.com>
committer: GitHub <noreply at github.com>
date: 2019-06-03T08:34:20+01:00
summary:

bpo-35814: Allow unpacking in r.h.s of annotated assignment expressions (GH-13760)

files:
A Misc/NEWS.d/next/Core and Builtins/2019-06-03-00-51-02.bpo-35814.Cf7sGY.rst
M Grammar/Grammar
M Lib/test/test_grammar.py
M Python/ast.c
M Python/graminit.c

diff --git a/Grammar/Grammar b/Grammar/Grammar
index 0cacfb648e9a..21f7e1a89115 100644
--- a/Grammar/Grammar
+++ b/Grammar/Grammar
@@ -84,7 +84,7 @@ small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt |
              import_stmt | global_stmt | nonlocal_stmt | assert_stmt)
 expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
                      [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
-annassign: ':' test ['=' (yield_expr|testlist)]
+annassign: ':' test ['=' (yield_expr|testlist_star_expr)]
 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
             '<<=' | '>>=' | '**=' | '//=')
diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py
index 2a3b71fac4a5..78d94593c7f3 100644
--- a/Lib/test/test_grammar.py
+++ b/Lib/test/test_grammar.py
@@ -454,6 +454,10 @@ def test_var_annot_rhs(self):
         exec(stmt, ns)
         self.assertEqual(list(ns['f']()), [None])
 
+        ns = {"a": 1, 'b': (2, 3, 4), "c":5, "Tuple": typing.Tuple}
+        exec('x: Tuple[int, ...] = a,*b,c', ns)
+        self.assertEqual(ns['x'], (1, 2, 3, 4, 5))
+
     def test_funcdef(self):
         ### [decorators] 'def' NAME parameters ['->' test] ':' suite
         ### decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-06-03-00-51-02.bpo-35814.Cf7sGY.rst b/Misc/NEWS.d/next/Core and Builtins/2019-06-03-00-51-02.bpo-35814.Cf7sGY.rst
new file mode 100644
index 000000000000..2b9f00a6d9e7
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2019-06-03-00-51-02.bpo-35814.Cf7sGY.rst	
@@ -0,0 +1,2 @@
+Allow unpacking in the right hand side of annotated assignments. In
+particular, ``t: Tuple[int, ...] = x, y, *z`` is now allowed.
diff --git a/Python/ast.c b/Python/ast.c
index b77552274d24..df9242977e3f 100644
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -3398,7 +3398,7 @@ ast_for_expr_stmt(struct compiling *c, const node *n)
         }
         else {
             ch = CHILD(ann, 3);
-            if (TYPE(ch) == testlist) {
+            if (TYPE(ch) == testlist_star_expr) {
                 expr3 = ast_for_testlist(c, ch);
             }
             else {
diff --git a/Python/graminit.c b/Python/graminit.c
index 0587b1c0fc10..7c40ce933c1d 100644
--- a/Python/graminit.c
+++ b/Python/graminit.c
@@ -742,7 +742,7 @@ static const arc arcs_17_2[2] = {
     {0, 2},
 };
 static const arc arcs_17_3[2] = {
-    {47, 4},
+    {81, 4},
     {84, 4},
 };
 static const arc arcs_17_4[1] = {



More information about the Python-checkins mailing list