## Summary
An `async def` stub without `yield` describes a coroutine function, even
when its return annotation is `AsyncIterator` or `AsyncGenerator`. For
example:
`stubs.pyi`:
```python
from collections.abc import AsyncIterator
async def values() -> AsyncIterator[int]: ...
```
`main.py`:
```python
from stubs import values
async def consume() -> None:
async for value in values():
pass
```
Before, the diagnostic identifies the missing iteration method but gives
no guidance on correcting the stub:
```text
error[not-iterable]: Object of type `CoroutineType[Any, Any, AsyncIterator[int]]` is not async-iterable
info: It has no `__aiter__` method
```
After, we point to the declaration and suggest how to declare an async
generator. We also suggest `await`, since a coroutine can intentionally
return an async iterator:
```text
error[not-iterable]: Object of type `CoroutineType[Any, Any, AsyncIterator[int]]` is not async-iterable
--> main.py:4:24
|
4 | async for value in values():
| ^^^^^^^^
|
::: stubs.pyi:3:1
|
3 | async def values() -> AsyncIterator[int]: ...
| ---------------------------------------- Without `yield` in the function body this function returns a coroutine
info: It has no `__aiter__` method
help: `await` the coroutine before iterating over its result
help: To declare `values` as an async generator, use `def` rather than `async def` or add `yield` to the body
```
For declarations in dependencies, we suggest reporting an incorrect stub
to the library maintainers. The stub guidance also appears for
incompatible callable return types, including method overrides, and is
limited to functions with stub-like bodies. The typing rules are
unchanged.
Closes https://github.com/astral-sh/ty/issues/2875.
1.9 KiB
What it does
Checks for objects that are not iterable but are used in a context that requires them to be.
Why is this bad?
Iterating over an object that is not iterable will raise a TypeError at runtime.
Examples
# TypeError: 'int' object is not iterable
for i in 34: # error
pass
Common issues
Async generator stubs
Calling an async def function whose body contains yield produces an async iterator, which can be
consumed with async for. Without yield, calling the function produces a coroutine, and its
return annotation describes the result of awaiting that coroutine.
This distinction matters in stub files, where replacing the implementation with ... removes the
yield. For example, this stub describes a coroutine function, even though its return annotation is
AsyncIterator[int]:
stubs.pyi:
from collections.abc import AsyncIterator
async def values() -> AsyncIterator[int]: ...
Iterating over the coroutine is an error. An async for loop awaits each item; it does not
automatically await a coroutine to obtain the iterator:
main.py:
from stubs import values
async def consume() -> None:
# error: "Object of type `CoroutineType[Any, Any, AsyncIterator[int]]` is not async-iterable"
async for value in values():
print(value)
To declare a function that directly produces an async iterator, use def rather than async def in
the stub:
with_def.pyi:
from collections.abc import AsyncIterator
def values() -> AsyncIterator[int]: ...
Alternatively, keep async def and include a yield expression in the stub body:
with_yield.pyi:
from collections.abc import AsyncIterator
async def values() -> AsyncIterator[int]:
yield 1
If the function intentionally returns a coroutine that produces an async iterator, await it before
iterating: async for value in await values(): ....