Skip to content

Complete type stubs for assertTemplateUsed / assertTemplateNotUsed - #1278

Merged
bluetech merged 1 commit into
pytest-dev:mainfrom
brianhelba:fix-assertTemplateUsed-overloads
Aug 3, 2026
Merged

Complete type stubs for assertTemplateUsed / assertTemplateNotUsed#1278
bluetech merged 1 commit into
pytest-dev:mainfrom
brianhelba:fix-assertTemplateUsed-overloads

Conversation

@brianhelba

Copy link
Copy Markdown
Contributor

mypy 1.20 (python/mypy#20350) changes how it types the return value of functions that return None: previously it used Any (suppressing downstream errors), now it propagates the actual None type. This means the existing -> None annotation on assertTemplateUsed / assertTemplateNotUsed causes type errors when these functions are used as context managers.

This adds @overload signatures to distinguish the two calling conventions, matching the pattern already used by assertNumQueries in this file:

  • response is HttpResponseBase: direct assertion, returns None
  • response is a str (shorthand for template_name): returns context manager
  • response is None (template_name passed by keyword): returns context manager

@brianhelba
brianhelba force-pushed the fix-assertTemplateUsed-overloads branch from d63cb82 to 319d2a8 Compare April 14, 2026 05:47
mypy 1.20 (python/mypy#20350) changes how it types the return value of
functions that return `None`: previously it used `Any` (suppressing downstream
errors), now it propagates the actual `None` type. This means the existing
`-> None` annotation on `assertTemplateUsed` / `assertTemplateNotUsed` causes
type errors when these functions are used as context managers.

This adds `@overload` signatures to distinguish the two calling conventions,
matching the pattern already used by `assertNumQueries` in this file:

* `response` is `HttpResponseBase`: direct assertion, returns `None`
* `response` is a `str` (shorthand for `template_name`): returns context manager
* `response` is `None` (`template_name` passed by keyword): returns context manager
@brianhelba
brianhelba force-pushed the fix-assertTemplateUsed-overloads branch from 319d2a8 to 7ad6c89 Compare April 14, 2026 05:52
@brianhelba

Copy link
Copy Markdown
Contributor Author

@kingbuzzman Please take a look.

@brianhelba

Copy link
Copy Markdown
Contributor Author

@kingbuzzman Could you take a look? This is a typing-only bugfix, to add support for Mypy 1.20+.

@bluetech bluetech left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Consider also submitting this improvement to django-stubs, it can improve the typing there as well: https://github.com/typeddjango/django-stubs/blob/61a3af749523e9645861057588fe67230ac6b6dd/django-stubs/test/testcases.pyi#L119-L125

Comment thread pytest_django/asserts.py
template_name: None = ...,
msg_prefix: str = ...,
count: int | None = ...,
) -> AbstractContextManager[None]: ...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically it's not None, the context manager returns itself. I'll change that to Any.

@bluetech
bluetech merged commit 81290fb into pytest-dev:main Aug 3, 2026
24 checks passed
@brianhelba
brianhelba deleted the fix-assertTemplateUsed-overloads branch August 5, 2026 02:18
@brianhelba

Copy link
Copy Markdown
Contributor Author

@bluetech Thanks!

I took your suggestion to fix upstream too: typeddjango/django-stubs#3564

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants