Skip to content

Commit 7dc236d

Browse files
gh-94947: Disallow parsing walrus with feature_version < (3, 8) (GH-94948)
* gh-94947: Disallow parsing walrus with feature_version < (3, 8) * oops, commit the parser * 📜🤖 Added by blurb_it. Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> (cherry picked from commit ae0be5a) Co-authored-by: Shantanu <[email protected]>
1 parent 30412d9 commit 7dc236d

File tree

4 files changed

+10
-2
lines changed

4 files changed

+10
-2
lines changed

Grammar/python.gram

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,9 @@ star_named_expression[expr_ty]:
660660
| named_expression
661661

662662
assignment_expression[expr_ty]:
663-
| a=NAME ':=' ~ b=expression { _PyAST_NamedExpr(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, EXTRA) }
663+
| a=NAME ':=' ~ b=expression {
664+
CHECK_VERSION(expr_ty, 8, "Assignment expressions are",
665+
_PyAST_NamedExpr(CHECK(expr_ty, _PyPegen_set_expr_context(p, a, Store)), b, EXTRA)) }
664666

665667
named_expression[expr_ty]:
666668
| assignment_expression

Lib/test/test_ast.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,11 @@ def test_issue40614_feature_version(self):
743743
with self.assertRaises(SyntaxError):
744744
ast.parse('f"{x=}"', feature_version=(3, 7))
745745

746+
def test_assignment_expression_feature_version(self):
747+
ast.parse('(x := 0)', feature_version=(3, 8))
748+
with self.assertRaises(SyntaxError):
749+
ast.parse('(x := 0)', feature_version=(3, 7))
750+
746751
def test_constant_as_name(self):
747752
for constant in "True", "False", "None":
748753
expr = ast.Expression(ast.Name(constant, ast.Load()))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:func:`ast.parse` will no longer parse assignment expressions when passed ``feature_version`` less than ``(3, 8)``. Patch by Shantanu Jain.

Parser/parser.c

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)