Skip to content

Commit ae0be5a

Browse files
gh-94947: Disallow parsing walrus with feature_version < (3, 8) (#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>
1 parent 97b4121 commit ae0be5a

File tree

4 files changed

+10
-2
lines changed

4 files changed

+10
-2
lines changed

β€ŽGrammar/python.gram

+3-1
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

+5
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()))
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

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
Β (0)