Skip to content

Commit b5e3ea2

Browse files
authored
gh-94996: Disallow parsing pos only params with feature_version < (3, 8) (GH-94997)
1 parent d2373fc commit b5e3ea2

File tree

5 files changed

+15
-6
lines changed

5 files changed

+15
-6
lines changed

Grammar/python.gram

+2-2
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,9 @@ params[arguments_ty]:
287287

288288
parameters[arguments_ty]:
289289
| a=slash_no_default b[asdl_arg_seq*]=param_no_default* c=param_with_default* d=[star_etc] {
290-
_PyPegen_make_arguments(p, a, NULL, b, c, d) }
290+
CHECK_VERSION(arguments_ty, 8, "Positional-only parameters are", _PyPegen_make_arguments(p, a, NULL, b, c, d)) }
291291
| a=slash_with_default b=param_with_default* c=[star_etc] {
292-
_PyPegen_make_arguments(p, NULL, a, NULL, b, c) }
292+
CHECK_VERSION(arguments_ty, 8, "Positional-only parameters are", _PyPegen_make_arguments(p, NULL, a, NULL, b, c)) }
293293
| a[asdl_arg_seq*]=param_no_default+ b=param_with_default* c=[star_etc] {
294294
_PyPegen_make_arguments(p, NULL, NULL, a, b, c) }
295295
| a=param_with_default+ b=[star_etc] { _PyPegen_make_arguments(p, NULL, NULL, NULL, a, b)}

Lib/test/test_ast.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,14 @@ def test_ast_asdl_signature(self):
738738
expressions[0] = f"expr = {ast.expr.__subclasses__()[0].__doc__}"
739739
self.assertCountEqual(ast.expr.__doc__.split("\n"), expressions)
740740

741+
def test_positional_only_feature_version(self):
742+
ast.parse('def foo(x, /): ...', feature_version=(3, 8))
743+
ast.parse('def bar(x=1, /): ...', feature_version=(3, 8))
744+
with self.assertRaises(SyntaxError):
745+
ast.parse('def foo(x, /): ...', feature_version=(3, 7))
746+
with self.assertRaises(SyntaxError):
747+
ast.parse('def bar(x=1, /): ...', feature_version=(3, 7))
748+
741749
def test_parenthesized_with_feature_version(self):
742750
ast.parse('with (CtxManager() as example): ...', feature_version=(3, 10))
743751
# While advertised as a feature in Python 3.10, this was allowed starting 3.9
@@ -746,7 +754,7 @@ def test_parenthesized_with_feature_version(self):
746754
ast.parse('with (CtxManager() as example): ...', feature_version=(3, 8))
747755
ast.parse('with CtxManager() as example: ...', feature_version=(3, 8))
748756

749-
def test_issue40614_feature_version(self):
757+
def test_debug_f_string_feature_version(self):
750758
ast.parse('f"{x=}"', feature_version=(3, 8))
751759
with self.assertRaises(SyntaxError):
752760
ast.parse('f"{x=}"', feature_version=(3, 7))

Lib/test/test_type_comments.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ def test_ignores(self):
322322
self.assertEqual(tree.type_ignores, [])
323323

324324
def test_longargs(self):
325-
for tree in self.parse_all(longargs):
325+
for tree in self.parse_all(longargs, minver=8):
326326
for t in tree.body:
327327
# The expected args are encoded in the function name
328328
todo = set(t.name[1:])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:func:`ast.parse` will no longer parse function definitions with positional-only params when passed ``feature_version`` less than ``(3, 8)``. Patch by Shantanu Jain.

Parser/parser.c

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

0 commit comments

Comments
 (0)