python / python/mypy

Mypy to recognize the return type of pytest fixtures

Offen
#17,668 1 Kommentar 12 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

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

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, die vorhandenen Plugin-Hooks von mypy und das pytest-Fixture-Beispiel im Issue zu lesen. Ermittle, wie Fixture-Argumente dargestellt werden und ob mypy oder ein Plugin deren annotierte Rückgabetypen bereitstellen kann. Als erledigt gilt die Aufgabe, wenn mypy eine ungültige Fixture-Verwendung wie die fehlende bar-Methode meldet, ohne dass eine Argumentannotation erforderlich ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
testing, tooling
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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