-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Mypy doesn't work well with AsyncGenerator and @abstractmethod #18681
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
Comments
Yep, it's the lack of yield, meaning the checker just wraps |
Can you give an example of how to define asynchronous def as abstract method? Maybe I miss something |
Just do |
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 class Base(ABC):
@abstractmethod
async def get(self, start: int, end: int) -> AsyncGenerator[int, None]:
if False: yield 1 |
Uh oh!
There was an error while loading. Please reload this page.
Bug Report
Mypy doesn't work well with
AsyncGenerator
and@abstractmethod
To Reproduce
Working (without async def): https://mypy-play.net/?mypy=latest&python=3.12&gist=b3f4c282f2435ac7bbbee2f435de3de7
Not working (with async def): https://mypy-play.net/?mypy=latest&python=3.12&gist=83f18b68c8ce201ecc907714bf947571
Expected Behavior
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 justpass
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
The text was updated successfully, but these errors were encountered: