Skip to content

Commit 4d45397

Browse files
zsolambv
authored andcommitted
make lib2to3 parse async generators everywhere
1 parent 8ed1833 commit 4d45397

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

Lib/lib2to3/pgen2/tokenize.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -512,13 +512,14 @@ def generate_tokens(readline):
512512
stashed = tok
513513
continue
514514

515-
if token == 'def':
515+
if token in ('def', 'for'):
516516
if (stashed
517517
and stashed[0] == NAME
518518
and stashed[1] == 'async'):
519519

520-
async_def = True
521-
async_def_indent = indents[-1]
520+
if token == 'def':
521+
async_def = True
522+
async_def_indent = indents[-1]
522523

523524
yield (ASYNC, stashed[1],
524525
stashed[2], stashed[3],

Lib/lib2to3/tests/test_parser.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,20 +200,27 @@ def test_async_var(self):
200200
self.validate("""await = 1""")
201201
self.validate("""def async(): pass""")
202202

203-
def test_async_with(self):
203+
def test_async_for(self):
204204
self.validate("""async def foo():
205205
async for a in b: pass""")
206206

207-
self.invalid_syntax("""def foo():
208-
async for a in b: pass""")
209-
210-
def test_async_for(self):
207+
def test_async_with(self):
211208
self.validate("""async def foo():
212209
async with a: pass""")
213210

214211
self.invalid_syntax("""def foo():
215212
async with a: pass""")
216213

214+
def test_async_generator(self):
215+
self.validate(
216+
"""async def foo():
217+
return (i * 2 async for i in arange(42))"""
218+
)
219+
self.validate(
220+
"""def foo():
221+
return (i * 2 async for i in arange(42))"""
222+
)
223+
217224

218225
class TestRaiseChanges(GrammarTest):
219226
def test_2x_style_1(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lib2to3 now recognizes async generators everywhere.

0 commit comments

Comments
 (0)