-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Generator with await not considered an AsyncGenerator #10534
Labels
Comments
Seems you are missing |
Simple repro with mypy latest: https://mypy-play.net/?mypy=master&python=3.12&gist=71cdbfcc61179de3290480733083180c&flags=warn-unreachable async def something(i: int) -> tuple[None, ...]:
return (None,)
async def main() -> None:
async_or_nah = (
thing
for i in range(5)
for thing in await something(i)
)
async for blah in async_or_nah: # error: "Generator[None, None, None]" has no attribute "__aiter__" (not async iterable) [attr-defined]
print(blah) |
hauntsaninja
pushed a commit
that referenced
this issue
Jun 30, 2024
Fixes #10534 This PR fixes a bug in typechecking asynchronous generators. Mypy currently typechecks a generator/comprehension as `AsyncGenerator` if the leftmost expression contains `await`, or if it contains an `async for`. However, there are other situations where we should get async generator: If there is an `await` expression in any of the conditions or in any sequence except for the leftmost one, the generator/comprehension should also be typechecked as `AsyncGenerator`. I've implemented this change in Mypy and added a test case to assert this behavior. If I enter the test cases into a regular repl, I can confirm that the runtime representation is generator/async_generator as the test case expects. According to the [language reference](https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-comp_for): > If a comprehension contains either async for clauses or await expressions or other asynchronous comprehensions it is called an asynchronous comprehension. Confusingly, the documentation itself is actually not quite correct either, as pointed out in python/cpython#114104 Alongside this change, I've made a PR to update the docs to be more precise: python/cpython#121175 has more details.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bug Report
await
keyword are considered Generator, rather than AsyncGeneratorawait
in a non-async functionTo Reproduce
test.py:
python:
> py test.py
mypy:
mypy test.py
Expected Behavior
No errors should be reported.
Actual Behavior
Your Environment
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: