python / python/mypy

Need plugin hooks for functions/methods

Offen
#6,760 8 Kommentare 5 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature priority-1-normal topic-plugins topic-usability
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

For some frameworks like pytest it would be useful to have a plugin that works with functions.
Currently mypy has decorator hook for classes.

Some ideas for possible hooks:

  1. update function namespace to inject some names
  2. modify function signature
  3. generate function signature (types)

For example pytest has the following fixture

from pathlib import Path

@pytest.fixture
def db_path(tmp_path: Path) -> Path:
    return tmp_path / 'test.db'

The fixture is used by name:

def test_a(db_path) -> None:
    ....

db_path argument here is a calucated the result of db_path() fixture call.
It is bound by parameter name which is the same as the fixture name.

Adding a type to db_path argument is tedious, especially if a fixture returns some complex type like Callable[[str, int], Awaitable[web.Response]].

Technically all needed information can be extracted from fixture definition if the definition is type annotated.

A plugin for pytest would be awesome but as @ilevkivskyi said mypy needs a new hook to support it.

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, den vorhandenen Decorator-Hook von mypy für Klassen und die in diesem Issue beschriebenen Konzepte der Plugin-API zu überprüfen. Ermittle, wie die Injektion in den Funktionsnamensraum, die Änderung von Signaturen und generierte Signaturen für pytest-style Fixtures funktionieren sollten, und definiere anschließend Tests, die zeigen, dass Fixture-Parameter den inferierten Rückgabetyp erhalten.

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
Ruhig
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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