Skip to content

gh-133194: Add CHECK_VERSION to new PEP758 grammar #133195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -442,15 +442,23 @@ try_stmt[stmt_ty]:
except_block[excepthandler_ty]:
| invalid_except_stmt_indent
| 'except' e=expressions ':' b=block {
_PyAST_ExceptHandler(e, NULL, b, EXTRA) }
CHECK_VERSION(
excepthandler_ty,
14,
"except expressions without parentheses",
_PyAST_ExceptHandler(e, NULL, b, EXTRA)) }
| 'except' e=expression 'as' t=NAME ':' b=block {
_PyAST_ExceptHandler(e, ((expr_ty) t)->v.Name.id, b, EXTRA) }
| 'except' ':' b=block { _PyAST_ExceptHandler(NULL, NULL, b, EXTRA) }
| invalid_except_stmt
except_star_block[excepthandler_ty]:
| invalid_except_star_stmt_indent
| 'except' '*' e=expressions ':' b=block {
_PyAST_ExceptHandler(e, NULL, b, EXTRA) }
CHECK_VERSION(
excepthandler_ty,
14,
"except expressions without parentheses",
_PyAST_ExceptHandler(e, NULL, b, EXTRA)) }
| 'except' '*' e=expression 'as' t=NAME ':' b=block {
_PyAST_ExceptHandler(e, ((expr_ty) t)->v.Name.id, b, EXTRA) }
| invalid_except_star_stmt
Expand Down
22 changes: 22 additions & 0 deletions Lib/test/test_ast/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,28 @@ def test_assignment_expression_feature_version(self):
with self.assertRaises(SyntaxError):
ast.parse('(x := 0)', feature_version=(3, 7))

def test_pep758_except_without_parens(self):
code = textwrap.dedent("""
try:
...
except ValueError, TypeError:
...
""")
ast.parse(code, feature_version=(3, 14))
with self.assertRaises(SyntaxError):
ast.parse(code, feature_version=(3, 13))

def test_pep758_except_star_without_parens(self):
code = textwrap.dedent("""
try:
...
except* ValueError, TypeError:
...
""")
ast.parse(code, feature_version=(3, 14))
with self.assertRaises(SyntaxError):
ast.parse(code, feature_version=(3, 13))

def test_conditional_context_managers_parse_with_low_feature_version(self):
# regression test for gh-115881
ast.parse('with (x() if y else z()): ...', feature_version=(3, 8))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:func:`ast.parse` will no longer parse new :pep:`758` syntax with older
*feature_version* passed.
4 changes: 2 additions & 2 deletions Parser/parser.c

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

Loading