Skip to content

Commit

Permalink
bpo-33348: parse expressions after * and ** in lib2to3 (pythonGH-6586)
Browse files Browse the repository at this point in the history
These are valid even in python 2.7


https://bugs.python.org/issue33348



Automerge-Triggered-By: @gpshead
  • Loading branch information
zsol authored and miss-islington committed Oct 24, 2019
1 parent 3bbb6db commit 96b06ae
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Lib/lib2to3/Grammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ arglist: argument (',' argument)* [',']
# that precede iterable unpackings are blocked; etc.
argument: ( test [comp_for] |
test '=' test |
'**' expr |
star_expr )
'**' test |
'*' test )

comp_iter: comp_for | comp_if
comp_for: [ASYNC] 'for' exprlist 'in' testlist_safe [comp_iter]
Expand Down
4 changes: 1 addition & 3 deletions Lib/lib2to3/fixes/fix_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ def transform(self, node, results):
# I feel like we should be able to express this logic in the
# PATTERN above but I don't know how to do it so...
if args:
if args.type == self.syms.star_expr:
return # Make no change.
if (args.type == self.syms.argument and
args.children[0].value == '**'):
args.children[0].value in {'**', '*'}):
return # Make no change.
if kwds and (kwds.type == self.syms.argument and
kwds.children[0].value == '**'):
Expand Down
4 changes: 1 addition & 3 deletions Lib/lib2to3/fixes/fix_intern.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ def transform(self, node, results):
# PATTERN above but I don't know how to do it so...
obj = results['obj']
if obj:
if obj.type == self.syms.star_expr:
return # Make no change.
if (obj.type == self.syms.argument and
obj.children[0].value == '**'):
obj.children[0].value in {'**', '*'}):
return # Make no change.
names = ('sys', 'intern')
new = ImportAndCall(node, results, names)
Expand Down
4 changes: 1 addition & 3 deletions Lib/lib2to3/fixes/fix_reload.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ def transform(self, node, results):
# PATTERN above but I don't know how to do it so...
obj = results['obj']
if obj:
if obj.type == self.syms.star_expr:
return # Make no change.
if (obj.type == self.syms.argument and
obj.children[0].value == '**'):
obj.children[0].value in {'**', '*'}):
return # Make no change.
names = ('importlib', 'reload')
new = ImportAndCall(node, results, names)
Expand Down
7 changes: 7 additions & 0 deletions Lib/lib2to3/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ def test_double_star_dict_literal(self):
def test_double_star_dict_literal_after_keywords(self):
self.validate("""func(spam='fried', **{'eggs':'scrambled'})""")

def test_double_star_expression(self):
self.validate("""func(**{'a':2} or {})""")
self.validate("""func(**() or {})""")

def test_star_expression(self):
self.validate("""func(*[] or [2])""")

def test_list_display(self):
self.validate("""[*{2}, 3, *[4]]""")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lib2to3 now recognizes expressions after ``*`` and `**` like in ``f(*[] or
[])``.

0 comments on commit 96b06ae

Please sign in to comment.