mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Cannot create a class that supports Iterable[RichComparisonT]

Open Dr-Irv opened this issue 3 years ago • 0 comments

Bug Report

In pandas-stubs, we'd like to be able to have a class that can be passed to the standard python sorted(), max(), min() functions, etc. But mypy doesn't seem to support this, while pyright does.

To Reproduce

from typing import TYPE_CHECKING, Iterable, Iterator, TypeVar

if TYPE_CHECKING:
    from _typeshed import SupportsRichComparisonT as SRCT
else:   # Use this to make the code runnable
    SRCT = TypeVar("SRCT")


class MyList(Iterable[SRCT]):
    def __init__(self, x: list[int]):
        self._x = x

    def __iter__(self) -> Iterator[int]:
        for i in self._x:
            yield i


foo = MyList([3, 2, 1])
print(sorted(foo))

Expected Behavior

No error reported by mypy

Actual Behavior

richcmp.py:13: error: Return type "Iterator[int]" of "__iter__" incompatible with return type "Iterator[SRCT]" in supertype "Iterable"  [override]
richcmp.py:18: error: Need type annotation for "foo"  [var-annotated]
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.991
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10

Dr-Irv avatar Jan 06 '23 02:01 Dr-Irv