Cannot create a class that supports Iterable[RichComparisonT]
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der Reproduktion in richcmp.py und der gemeldeten Wechselwirkung zwischen Iterable[SupportsRichComparisonT] und iter. Vergleiche das Verhalten von mypy mit pyright anhand des bereitgestellten Beispiels für Python 3.10. Erledigt ist die Aufgabe, wenn das Beispiel ohne die gemeldeten Fehler bei override und Variablenannotationen eine Typprüfung besteht.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100