pytest-dev / pytest-dev/pytest-django

Wildcard import of asserts raises false negative with mypy

Open
#991 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.5k
Forks
367
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with pytest_django/asserts.py around the runtime-generated all described in the issue, then reproduce the example with mypy. The work is done when mypy recognizes assertTemplateUsed through the shown import pattern and the behavior is covered by a regression check.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
testing-qa, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.