python / python/mypy

Protocol restriction in generics do not work in methods. But work on functions.

Offen
#11,167 0 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug Report

Protocol restriction in generics do not work in methods. But work on functions.

To Reproduce

from typing import *

CanCompare = TypeVar("CanCompare", bound='Comparable')


class Comparable(Protocol):
    def __lt__(self: CanCompare, other: CanCompare) -> bool:
        pass

    def __gt__(self: CanCompare, other: CanCompare) -> bool:
        pass

    def __le__(self: CanCompare, other: CanCompare) -> bool:
        pass

    def __ge__(self: CanCompare, other: CanCompare) -> bool:
        pass


A = TypeVar('A', covariant=True)


class CustomList(list, Generic[A]):

    def getMax(self: 'CustomList'[CanCompare]) -> Optional[CanCompare]:
        return functionGetMax(self)


def functionGetMax(aList: 'CustomList'[CanCompare]) -> Optional[CanCompare]:
    pass


canCompareMe: CustomList[int] = CustomList([1, 2, 3])
canCompareMe.getMax()  # ok
functionGetMax(canCompareMe)  # ok


class NotComparable:
    pass  # no comparisons implemented


impossibleToCompare: CustomList[NotComparable] = CustomList([NotComparable(), NotComparable()])
impossibleToCompare.getMax() # Bug: No error raised
functionGetMax(impossibleToCompare)  # Correctly raised error: Value of type variable "CanCompare" of "functionGetMax" cannot be "NotComparable"

Expected Behaviou
Mypy should raise an error on the .getMax() method when called on a CustomList[NotComparable].

Actual Behavior
Instead, no error was raised.
Curiously, the functionGetMax function correctly raises the error.

(Write what happened.)

Your Environment

  • Mypy version used: Both mypy 0.910 and mypy-0.920+dev.58fb493b04c61f6202ac8ba0811d3ff8546a8e60
  • Mypy command-line flags: No flags
  • Mypy configuration options from mypy.ini (and other config files): no config options
  • Python version used: Python 3.9.6 (v3.9.6:db3ff76da1, Jun 28 2021, 11:49:53)
  • Operating system and version: Macosx 10.15.7

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem bereitgestellten Reproducer und vergleiche, wie mypy die Einschränkung des generischen Protokolls in getMax gegenüber functionGetMax prüft. Verfolge den Pfad der Typprüfung von Methodenaufrufen und dessen Umgang mit CustomList[NotComparable]; abgeschlossen ist die Aufgabe, wenn der Methodenaufruf denselben Type-Variable-Fehler wie der Funktionsaufruf erzeugt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
tooling
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.