Thanks for the writeup. Note you can see mypy’s documentation that covers this over here: More types - mypy 1.13.0 documentation
Given the nested Callable[..., Coroutine[Any, Any, AsyncGenerator[X, None]]]
type is rare in practice (but not non-existent), I think it’s usually still intuitive to readers of the signature what the type is. The main difficulty I’ve seen comes up in the cases when users omit the body (e.g. protocols, base classes with dummy impls, stubs, overloads). See also Overloads of async generators: inconsistent Coroutine wrapping - #12 by hauntsaninja
I think the options here are:
- Rely on documentation and special cased type checker diagnostics (mypy gained some, but could have more)
- Add special casing in tools that consume annotations so that
async def
never adds a wrapping toAsyncGenerator
return type. See sterliakov’s post here for some discussion of this: Overloads of async generators: inconsistent Coroutine wrapping - #15 by sterliakov - Add some new special form or syntax that type checkers would interpret as if there was a
yield
in the body. I think this could be confusing for backward compat and runtime type checking reasons.