Weird error message for incompatible override when using a `TypeVar` for `self`
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug Report
Minimal repro:
from typing import TypeVar
Self = TypeVar("Self")
class Flag:
def __or__(self: Self, other: Self) -> Self: ...
__ror__ = __or__
class IntFlag(int, Flag):
def __or__(self: Self, other: int) -> Self: ...
def __ror__(self: Self, other: int) -> Self: ...
mypy output:
main.py:11: error: Signature of "__ror__" incompatible with supertype "Flag"
main.py:11: note: Superclass:
main.py:11: note: def [Self] __or__(self, Self, Self) -> Self
main.py:11: note: Subclass:
main.py:11: note: def __ror__(self, int) -> IntFlag
Mypy playground link here: https://mypy-play.net/?mypy=latest&python=3.10&gist=26b60881cd02e608bb9e555db4c3f627
Expected Behavior
I'm not sure an error is even appropriate here: no incompatible override is reported for __or__.
Even if an error is appropriate here, however, the error message is weird and garbled. According to the error message, mypy seems to think that __ror__ in the superclass expects three arguments (it only expects two). It also seems to think the method in the subclass will return IntFlag (not necessarily true; the return type will be whatever self is, and self could be any subtype of IntFlag).
The function that creates this error message is here: https://github.com/python/mypy/blob/9b4bce9065cbef6185fbc77f2849b63dc9e5e293/mypy/messages.py#L824
Mypy version used
- 0.950.
- This doesn't seem to be a recent regression: I can reproduce as far back as 0.920, which is the earliest mypy version that has the verbose error messages.
- I've also reproduced the bug on mypy master.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la riproduzione minima in Python e ispeziona la costruzione della diagnostica in mypy/messages.py intorno alla riga 824. Confronta le signature riportate della superclasse e della sottoclasse con i tipi self basati su TypeVar, quindi determina se l'override debba essere accettato o come debba essere visualizzato il relativo messaggio. Il lavoro è completato quando il comportamento è corretto e la riproduzione è coperta da un test di regressione.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100