Faulty Protocol-TypeVar interactions
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
A bit of background: I'm trying the to describe certain operations (e.g. addition) of the numpy.ndarray class.
As the annotations of most operations are highly dependent on the type of underlying scalar, it is easier to let
aforementioned scalars handle all the details and dispatch results the actual ndarray class using protocols.
However, when trying to implementing this I encountered some odd interaction between the, herein created,
__radd__-protocol and a typevar in the scalars' actual __radd__ method:
from typing import Protocol, TypeVar, Generic
# Define a protocol for `__radd__`
T_co = TypeVar("T_co", covariant=True)
T_contra = TypeVar("T_contra", contravariant=True)
class SupportsRAdd(Protocol[T_contra, T_co]):
def __radd__(self, other: T_contra) -> T_co: ...
With the protocol in hand we can define our scalar (e.g. bool_) and the array used for embedding the scalar:
class generic: ... # baseclass for numpy scalars
T1 = TypeVar("T1", bound=generic)
T2 = TypeVar("T2", bound=generic)
class bool_(generic):
def __radd__(self, other: T1) -> T1: ...
def __add__(self, other: T1) -> T1: ...
class ndarray(Generic[T1]):
def __add__(self, other: SupportsRAdd[T1, T2]) -> ndarray[T2]: ...
So far so good.
Additions involving ndarray[bool_] should now now be described by the underlying bool_, which is more or less what happens:
array: ndarray[bool_]
scalar: bool_
# note: Revealed type is 'op.bool_*'
reveal_type(scalar + scalar)
# error: Value of type variable "T1" of "__radd__" of "bool_" cannot be "ndarray[bool_]" [type-var]
# note: Revealed type is 'op.ndarray*[op.bool_]'
reveal_type(array + scalar)
In both cases the return-type is correctly inferred, but in the array + scalar example it is accompanied by an error (a false positive?). Note removing the typevars in bool_.__add__ & .__radd__ does get rid of the error, but that's not exactly a viable solution due the sheer amount of generic subclasses.
Expected Behavior
Reveal the return type without any errors.
Actual Behavior
The return type is revealed in addition to producing a [type-var]-related error.
Your Environment
- Mypy version used: mypy 0.782
- Mypy command-line flags:
--show-error-codes(including or excluding--strictmakes no difference here) - Mypy configuration options from
mypy.ini(and other config files): n.a. - Python version used: Python 3.8.3
- Operating system and version: macOS 10.15.6
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 eseguendo con mypy e --show-error-codes il reproducer fornito per Protocol, TypeVar, Generic, ndarray e bool_. Analizza l’interazione tra add e radd che emette l’errore type-var; il lavoro è completato quando il tipo restituito rivelato rimane corretto senza produrre tale errore.
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
- 35/100