python / python/mypy

Mypy to recognize the return type of pytest fixtures

Abierto
#17,668 1 comentario 12 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

feature
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Merge medio
1 d 18 h
PR fusionados (30 d)
54

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Empieza leyendo los hooks de plugins existentes de mypy y el ejemplo de fixture de pytest incluido en el issue. Determina cómo se representan los argumentos de los fixtures y si mypy o un plugin puede proporcionar sus tipos de retorno anotados. Se considera terminado cuando mypy informa de usos no válidos de fixtures, como la ausencia del método bar, sin requerir una anotación para el argumento.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
testing, tooling
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Necesita aclaración
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.