You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Mypy doesn't work well with AsyncGenerator and @abstractmethod #18681
I want to define "async def" method with @abstractmethod decorator, to show that all subclasses need to use asynchronous version. But problem is that when I do this - mypy gets crazy and tells that it has a problem in implementation of the interface method.
I think that problem is in usage of yield in the actual method to make it a generator, and just pass for the abstract method, that is missing yield. but in this case we sacrifice the correct definition of the base method and it seems misleading for developer that want to implement the base method that is missing he async keyword.
Actual Behavior
Return type "AsyncGenerator[int, None]" of "get" incompatible with return type "Coroutine[Any, Any, AsyncGenerator[int, None]]" in supertype "Base" [override]
Yep, it's the lack of yield, meaning the checker just wraps AsyncGenerator in Coroutine. If you make the abstract method a synchronous method it'll work fine, since the only difference between the different function types is their return type.
Yep, it's the lack of yield, meaning the checker just wraps AsyncGenerator in Coroutine. If you make the abstract method a synchronous method it'll work fine, since the only difference between the different function types is their return type.
Can you give an example of how to define asynchronous def as abstract method? Maybe I miss something
Just do def func(self) -> Coroutine[int]:, or the same with AsyncGenerator, Generator or superclasses of those. It’s more explicit, but there’s no wrapping going on so the result is clear.
Just do def func(self) -> Coroutine[int]:, or the same with AsyncGenerator, Generator or superclasses of those. It’s more explicit, but there’s no wrapping going on so the result is clear.
My target goal is to define asynchronous abstract method to show the interface to use to implement a final class. I posted two links with code and just wondering how to tell mypy to keep my abstract as it is. Maybe I need use another decorator, like async_abstract or something like this?
This is just another manifestation of #5385 with s/Protocol/ABC/ (or also #5070 with more discussion, if you prefer). The most common approach that still uses async def for intuitively correct interface is
Activity
TeamSpen210 commentedon Feb 14, 2025
Yep, it's the lack of yield, meaning the checker just wraps
AsyncGeneratorinCoroutine. If you make the abstract method a synchronous method it'll work fine, since the only difference between the different function types is their return type.1st commentedon Feb 14, 2025
Can you give an example of how to define asynchronous def as abstract method? Maybe I miss something
TeamSpen210 commentedon Feb 15, 2025
Just do
def func(self) -> Coroutine[int]:, or the same withAsyncGenerator,Generatoror superclasses of those. It’s more explicit, but there’s no wrapping going on so the result is clear.1st commentedon Feb 15, 2025
My target goal is to define asynchronous abstract method to show the interface to use to implement a final class. I posted two links with code and just wondering how to tell mypy to keep my abstract as it is. Maybe I need use another decorator, like async_abstract or something like this?
sterliakov commentedon Feb 15, 2025
This is just another manifestation of #5385 with s/Protocol/ABC/ (or also #5070 with more discussion, if you prefer). The most common approach that still uses
async deffor intuitively correct interface is