diff options
author | Ivan Levkivskyi <levkivskyi@gmail.com> | 2019-01-25 01:39:19 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-25 01:39:19 (GMT) |
commit | 62c35a8a8ff5854ed470b1c16a7a14f3bb80368c (patch) | |
tree | c9fbcd76316803a7d1272301a64a3c3ea233d2bd | |
parent | 1396d8fab4d0ae830d45f4937322bbb43ce0c30e (diff) | |
download | cpython-62c35a8a8ff5854ed470b1c16a7a14f3bb80368c.zip cpython-62c35a8a8ff5854ed470b1c16a7a14f3bb80368c.tar.gz cpython-62c35a8a8ff5854ed470b1c16a7a14f3bb80368c.tar.bz2 |
bpo-35814: Allow same r.h.s. in annotated assignments as in normal ones (GH-11667)
-rw-r--r-- | Grammar/Grammar | 2 | ||||
-rw-r--r-- | Lib/test/test_grammar.py | 9 | ||||
-rw-r--r-- | Lib/test/test_parser.py | 2 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Core and Builtins/2019-01-24-13-25-21.bpo-35814.r_MjA6.rst | 2 | ||||
-rw-r--r-- | Python/ast.c | 7 | ||||
-rw-r--r-- | Python/graminit.c | 7 |
6 files changed, 23 insertions, 6 deletions
diff --git a/Grammar/Grammar b/Grammar/Grammar index f21fa11..8455c12 100644 --- a/Grammar/Grammar +++ b/Grammar/Grammar @@ -40,7 +40,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))*) -annassign: ':' test ['=' test] +annassign: ':' test ['=' (yield_expr|testlist)] 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 3ed19ff..74590eb 100644 --- a/Lib/test/test_grammar.py +++ b/Lib/test/test_grammar.py @@ -445,6 +445,15 @@ class GrammarTests(unittest.TestCase): exec('X: str', {}, CNS2()) self.assertEqual(nonloc_ns['__annotations__']['x'], str) + def test_var_annot_rhs(self): + ns = {} + exec('x: tuple = 1, 2', ns) + self.assertEqual(ns['x'], (1, 2)) + stmt = ('def f():\n' + ' x: int = yield') + exec(stmt, ns) + self.assertEqual(list(ns['f']()), [None]) + def test_funcdef(self): ### [decorators] 'def' NAME parameters ['->' test] ':' suite ### decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE diff --git a/Lib/test/test_parser.py b/Lib/test/test_parser.py index ac3899b..19f1782 100644 --- a/Lib/test/test_parser.py +++ b/Lib/test/test_parser.py @@ -166,7 +166,7 @@ class RoundtripLegalSyntaxTestCase(unittest.TestCase): with self.assertRaises(SyntaxError): exec("x, *y, z: int = range(5)", {}, {}) with self.assertRaises(SyntaxError): - exec("t: tuple = 1, 2", {}, {}) + exec("x: int = 1, y = 2", {}, {}) with self.assertRaises(SyntaxError): exec("u = v: int", {}, {}) with self.assertRaises(SyntaxError): diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-01-24-13-25-21.bpo-35814.r_MjA6.rst b/Misc/NEWS.d/next/Core and Builtins/2019-01-24-13-25-21.bpo-35814.r_MjA6.rst new file mode 100644 index 0000000..5d216b2 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2019-01-24-13-25-21.bpo-35814.r_MjA6.rst @@ -0,0 +1,2 @@ +Allow same right hand side expressions in annotated assignments as in normal ones.
+In particular, ``x: Tuple[int, int] = 1, 2`` (without parentheses on the right) is now allowed.
\ No newline at end of file diff --git a/Python/ast.c b/Python/ast.c index 6560026..e10e63f 100644 --- a/Python/ast.c +++ b/Python/ast.c @@ -3163,7 +3163,12 @@ ast_for_expr_stmt(struct compiling *c, const node *n) } else { ch = CHILD(ann, 3); - expr3 = ast_for_expr(c, ch); + if (TYPE(ch) == testlist) { + expr3 = ast_for_testlist(c, ch); + } + else { + expr3 = ast_for_expr(c, ch); + } if (!expr3) { return NULL; } diff --git a/Python/graminit.c b/Python/graminit.c index 91092f1..225d327 100644 --- a/Python/graminit.c +++ b/Python/graminit.c @@ -498,8 +498,9 @@ static arc arcs_17_2[2] = { {31, 3}, {0, 2}, }; -static arc arcs_17_3[1] = { - {26, 4}, +static arc arcs_17_3[2] = { + {50, 4}, + {9, 4}, }; static arc arcs_17_4[1] = { {0, 4}, @@ -508,7 +509,7 @@ static state states_17[5] = { {1, arcs_17_0}, {1, arcs_17_1}, {2, arcs_17_2}, - {1, arcs_17_3}, + {2, arcs_17_3}, {1, arcs_17_4}, }; static arc arcs_18_0[2] = { |