Skip to content

Commit d04661f

Browse files
miss-islingtonzsol
andauthored
bpo-33348: parse expressions after * and ** in lib2to3 (GH-6586)
These are valid even in python 2.7 https://bugs.python.org/issue33348 Automerge-Triggered-By: @gpshead (cherry picked from commit 96b06ae) Co-authored-by: Zsolt Dollenstein <[email protected]>
1 parent d8fc9c8 commit d04661f

File tree

6 files changed

+14
-11
lines changed

6 files changed

+14
-11
lines changed

Lib/lib2to3/Grammar.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ arglist: argument (',' argument)* [',']
138138
# that precede iterable unpackings are blocked; etc.
139139
argument: ( test [comp_for] |
140140
test '=' test |
141-
'**' expr |
142-
star_expr )
141+
'**' test |
142+
'*' test )
143143

144144
comp_iter: comp_for | comp_if
145145
comp_for: [ASYNC] 'for' exprlist 'in' testlist_safe [comp_iter]

Lib/lib2to3/fixes/fix_apply.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ def transform(self, node, results):
3737
# I feel like we should be able to express this logic in the
3838
# PATTERN above but I don't know how to do it so...
3939
if args:
40-
if args.type == self.syms.star_expr:
41-
return # Make no change.
4240
if (args.type == self.syms.argument and
43-
args.children[0].value == '**'):
41+
args.children[0].value in {'**', '*'}):
4442
return # Make no change.
4543
if kwds and (kwds.type == self.syms.argument and
4644
kwds.children[0].value == '**'):

Lib/lib2to3/fixes/fix_intern.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ def transform(self, node, results):
3030
# PATTERN above but I don't know how to do it so...
3131
obj = results['obj']
3232
if obj:
33-
if obj.type == self.syms.star_expr:
34-
return # Make no change.
3533
if (obj.type == self.syms.argument and
36-
obj.children[0].value == '**'):
34+
obj.children[0].value in {'**', '*'}):
3735
return # Make no change.
3836
names = ('sys', 'intern')
3937
new = ImportAndCall(node, results, names)

Lib/lib2to3/fixes/fix_reload.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ def transform(self, node, results):
2727
# PATTERN above but I don't know how to do it so...
2828
obj = results['obj']
2929
if obj:
30-
if obj.type == self.syms.star_expr:
31-
return # Make no change.
3230
if (obj.type == self.syms.argument and
33-
obj.children[0].value == '**'):
31+
obj.children[0].value in {'**', '*'}):
3432
return # Make no change.
3533
names = ('importlib', 'reload')
3634
new = ImportAndCall(node, results, names)

Lib/lib2to3/tests/test_parser.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,13 @@ def test_double_star_dict_literal(self):
253253
def test_double_star_dict_literal_after_keywords(self):
254254
self.validate("""func(spam='fried', **{'eggs':'scrambled'})""")
255255

256+
def test_double_star_expression(self):
257+
self.validate("""func(**{'a':2} or {})""")
258+
self.validate("""func(**() or {})""")
259+
260+
def test_star_expression(self):
261+
self.validate("""func(*[] or [2])""")
262+
256263
def test_list_display(self):
257264
self.validate("""[*{2}, 3, *[4]]""")
258265

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lib2to3 now recognizes expressions after ``*`` and `**` like in ``f(*[] or
2+
[])``.

0 commit comments

Comments
 (0)