Faulty Protocol-TypeVar interactions
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
🐛 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
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 damit, den bereitgestellten Reproducer für Protocol, TypeVar, Generic, ndarray und bool_ mit mypy und --show-error-codes auszuführen. Untersuche die Interaktion von add und radd, die den Type-Var-Fehler ausgibt; abgeschlossen ist die Aufgabe, wenn der aufgedeckte Rückgabetyp korrekt bleibt, ohne diesen Fehler zu erzeugen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100