make lib2to3 parse async generators everywhere (GH-6588)

This commit is contained in:
Zsolt Dollenstein 2021-08-10 10:31:32 +01:00 committed by GitHub
parent 8ed1833912
commit 149addd496
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 8 deletions

View file

@ -512,13 +512,14 @@ def generate_tokens(readline):
stashed = tok stashed = tok
continue continue
if token == 'def': if token in ('def', 'for'):
if (stashed if (stashed
and stashed[0] == NAME and stashed[0] == NAME
and stashed[1] == 'async'): and stashed[1] == 'async'):
async_def = True if token == 'def':
async_def_indent = indents[-1] async_def = True
async_def_indent = indents[-1]
yield (ASYNC, stashed[1], yield (ASYNC, stashed[1],
stashed[2], stashed[3], stashed[2], stashed[3],

View file

@ -200,20 +200,27 @@ class TestAsyncAwait(GrammarTest):
self.validate("""await = 1""") self.validate("""await = 1""")
self.validate("""def async(): pass""") self.validate("""def async(): pass""")
def test_async_with(self): def test_async_for(self):
self.validate("""async def foo(): self.validate("""async def foo():
async for a in b: pass""") async for a in b: pass""")
self.invalid_syntax("""def foo(): def test_async_with(self):
async for a in b: pass""")
def test_async_for(self):
self.validate("""async def foo(): self.validate("""async def foo():
async with a: pass""") async with a: pass""")
self.invalid_syntax("""def foo(): self.invalid_syntax("""def foo():
async with a: pass""") async with a: pass""")
def test_async_generator(self):
self.validate(
"""async def foo():
return (i * 2 async for i in arange(42))"""
)
self.validate(
"""def foo():
return (i * 2 async for i in arange(42))"""
)
class TestRaiseChanges(GrammarTest): class TestRaiseChanges(GrammarTest):
def test_2x_style_1(self): def test_2x_style_1(self):

View file

@ -0,0 +1 @@
lib2to3 now recognizes async generators everywhere.