-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Implement async generators (PEP 525) #2711
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
Changes from 1 commit
c741a97
4dc62b6
c64e7fc
0117379
01b50de
feaf051
2273ac8
c255d84
facc5ed
f193e42
dc0206c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -410,25 +410,125 @@ def f() -> Generator[int, str, int]: | |
[builtins fixtures/async_await.pyi] | ||
[out] | ||
|
||
-- Async generators (PEP 525), some test cases adapted from the PEP text | ||
-- --------------------------------------------------------------------- | ||
|
||
[case testAsyncGenerator] | ||
# flags: --fast-parser --python-version 3.6 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
from mypy_extensions import AsyncGenerator | ||
|
||
async def f() -> int: | ||
return 42 | ||
|
||
async def g() -> AsyncGenerator[int, str]: | ||
async def g() -> AsyncGenerator[int, None]: | ||
value = await f() | ||
reveal_type(value) # E: Revealed type is 'builtins.int' | ||
x = yield value | ||
reveal_type(x) # E: Revealed type is 'builtins.str' | ||
reveal_type(value) # E: Revealed type is 'builtins.int*' | ||
yield value | ||
# return without a value is fine | ||
return | ||
reveal_type(g) # E: Revealed type is 'def () -> typing.AsyncGenerator[builtins.int, void]' | ||
reveal_type(g()) # E: Revealed type is 'typing.AsyncGenerator[builtins.int, void]' | ||
|
||
async def h() -> None: | ||
async for item in g(): | ||
reveal_type(item) # E: Revealed type is 'builtins.int' | ||
reveal_type(item) # E: Revealed type is 'builtins.int*' | ||
|
||
[builtins fixtures/dict.pyi] | ||
|
||
[case testAsyncGeneratorManualIter] | ||
# flags: --fast-parser --python-version 3.6 | ||
from mypy_extensions import AsyncGenerator | ||
|
||
async def genfunc() -> AsyncGenerator[int, None]: | ||
yield 1 | ||
yield 2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe also add a test where you |
||
|
||
async def user() -> None: | ||
gen = genfunc() | ||
|
||
reveal_type(gen.__aiter__()) # E: Revealed type is 'typing.AsyncGenerator[builtins.int*, void]' | ||
|
||
reveal_type(await gen.__anext__()) # E: Revealed type is 'builtins.int*' | ||
|
||
[builtins fixtures/dict.pyi] | ||
|
||
[case testAsyncGeneratorAsend] | ||
# flags: --fast-parser --python-version 3.6 | ||
from mypy_extensions import AsyncGenerator | ||
|
||
async def f() -> None: | ||
pass | ||
|
||
async def gen() -> AsyncGenerator[int, str]: | ||
await f() | ||
v = yield 42 | ||
reveal_type(v) # E: Revealed type is 'builtins.str' | ||
await f() | ||
|
||
async def h() -> None: | ||
g = gen() | ||
await g.asend(()) # E: Argument 1 to "asend" of "AsyncGenerator" has incompatible type "Tuple[]"; expected "str" | ||
reveal_type(await g.asend('hello')) # E: Revealed type is 'builtins.int*' | ||
|
||
[builtins fixtures/dict.pyi] | ||
|
||
[case testAsyncGeneratorAthrow] | ||
# flags: --fast-parser --python-version 3.6 | ||
from mypy_extensions import AsyncGenerator | ||
|
||
async def gen() -> AsyncGenerator[str, int]: | ||
try: | ||
yield 'hello' | ||
except BaseException: | ||
yield 'world' | ||
|
||
async def h() -> None: | ||
g = gen() | ||
v = await g.asend(1) | ||
reveal_type(v) # E: Revealed type is 'builtins.str*' | ||
await g.athrow(BaseException) | ||
|
||
[builtins fixtures/dict.pyi] | ||
|
||
[case testAsyncGeneratorNoSyncIteration] | ||
# flags: --fast-parser --python-version 3.6 | ||
from mypy_extensions import AsyncGenerator | ||
|
||
async def gen() -> AsyncGenerator[int, None]: | ||
for i in (1, 2, 3): | ||
yield i | ||
|
||
def h() -> None: | ||
for i in gen(): | ||
pass | ||
|
||
[builtins fixtures/dict.pyi] | ||
|
||
[out] | ||
main:9: error: Iterable expected | ||
main:9: error: AsyncGenerator[int, None] has no attribute "__iter__"; maybe "__aiter__"? | ||
|
||
[case testAsyncGeneratorNoYieldFrom] | ||
# flags: --fast-parser --python-version 3.6 | ||
from mypy_extensions import AsyncGenerator | ||
|
||
async def f() -> AsyncGenerator[int, None]: | ||
pass | ||
|
||
async def gen() -> AsyncGenerator[int, None]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add some tests with |
||
yield from f() # E: 'yield from' in async function | ||
|
||
[builtins fixtures/dict.pyi] | ||
|
||
[case testAsyncGeneratorNoReturnWithValue] | ||
# flags: --fast-parser --python-version 3.6 | ||
from mypy_extensions import AsyncGenerator | ||
|
||
async def gen() -> AsyncGenerator[int, None]: | ||
yield 1 | ||
return 42 | ||
|
||
[builtins fixtures/dict.pyi] | ||
|
||
-- The full matrix of coroutine compatibility | ||
-- ------------------------------------------ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,3 +38,5 @@ class tuple: pass | |
class function: pass | ||
class float: pass | ||
class bool: pass | ||
|
||
class BaseException: pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't comment there, but you might update the comment after
elif return_type.args:
below (addAsyncGenerator
).