python / python/mypy

More-helpful typing errors with Generics and Callables

Offen
#15,522 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature topic-error-reporting topic-usability
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Feature

Taking into consideration the following code (using mypy==1.4.1), the error messages are not very useful in debugging the problem. I have included the Pyright error message for comparison:

"""A test pipeline"""
from __future__ import annotations

from typing import Callable, Generic, TypeVar

from pydantic import BaseModel

from filter_base.filters.base_filter import BaseFilter

ModelT = TypeVar("ModelT", bound=BaseModel)


class TestFilter(BaseFilter, Generic[ModelT]):
    """A filter for loading a JSON file."""

    def __init__(self, model_type: type[ModelT]):
        """Test filter."""
        self.model_type: type[ModelT] = model_type

    def output(self) -> ModelT:
        """Output the loaded model"""
        return self.model_type()

    def input(self, obj: ModelT) -> None:
        """Input the model"""
        pass


T = TypeVar("T")


def connect(
    connect_signal: Callable[[], T],
    connect_slot: Callable[[T], None],
) -> None:
    """Connect a signal to a slot"""

    # TODO: this is a bit silly - we're just immedicately calling the signal/slot
    # however - it'll do for the purposes of type checking
    connect_slot(connect_signal())


class SimpleModel(BaseModel):
    """A simple model"""

    a: int
    b: str


class SimpleModel2(BaseModel):
    """A simple model"""

    c: int
    d: str


def main() -> None:
    a = TestFilter(SimpleModel)
    b = TestFilter(SimpleModel2)

    connect(a.output, b.input)

The diagnostics from mypy and Pyright are as follows (error is on the final line of code):

Diagnostics:
1. mypy: Cannot infer type argument 1 of "connect" [misc]
2. Pyright: Argument of type "(obj: SimpleModel2) -> None" cannot be assigned to parameter "connect_slot" of type "(T@connect) -> None" in function "connect"
     Type "(obj: SimpleModel2) -> None" cannot be assigned to type "(SimpleModel) -> None"
       Parameter 1: type "SimpleModel" cannot be assigned to type "SimpleModel2"
         "SimpleModel" is incompatible with "SimpleModel2" [reportGeneralTypeIssues]

Pitch

Having the error messages of mypy more in-line with Pyright's reporting would mean that it is not essential to use Pyright's typechecking system when developing these sorts of generic systems.

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 damit, das bereitgestellte Python-Beispiel mit mypy 1.4.1 zu reproduzieren, und vergleiche seine abschließende Diagnose mit der in der Issue gezeigten Pyright-Ausgabe. Erledigt ist die Aufgabe, wenn mypy das inkompatible Generic Callable-Argument mit ähnlich verwertbaren Typdetails erklärt; die Issue nennt keine Quelldateien oder Tests, auf die gezielt werden soll.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

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