pytest-dev / pytest-django

A Django plugin for pytest.
https://pytest-django.readthedocs.io/
Other
1.33k stars 341 forks source link

Wildcard import of asserts raises false negative with mypy #991

Open Joeyh021 opened 2 years ago

Joeyh021 commented 2 years ago

Short example:

from django.test.client import Client
from pytest_django import asserts

def test_main_index_page(client: Client) -> None:
    response = client.get("/")
    asserts.assertTemplateUsed(response, "main/index.html")

Gives the following error in mypy

main\tests.py:7: error: Name "assertTemplateUsed" is not defined

I believe this is because wildcard imports import all the names under the __all__ attribute, which is generated at runtime in the asserts module (see here). Hence, when importing as wildcard, mypy sees the module as having nothing defined in it.

I'm not sure what the solution to this is, maybe a better way to import all the assertions, or perhaps at least be able to signify to static analysis tools that they do exist in __all__? I'd be happy to throw together a PR for any proposed fix.