Skip to content

Commit

Permalink
gh-94947: Disallow parsing walrus with feature_version < (3, 8) (GH-9…
Browse files Browse the repository at this point in the history
…4948)

* 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]>
  • Loading branch information
miss-islington and hauntsaninja authored Jul 18, 2022
1 parent 30412d9 commit 7dc236d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,9 @@ star_named_expression[expr_ty]:
| named_expression

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

named_expression[expr_ty]:
| assignment_expression
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,11 @@ def test_issue40614_feature_version(self):
with self.assertRaises(SyntaxError):
ast.parse('f"{x=}"', feature_version=(3, 7))

def test_assignment_expression_feature_version(self):
ast.parse('(x := 0)', feature_version=(3, 8))
with self.assertRaises(SyntaxError):
ast.parse('(x := 0)', feature_version=(3, 7))

def test_constant_as_name(self):
for constant in "True", "False", "None":
expr = ast.Expression(ast.Name(constant, ast.Load()))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:func:`ast.parse` will no longer parse assignment expressions when passed ``feature_version`` less than ``(3, 8)``. Patch by Shantanu Jain.
2 changes: 1 addition & 1 deletion Parser/parser.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7dc236d

Please sign in to comment.