From 7ec5e702386b1b86e830f4fbb7e3af65a2fe1b39 Mon Sep 17 00:00:00 2001 From: Matthias Schoettle Date: Thu, 26 Feb 2026 15:42:26 -0500 Subject: [PATCH 1/2] Fix type hints of assert functions Updated type hints in assert functions to use Iterable instead of Collection. And have assertRaisesMessage return something to use as a context manager. --- pytest_django/asserts.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pytest_django/asserts.py b/pytest_django/asserts.py index 0dbc47bf..26b86720 100644 --- a/pytest_django/asserts.py +++ b/pytest_django/asserts.py @@ -25,7 +25,7 @@ class MessagesTestCase(MessagesTestMixin, TestCase): test_case = TestCase("run") if TYPE_CHECKING: - from collections.abc import Callable, Collection, Iterator, Sequence + from collections.abc import Callable, Collection, Iterable, Iterator, Sequence from contextlib import AbstractContextManager from typing import Any, overload @@ -130,14 +130,14 @@ def assertRaisesMessage( expected_message: str, *args: Any, **kwargs: Any, - ) -> None: ... + ) -> Any: ... def assertWarnsMessage( expected_warning: Warning, expected_message: str, *args: Any, **kwargs: Any, - ) -> None: ... + ) -> Any: ... def assertFieldOutput( fieldclass: type[forms.Field], @@ -146,7 +146,7 @@ def assertFieldOutput( field_args: Any = ..., field_kwargs: Any = ..., empty_value: str = ..., - ) -> None: ... + ) -> Any: ... def assertHTMLEqual( html1: str, @@ -201,7 +201,7 @@ def assertXMLNotEqual( # Removed in Django 5.1: use assertQuerySetEqual. def assertQuerysetEqual( qs: Iterator[Any] | list[Model] | QuerySet | RawQuerySet, - values: Collection[Any], + values: Iterable[Any], transform: Callable[[Model], Any] | type[str] | None = ..., ordered: bool = ..., msg: str | None = ..., @@ -209,7 +209,7 @@ def assertQuerysetEqual( def assertQuerySetEqual( qs: Iterator[Any] | list[Model] | QuerySet | RawQuerySet, - values: Collection[Any], + values: Iterable[Any], transform: Callable[[Model], Any] | type[str] | None = ..., ordered: bool = ..., msg: str | None = ..., From ca49b8a2e1c01968861cee3e9d1eb90c67c4498e Mon Sep 17 00:00:00 2001 From: Matthias Schoettle Date: Thu, 26 Feb 2026 17:41:36 -0500 Subject: [PATCH 2/2] Remove unused 'Collection' import --- pytest_django/asserts.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytest_django/asserts.py b/pytest_django/asserts.py index 26b86720..6d7c6c50 100644 --- a/pytest_django/asserts.py +++ b/pytest_django/asserts.py @@ -25,7 +25,7 @@ class MessagesTestCase(MessagesTestMixin, TestCase): test_case = TestCase("run") if TYPE_CHECKING: - from collections.abc import Callable, Collection, Iterable, Iterator, Sequence + from collections.abc import Callable, Iterable, Iterator, Sequence from contextlib import AbstractContextManager from typing import Any, overload