Skip to content

Complete type stubs for assertTemplateUsed / assertTemplateNotUsed#1278

Open
brianhelba wants to merge 1 commit intopytest-dev:mainfrom
brianhelba:fix-assertTemplateUsed-overloads
Open

Complete type stubs for assertTemplateUsed / assertTemplateNotUsed#1278
brianhelba wants to merge 1 commit intopytest-dev:mainfrom
brianhelba:fix-assertTemplateUsed-overloads

Conversation

@brianhelba
Copy link
Copy Markdown

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
Author

@kingbuzzman Please take a look.

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.

1 participant