Skip to content

Commit 75a4ece

Browse files
committed
make lib2to3 parse async generators everywhere
1 parent 650ba4f commit 75a4ece

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
@@ -513,13 +513,14 @@ def generate_tokens(readline):
513513
stashed = tok
514514
continue
515515

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

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

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

Lib/lib2to3/tests/test_parser.py

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

199-
def test_async_with(self):
199+
def test_async_for(self):
200200
self.validate("""async def foo():
201201
async for a in b: pass""")
202202

203-
self.invalid_syntax("""def foo():
204-
async for a in b: pass""")
205-
206-
def test_async_for(self):
203+
def test_async_with(self):
207204
self.validate("""async def foo():
208205
async with a: pass""")
209206

210207
self.invalid_syntax("""def foo():
211208
async with a: pass""")
212209

210+
def test_async_generator(self):
211+
self.validate(
212+
"""async def foo():
213+
return (i * 2 async for i in arange(42))"""
214+
)
215+
self.validate(
216+
"""def foo():
217+
return (i * 2 async for i in arange(42))"""
218+
)
219+
213220

214221
class TestRaiseChanges(GrammarTest):
215222
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)