Skip to content

bpo-33349: make lib2to3 parse async generators everywhere #6588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Lib/lib2to3/pgen2/tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,13 +512,14 @@ def generate_tokens(readline):
stashed = tok
continue

if token == 'def':
if token in ('def', 'for'):
if (stashed
and stashed[0] == NAME
and stashed[1] == 'async'):

async_def = True
async_def_indent = indents[-1]
if token == 'def':
async_def = True
async_def_indent = indents[-1]

yield (ASYNC, stashed[1],
stashed[2], stashed[3],
Expand Down
17 changes: 12 additions & 5 deletions Lib/lib2to3/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,20 +200,27 @@ def test_async_var(self):
self.validate("""await = 1""")
self.validate("""def async(): pass""")

def test_async_with(self):
def test_async_for(self):
self.validate("""async def foo():
async for a in b: pass""")

self.invalid_syntax("""def foo():
async for a in b: pass""")

def test_async_for(self):
def test_async_with(self):
self.validate("""async def foo():
async with a: pass""")

self.invalid_syntax("""def foo():
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):
def test_2x_style_1(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib2to3 now recognizes async generators everywhere.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the news entry needs more details -- it's currently rather vague.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about "lib2to3 now recognizes async generators in non-async contexts"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no opposition to the current version though the one you proposed also OK.