python / python/mypy

Cannot create a class that supports Iterable[RichComparisonT]

Aperta
#14,399 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con la riproduzione in richcmp.py e con l’interazione segnalata tra Iterable[SupportsRichComparisonT] e iter. Confronta il comportamento di mypy con quello di pyright usando l’esempio fornito per Python 3.10. Il lavoro è completato quando l’esempio supera il controllo dei tipi senza gli errori segnalati di override e annotazione delle variabili.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.