python / python/mypy

Support multi-statement inference for custom types

Offen
#13,134 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Feature

Multi-statement inference as proposed in #254 and #1055 is supported for empty standard collection classes, like dict or list, but not for custom classes. Thus, an example such as https://mypy-play.net/?mypy=latest&python=3.10&gist=7df00fc3d8612804369dbad6b5cb973b fails.

My proposal is to allow user-defined generics to infer the generic from their first usage, so that the example work.

Pitch

This problem arose in the context of making a typed library compatible with scikit-learn (which currently is not typed). In scikit-learn, the actual data of a machine learning problem is not passed to the constructor of the object, but instead passed to the fit method (for training). Thus, if one wants to make a scikit-learn compatible object which is generic over the data accepted, one currently needs to provide the annotation, as Mypy won't infer the generic type from the call to fit. This is cumbersome for users, as most of them should benefit from the typing without altering the code to add superfluous annotations. It also obscures the examples, as adding annotations would confuse users not used to them.

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

Der Issue nennt keine Repository-Dateien oder Tests. Reproduziere das verlinkte mypy-play-Beispiel und vergleiche das Verhalten von custom-generic mit der bestehenden Mehrfachanweisungs-Inferenz für leere dict und list; abgeschlossen ist die Arbeit, wenn custom-generic aus seiner ersten Verwendung inferiert wird und die Regressionstestabdeckung das Beispiel verifiziert.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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