Skip to content

Commit 0e63776

Browse files
miss-islingtonzsol
andauthored
make lib2to3 parse async generators everywhere (GH-6588) (GH-27703)
(cherry picked from commit 149addd) Co-authored-by: Zsolt Dollenstein <[email protected]>
1 parent ede1dc4 commit 0e63776

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
@@ -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)