Skip to content

Commit

Permalink
test.py: Adds a lambda test
Browse files Browse the repository at this point in the history
We don't special-case Lambda statements, but they work anyways. This
test puts guardrails on the behaviour and adds an explanation.
  • Loading branch information
Graeme Smecher committed Aug 22, 2024
1 parent a37f1be commit 862c939
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,23 @@ def doctest_exception_inside_async_function():
>>> with pytest.raises(ZeroDivisionError):
... hello()
"""


def doctest_lambda():
"""
Lambdas aren't special-cased, but work anyways. It's subtle and worth
understanding why (and testing!)
>>> import asyncio
>>> %reload_ext awaitless
>>> x = lambda: asyncio.sleep(0)
>>> x()
Out[1]: <Task finished ... result=None>
...wait, what? OK - this worked. That's because the lambda doesn't contain
Expr or Statement nodes, and therefore doesn't trigger AST modification
that would insert an illegal await.
The actual conversion from coroutine to task occurred in the last line,
which was an Expr.
"""

0 comments on commit 862c939

Please sign in to comment.