From b393698c7ff13361c2545e9c142b64f5f571f7a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Mon, 12 Mar 2018 19:51:27 -0700 Subject: [PATCH 1/4] lib2to3: support trailing comma after **kwargs 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). --- Lib/lib2to3/Grammar.txt | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/Lib/lib2to3/Grammar.txt b/Lib/lib2to3/Grammar.txt index 0bdfcafcf3cabb6..701d0ea38525eb9 100644 --- a/Lib/lib2to3/Grammar.txt +++ b/Lib/lib2to3/Grammar.txt @@ -1,26 +1,7 @@ # Grammar for 2to3. This grammar supports Python 2.x and 3.x. -# Note: Changing the grammar specified in this file will most likely -# require corresponding changes in the parser module -# (../Modules/parsermodule.c). If you can't make the changes to -# that module yourself, please co-ordinate the required changes -# with someone who can; ask around on python-dev for help. Fred -# Drake will probably be listening there. - -# NOTE WELL: You should also follow all the steps listed in PEP 306, -# "How to Change Python's Grammar" - -# Commands for Kees Blom's railroad program -#diagram:token NAME -#diagram:token NUMBER -#diagram:token STRING -#diagram:token NEWLINE -#diagram:token ENDMARKER -#diagram:token INDENT -#diagram:output\input python.bla -#diagram:token DEDENT -#diagram:output\textwidth 20.04cm\oddsidemargin 0.0cm\evensidemargin 0.0cm -#diagram:rules +# NOTE WELL: You should also follow all the steps listed at +# https://devguide.python.org/grammar/ # Start symbols for the grammar: # file_input is a module or sequence of commands read from an input file; @@ -38,13 +19,13 @@ async_funcdef: 'async' funcdef funcdef: 'def' NAME parameters ['->' test] ':' suite parameters: '(' [typedargslist] ')' typedargslist: ((tfpdef ['=' test] ',')* - ('*' [tname] (',' tname ['=' test])* [',' '**' tname] | '**' tname) + ('*' [tname] (',' tname ['=' test])* [',' '**' tname [',']] | '**' tname [',']) | tfpdef ['=' test] (',' tfpdef ['=' test])* [',']) tname: NAME [':' test] tfpdef: tname | '(' tfplist ')' tfplist: tfpdef (',' tfpdef)* [','] varargslist: ((vfpdef ['=' test] ',')* - ('*' [vname] (',' vname ['=' test])* [',' '**' vname] | '**' vname) + ('*' [vname] (',' vname ['=' test])* [',' '**' vname [',']] | '**' vname [',']) | vfpdef ['=' test] (',' vfpdef ['=' test])* [',']) vname: NAME vfpdef: vname | '(' vfplist ')' From 59fefbee0781ba10a4e4c03ba7128a3776ae26ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Mon, 12 Mar 2018 19:58:34 -0700 Subject: [PATCH 2/4] Update NEWS --- .../next/Library/2018-03-12-19-58-25.bpo-33064.LO2KIY.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2018-03-12-19-58-25.bpo-33064.LO2KIY.rst diff --git a/Misc/NEWS.d/next/Library/2018-03-12-19-58-25.bpo-33064.LO2KIY.rst b/Misc/NEWS.d/next/Library/2018-03-12-19-58-25.bpo-33064.LO2KIY.rst new file mode 100644 index 000000000000000..b008fa778a48558 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-03-12-19-58-25.bpo-33064.LO2KIY.rst @@ -0,0 +1,2 @@ +lib2to3 now properly supports trailing commas after ``**kwargs`` in function +signatures. From cb4fc745f232bb23717fbb4776ac5790dc29adfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Tue, 13 Mar 2018 00:06:12 -0700 Subject: [PATCH 3/4] Update tests --- Lib/lib2to3/tests/test_parser.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Lib/lib2to3/tests/test_parser.py b/Lib/lib2to3/tests/test_parser.py index 0cbba26bec042e0..593f923359d913c 100644 --- a/Lib/lib2to3/tests/test_parser.py +++ b/Lib/lib2to3/tests/test_parser.py @@ -9,7 +9,6 @@ # Testing imports from . import support from .support import driver, driver_no_print_statement -from test.support import verbose # Python imports import difflib @@ -22,7 +21,6 @@ import sys import tempfile import unittest -import warnings # Local imports from lib2to3.pgen2 import driver as pgen2_driver @@ -305,6 +303,17 @@ def test_8(self): *g:6, h:7, i=8, j:9=10, **k:11) -> 12: pass""" self.validate(s) + def test_9(self): + s = """def f( + a: str, + b: int, + *, + c: bool = False, + **kwargs, + ) -> None: + call(c=c, **kwargs,)""" + self.validate(s) + # Adapted from Python 3's Lib/test/test_grammar.py:GrammarTests.test_var_annot class TestVarAnnotations(GrammarTest): @@ -407,7 +416,7 @@ def test_new_syntax(self): self.validate("class B(t, *args): pass") self.validate("class B(t, **kwargs): pass") self.validate("class B(t, *args, **kwargs): pass") - self.validate("class B(t, y=9, *args, **kwargs): pass") + self.validate("class B(t, y=9, *args, **kwargs,): pass") class TestParserIdempotency(support.TestCase): From 072da3ab6b0054dbf8d735709259971b5eb2a7e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Tue, 13 Mar 2018 00:26:50 -0700 Subject: [PATCH 4/4] Handle *args, too --- Lib/lib2to3/Grammar.txt | 4 ++-- Lib/lib2to3/tests/test_parser.py | 21 +++++++++++++++++++ .../2018-03-12-19-58-25.bpo-33064.LO2KIY.rst | 4 ++-- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/Lib/lib2to3/Grammar.txt b/Lib/lib2to3/Grammar.txt index 701d0ea38525eb9..b19b4a21fadd721 100644 --- a/Lib/lib2to3/Grammar.txt +++ b/Lib/lib2to3/Grammar.txt @@ -19,13 +19,13 @@ async_funcdef: 'async' funcdef funcdef: 'def' NAME parameters ['->' test] ':' suite parameters: '(' [typedargslist] ')' typedargslist: ((tfpdef ['=' test] ',')* - ('*' [tname] (',' tname ['=' test])* [',' '**' tname [',']] | '**' tname [',']) + ('*' [tname] (',' tname ['=' test])* [',' ['**' tname [',']]] | '**' tname [',']) | tfpdef ['=' test] (',' tfpdef ['=' test])* [',']) tname: NAME [':' test] tfpdef: tname | '(' tfplist ')' tfplist: tfpdef (',' tfpdef)* [','] varargslist: ((vfpdef ['=' test] ',')* - ('*' [vname] (',' vname ['=' test])* [',' '**' vname [',']] | '**' vname [',']) + ('*' [vname] (',' vname ['=' test])* [',' ['**' vname [',']]] | '**' vname [',']) | vfpdef ['=' test] (',' vfpdef ['=' test])* [',']) vname: NAME vfpdef: vname | '(' vfplist ')' diff --git a/Lib/lib2to3/tests/test_parser.py b/Lib/lib2to3/tests/test_parser.py index 593f923359d913c..cec2f98e9665878 100644 --- a/Lib/lib2to3/tests/test_parser.py +++ b/Lib/lib2to3/tests/test_parser.py @@ -314,6 +314,27 @@ def test_9(self): call(c=c, **kwargs,)""" self.validate(s) + def test_10(self): + s = """def f( + a: str, + ) -> None: + call(a,)""" + self.validate(s) + + def test_11(self): + s = """def f( + a: str = '', + ) -> None: + call(a=a,)""" + self.validate(s) + + def test_12(self): + s = """def f( + *args: str, + ) -> None: + call(*args,)""" + self.validate(s) + # Adapted from Python 3's Lib/test/test_grammar.py:GrammarTests.test_var_annot class TestVarAnnotations(GrammarTest): diff --git a/Misc/NEWS.d/next/Library/2018-03-12-19-58-25.bpo-33064.LO2KIY.rst b/Misc/NEWS.d/next/Library/2018-03-12-19-58-25.bpo-33064.LO2KIY.rst index b008fa778a48558..c8e955e335cb76f 100644 --- a/Misc/NEWS.d/next/Library/2018-03-12-19-58-25.bpo-33064.LO2KIY.rst +++ b/Misc/NEWS.d/next/Library/2018-03-12-19-58-25.bpo-33064.LO2KIY.rst @@ -1,2 +1,2 @@ -lib2to3 now properly supports trailing commas after ``**kwargs`` in function -signatures. +lib2to3 now properly supports trailing commas after ``*args`` and +``**kwargs`` in function signatures.