Skip to content

Async iterator + inheritance = error #17128

@SashaOv

Description

@SashaOv

Bug Report

The return type of async generator in the derived class cannot match the superclass definition.

To Reproduce

from abc import ABC, abstractmethod from typing import AsyncIterator, Protocol class BaseClass(ABC): @abstractmethod async def async_generator(self) -> AsyncIterator[int]: raise NotImplementedError class SubClass(BaseClass): async def async_generator(self) -> AsyncIterator[int]: yield 1 # -> Error message: Return type "AsyncIterator[int]" of "async_generator" incompatible with return type "Coroutine[Any, Any, AsyncIterator[int]]" in supertype "BaseClass" class PBaseClass(Protocol): async def async_generator(self) -> AsyncIterator[int]: pass class PSubClass(PBaseClass): async def async_generator(self) -> AsyncIterator[int]: yield 1 # -> Error message: Return type "AsyncIterator[int]" of "async_generator" incompatible with return type "Coroutine[Any, Any, AsyncIterator[int]]" in supertype "PBaseClass"  **Your Environment** - Mypy version used: 1.5.1 - Mypy command-line flags: `mypy --ignore-missing-imports --config-file tox.ini $src` - Mypy configuration options from `mypy.ini` (and other config files):

[mypy]
mypy_path = {[tox]module}
ignore_missing_imports = True
exclude = interview

- Python version used: 3.11.1 

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions