python / python/mypy

Proposal: infer obvious parameters and return types

Offen
#10,149 8 Kommentare 13 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Feature

Similar to #4409, but also broader, allow for mypy to infer the parameter and return types for a function that's a passthrough to other typed functions.

Pitch

Consider the case in keyring, where the get_password function is a convenience accessor to (a) resolve the backend and (b) invoke the method of the same name on that backend with the same parameters:

https://github.com/jaraco/keyring/blob/db6896acea942a86d3fbee7e2a556fffb38055ba/keyring/core.py#L54-L56

The get_keyring() is annotated and always returns a KeyringBackend. KeyringBackend is annotated and its get_password always demands str parameters and declares its return type.

As a result, it's unambiguous what the required parameters and return type for core.get_password must be.

In pypa/twine#733, we learned that if a downstream consumer of the library enables disallow_untyped_calls, it will fail on core.get_password unless that function is redundantly decorated with the same parameters and return types of KeyringBackend.get_password.

It would be nice if mypy could infer the types from these unambiguous cases like passthrough functions, possibly gated by a feature flag or a decorator on the function (e.g. @typing.passthrough), and avoid the somewhat messy redundancy that results from hand-copying the types.

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 der Durchsicht von Issue #4409 und des Passthrough-Beispiels in keyring/core.py sowie der darin referenzierten Definitionen von get_keyring() und KeyringBackend. Ermittle, wie mypy diese eindeutigen Wrapper erkennen könnte, und definiere den Abschluss als das Ableiten übereinstimmender Parameter- und Rückgabetypen ohne redundante Annotationen, wobei ein vorgeschlagenes Feature-Flag oder ein Decorator angegeben 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
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

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