pytest-django
pytest-django copied to clipboard
Wildcard import of asserts raises false negative with mypy
trafficstars
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.