Description
Summary
Consider the example:
from collections.abc import AsyncGenerator
import aiofiles
class AsyncTest:
async def __call__(self, path: str) -> AsyncGenerator[str]:
async with aiofiles.open(path) as f:
async for line in f:
yield line
async def test() -> None:
test: AsyncTest = AsyncTest()
items: list[str] = [item async for item in test('/')]Ty does not complain about anything here, as it shouldn't. But if I replace the __call__ function with a stub:
class AsyncTest:
async def __call__(self, path: str) -> AsyncGenerator[str]: ...
async def test() -> None:
test: AsyncTest = AsyncTest()
items: list[str] = [item async for item in test('/')]Then, on the last line, ty reports an error: Object of type CoroutineType[Any, Any, AsyncGenerator[str, None]] is not async-iterable.
I assume that in the first case, ty can see the yield in the function body, so it infers that despite the async def, this is actually an async generator function.
But per PEP 525 / typing spec: if you annotate the return type of an async generator function, you should use AsyncGenerator[YieldType, SendType] as the return annotation, and the function is understood to be an async generator (not a coroutine returning one). Therefore, ty should be able to understand the function is an async generator even in the second example.
Version
0.0.17
Activity
sharkdp commentedon Feb 23, 2026
Correct. That behavior is consistent with what other type checkers do.
Where exactly did you see this in PEP 525 or the typing spec? I couldn't find that reference.
There are some relevant discussions on discuss.python.org here: https://discuss.python.org/t/ambivalent-async-generator-function-signature/71439 https://discuss.python.org/t/overloads-of-async-generators-inconsistent-coroutine-wrapping/56665/12. Some discussed solutions are to omit the
asyncofdef __call__, or to add a dummyyield …body in the stub.mypy has documentation on this topic and a dedicated diagnostic. So it's reasonable to help ty users here as well.
niksabaldun commentedon Feb 23, 2026
I have to apologize. I trusted Claude on this, but on further scrutiny it admitted that it "cited PEP 525 / typing spec with more confidence than was warranted". I should have been more thorough.
So, the solution is to add a dummy yield to the stub. Closing the issue.
sharkdp commentedon Feb 23, 2026
I see, thanks for the clarification.
I think it's reasonable to leave this open though, given that it confuses both bots and humans (me included).
lvndry commentedon May 5, 2026
Hit this today with an abstract base class. Minimal repro:
ty 0.0.34 on the
Concreteoverride:Workaround for now: declare the abstract method as
definstead ofasync def. Works at runtime but misrepresents the contract to implementors.jonathandung commentedon Jun 1, 2026
Why does it misrepresent the contract? If I see an
async deffunction, I would expect to have toawaitits return value. You can make a sync function that trivially combines some async generators and returns a new async generator; I do this often. There may also be legitimate cases where you would make an async function that returns anAsyncGenerator. For example:primewould return a coroutine resolving to an async generator, and ifasync def func() -> AsyncGeneratorwere to be unconditionally interpreted as sugar fordef func() -> AsyncGenerator, the above case would require an even less idiomatic workaround (omitting the async def and annotating the return type asCoroutineType[Any, Any, AsyncGenerator[T, R]]).Of course, I am talking about .pyi files here, where there is no implementation. It should of course be acceptable (and desired!) that a concrete implementation using
async defandyieldin its body is inferred as returning an async generator, and not an awaitable of.[-]Properly recognize async generator functions[/-][+]Better diagnostic to help users annotate async generator in stub[/+]carljm commentedon Jun 1, 2026
@lvndry I don't think using
defoverasync defdoes misrepresent the contract -- for an async generator, the annotated return type fully describes the actual return type, and theasyncprefix keyword (when there is no body) is if anything the misleading part (since it suggests a possible wrapping in an awaitable, that doesn't actually occur at runtime for an async generator).In any case, as described in the mypy docs linked above, the two options here are "remove
async" or "add a stub body withyield". Given the way these features have been specified to work, this is (perhaps unfortunately) just the state of things, and is true for all Python type checkers. So the only actionable item for ty here is to improve our diagnostics and documentation to recommend those two options.[ty] Improve diagnostics for async generator stubs (#28692)