python / python/mypy

Need plugin hooks for functions/methods

Aperta
#6,760 8 commenti 5 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature priority-1-normal topic-plugins topic-usability
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia esaminando l’hook per i decorator esistente di mypy per le classi e i concetti della Plugin API descritti nell’issue. Determina come dovrebbero funzionare l’iniezione nello spazio dei nomi delle funzioni, la modifica delle signature e le signature generate per le fixture in stile pytest, quindi definisci test che mostrino che i parametri delle fixture ricevono il tipo di ritorno inferito.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Tranquilla
Chiarezza
Da chiarire
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.