Skip to content

Commit b51f5de

Browse files
authored
bpo-33064: lib2to3: support trailing comma after *args and **kwargs (#6096)
New tests also added. I also made the comments in line with the builtin Grammar/Grammar. PEP 306 was withdrawn, Kees Blom's railroad program has been lost to the sands of time for at least 16 years now (I found a python-dev post from people looking for it).
1 parent a34510a commit b51f5de

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
lines changed

Lib/lib2to3/Grammar.txt

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,7 @@
11
# Grammar for 2to3. This grammar supports Python 2.x and 3.x.
22

3-
# Note: Changing the grammar specified in this file will most likely
4-
# require corresponding changes in the parser module
5-
# (../Modules/parsermodule.c). If you can't make the changes to
6-
# that module yourself, please co-ordinate the required changes
7-
# with someone who can; ask around on python-dev for help. Fred
8-
# Drake <[email protected]> will probably be listening there.
9-
10-
# NOTE WELL: You should also follow all the steps listed in PEP 306,
11-
# "How to Change Python's Grammar"
12-
13-
# Commands for Kees Blom's railroad program
14-
#diagram:token NAME
15-
#diagram:token NUMBER
16-
#diagram:token STRING
17-
#diagram:token NEWLINE
18-
#diagram:token ENDMARKER
19-
#diagram:token INDENT
20-
#diagram:output\input python.bla
21-
#diagram:token DEDENT
22-
#diagram:output\textwidth 20.04cm\oddsidemargin 0.0cm\evensidemargin 0.0cm
23-
#diagram:rules
3+
# NOTE WELL: You should also follow all the steps listed at
4+
# https://devguide.python.org/grammar/
245

256
# Start symbols for the grammar:
267
# file_input is a module or sequence of commands read from an input file;
@@ -38,13 +19,13 @@ async_funcdef: 'async' funcdef
3819
funcdef: 'def' NAME parameters ['->' test] ':' suite
3920
parameters: '(' [typedargslist] ')'
4021
typedargslist: ((tfpdef ['=' test] ',')*
41-
('*' [tname] (',' tname ['=' test])* [',' '**' tname] | '**' tname)
22+
('*' [tname] (',' tname ['=' test])* [',' ['**' tname [',']]] | '**' tname [','])
4223
| tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
4324
tname: NAME [':' test]
4425
tfpdef: tname | '(' tfplist ')'
4526
tfplist: tfpdef (',' tfpdef)* [',']
4627
varargslist: ((vfpdef ['=' test] ',')*
47-
('*' [vname] (',' vname ['=' test])* [',' '**' vname] | '**' vname)
28+
('*' [vname] (',' vname ['=' test])* [',' ['**' vname [',']]] | '**' vname [','])
4829
| vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
4930
vname: NAME
5031
vfpdef: vname | '(' vfplist ')'

Lib/lib2to3/tests/test_parser.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
# Testing imports
1010
from . import support
1111
from .support import driver, driver_no_print_statement
12-
from test.support import verbose
1312

1413
# Python imports
1514
import difflib
@@ -22,7 +21,6 @@
2221
import sys
2322
import tempfile
2423
import unittest
25-
import warnings
2624

2725
# Local imports
2826
from lib2to3.pgen2 import driver as pgen2_driver
@@ -305,6 +303,38 @@ def test_8(self):
305303
*g:6, h:7, i=8, j:9=10, **k:11) -> 12: pass"""
306304
self.validate(s)
307305

306+
def test_9(self):
307+
s = """def f(
308+
a: str,
309+
b: int,
310+
*,
311+
c: bool = False,
312+
**kwargs,
313+
) -> None:
314+
call(c=c, **kwargs,)"""
315+
self.validate(s)
316+
317+
def test_10(self):
318+
s = """def f(
319+
a: str,
320+
) -> None:
321+
call(a,)"""
322+
self.validate(s)
323+
324+
def test_11(self):
325+
s = """def f(
326+
a: str = '',
327+
) -> None:
328+
call(a=a,)"""
329+
self.validate(s)
330+
331+
def test_12(self):
332+
s = """def f(
333+
*args: str,
334+
) -> None:
335+
call(*args,)"""
336+
self.validate(s)
337+
308338

309339
# Adapted from Python 3's Lib/test/test_grammar.py:GrammarTests.test_var_annot
310340
class TestVarAnnotations(GrammarTest):
@@ -407,7 +437,7 @@ def test_new_syntax(self):
407437
self.validate("class B(t, *args): pass")
408438
self.validate("class B(t, **kwargs): pass")
409439
self.validate("class B(t, *args, **kwargs): pass")
410-
self.validate("class B(t, y=9, *args, **kwargs): pass")
440+
self.validate("class B(t, y=9, *args, **kwargs,): pass")
411441

412442

413443
class TestParserIdempotency(support.TestCase):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lib2to3 now properly supports trailing commas after ``*args`` and
2+
``**kwargs`` in function signatures.

0 commit comments

Comments
 (0)