Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e10e7c7

Browse filesBrowse files
authored
bpo-40334: Spacialized error message for invalid args after bare '*' (GH-19865)
When parsing things like `def f(*): pass` the old parser used to output `SyntaxError: named arguments must follow bare *`, which the new parser wasn't able to do.
1 parent c3f0014 commit e10e7c7
Copy full SHA for e10e7c7

File tree

Expand file treeCollapse file tree

5 files changed

+472
-210
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+472
-210
lines changed

‎Grammar/python.gram

Copy file name to clipboardExpand all lines: Grammar/python.gram
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ star_etc[StarEtc*]:
249249
| '*' ',' b=param_maybe_default+ c=[kwds] {
250250
_PyPegen_star_etc(p, NULL, b, c) }
251251
| a=kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
252+
| invalid_star_etc
252253

253254
kwds[arg_ty]: '**' a=param_no_default { a }
254255

@@ -356,6 +357,7 @@ lambda_star_etc[StarEtc*]:
356357
| '*' ',' b=lambda_param_maybe_default+ c=[lambda_kwds] {
357358
_PyPegen_star_etc(p, NULL, b, c) }
358359
| a=lambda_kwds { _PyPegen_star_etc(p, NULL, NULL, a) }
360+
| invalid_lambda_star_etc
359361

360362
lambda_kwds[arg_ty]: '**' a=lambda_param_no_default { a }
361363

@@ -636,6 +638,10 @@ invalid_comprehension:
636638
invalid_parameters:
637639
| param_no_default* (slash_with_default | param_with_default+) param_no_default {
638640
RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
641+
invalid_star_etc:
642+
| '*' (')' | ',' (')' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
643+
invalid_lambda_star_etc:
644+
| '*' (':' | ',' (':' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
639645
invalid_double_type_comments:
640646
| TYPE_COMMENT NEWLINE TYPE_COMMENT NEWLINE INDENT {
641647
RAISE_SYNTAX_ERROR("Cannot have two type comments on def") }

‎Lib/test/test_exceptions.py

Copy file name to clipboardExpand all lines: Lib/test/test_exceptions.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,11 @@ def baz():
242242
check('from __future__ import doesnt_exist', 1, 1)
243243
check('from __future__ import braces', 1, 1)
244244
check('x=1\nfrom __future__ import division', 2, 1)
245+
check('def f(*):\n pass', 1, 7 if support.use_old_parser() else 8)
245246

246247
@support.skip_if_new_parser("Pegen column offsets might be different")
247248
def testSyntaxErrorOffsetCustom(self):
248249
self.check('for 1 in []: pass', 1, 5)
249-
self.check('def f(*):\n pass', 1, 7)
250250
self.check('[*x for x in xs]', 1, 2)
251251
self.check('def f():\n x, y: int', 2, 3)
252252
self.check('(yield i) = 2', 1, 1)

‎Lib/test/test_peg_parser.py

Copy file name to clipboardExpand all lines: Lib/test/test_peg_parser.py
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,12 @@ def f():
603603
("1 += 1", "cannot assign to literal"),
604604
("pass\n pass", "unexpected indent"),
605605
("def f():\npass", "expected an indented block"),
606+
("def f(*): pass", "named arguments must follow bare *"),
607+
("def f(*,): pass", "named arguments must follow bare *"),
608+
("def f(*, **a): pass", "named arguments must follow bare *"),
609+
("lambda *: pass", "named arguments must follow bare *"),
610+
("lambda *,: pass", "named arguments must follow bare *"),
611+
("lambda *, **a: pass", "named arguments must follow bare *"),
606612
]
607613

608614
GOOD_BUT_FAIL_TEST_CASES = [

‎Lib/test/test_syntax.py

Copy file name to clipboardExpand all lines: Lib/test/test_syntax.py
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -627,9 +627,9 @@
627627
Traceback (most recent call last):
628628
SyntaxError: cannot assign to __debug__
629629
630-
# >>> with (lambda *:0): pass
631-
# Traceback (most recent call last):
632-
# SyntaxError: named arguments must follow bare *
630+
>>> with (lambda *:0): pass
631+
Traceback (most recent call last):
632+
SyntaxError: named arguments must follow bare *
633633
634634
Corner-cases that used to crash:
635635

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.