python / python/mypy

Mypy to recognize the return type of pytest fixtures

Aperta
#17,668 1 commento 12 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Feature

When using pytest one can declare fixtures and also define a return type for them, which IDE's like Pycharm can then use to provide further code completion.
Currently mypy does not know what the type of the fixture is and ignores any obvious errors towards the type. A mechanism could be added to make sure mypy knows the type to detect issues in tests at static code check (either via mypy directly or by adding a plugin hook that changes argument types)

Pitch

either integrated into mypy or a way to write a plugin to do this. The current hooks in the plugins do not allow to inform mypy what type a specific argument is, they only seem to change what return type are of the functions it sees. Mypy does not seem know pytest fixture arguments are actually function calls, and does not provide a way for plugins to tell it what type an argument is.


import pytest

class MyClass:
    def foo(self):
        pass

def test_myclass(my_typed_fixture):
    my_typed_fixture.foo()
    my_typed_fixture.bar()
    assert True

@pytest.fixture
def my_typed_fixture() -> MyClass:
    return MyClass()

This should see that 'bar' does not exist, but it does not because we did not add a type hint to the argument my_typed_fixture being 'MyClass' (while we could, this makes it harder for testers and it is not needed for typing to work in major IDE's)

Pycharm does know, by scanning the hierarchy for matching fixtures (in test, in conftest.py of same direcvtory and higher directories). This makes pycharm very powerful for checking code validity, but that is not run in pipelines or commit-hooks.

Screenshot 2024-08-13 at 11 13 00

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 leggendo gli hook dei plugin esistenti di mypy e l’esempio di fixture di pytest nell’issue. Determina come vengono rappresentati gli argomenti delle fixture e se mypy o un plugin può fornire i relativi tipi di ritorno annotati. Il lavoro è completato quando mypy segnala un uso non valido di una fixture, come l’assenza del metodo bar, senza richiedere un’annotazione per l’argomento.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.