Skip to content

Commit 28484f4

Browse files
committed
Add pytest_django.DjangoCaptureOnCommitCallbacks for typing purposes
This allows typing the `django_capture_on_commit_callbacks` fixture.
1 parent 017bd77 commit 28484f4

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

docs/helpers.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,14 @@ Example usage::
514514
assert mailoutbox[0].subject == 'Contact Form'
515515
assert mailoutbox[0].body == 'I like your site'
516516

517+
If you use type annotations, you can annotate the fixture like this::
518+
519+
from pytest_django import DjangoCaptureOnCommitCallbacks
520+
521+
def test_on_commit(
522+
django_capture_on_commit_callbacks: DjangoCaptureOnCommitCallbacks,
523+
):
524+
...
517525

518526
.. fixture:: mailoutbox
519527

pytest_django/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
__version__ = "unknown"
66

77

8+
from .fixtures import DjangoCaptureOnCommitCallbacks
89
from .plugin import DjangoDbBlocker
910

1011

1112
__all__ = [
1213
"__version__",
14+
"DjangoCaptureOnCommitCallbacks",
1315
"DjangoDbBlocker",
1416
]

pytest_django/fixtures.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
from typing import (
88
TYPE_CHECKING,
99
Any,
10+
Callable,
1011
ClassVar,
12+
ContextManager,
1113
Generator,
1214
Iterable,
1315
List,
1416
Literal,
1517
Optional,
18+
Protocol,
1619
Tuple,
1720
Union,
1821
)
@@ -647,8 +650,20 @@ def django_assert_max_num_queries(pytestconfig: pytest.Config):
647650
return partial(_assert_num_queries, pytestconfig, exact=False)
648651

649652

653+
class DjangoCaptureOnCommitCallbacks(Protocol):
654+
"""The type of the `django_capture_on_commit_callbacks` fixture."""
655+
656+
def __call__(
657+
self,
658+
*,
659+
using: str = ...,
660+
execute: bool = ...,
661+
) -> ContextManager[list[Callable[[], Any]]]:
662+
pass # pragma: no cover
663+
664+
650665
@pytest.fixture()
651-
def django_capture_on_commit_callbacks():
666+
def django_capture_on_commit_callbacks() -> DjangoCaptureOnCommitCallbacks:
652667
from django.test import TestCase
653668

654-
return TestCase.captureOnCommitCallbacks
669+
return TestCase.captureOnCommitCallbacks # type: ignore[no-any-return]

tests/test_fixtures.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
from .helpers import DjangoPytester
2020

21-
from pytest_django import DjangoDbBlocker
21+
from pytest_django import DjangoCaptureOnCommitCallbacks, DjangoDbBlocker
2222
from pytest_django_test.app.models import Item
2323

2424

@@ -232,7 +232,9 @@ def test_queries(django_assert_num_queries):
232232

233233

234234
@pytest.mark.django_db
235-
def test_django_capture_on_commit_callbacks(django_capture_on_commit_callbacks) -> None:
235+
def test_django_capture_on_commit_callbacks(
236+
django_capture_on_commit_callbacks: DjangoCaptureOnCommitCallbacks,
237+
) -> None:
236238
if not connection.features.supports_transactions:
237239
pytest.skip("transactions required for this test")
238240

@@ -255,7 +257,9 @@ def test_django_capture_on_commit_callbacks(django_capture_on_commit_callbacks)
255257

256258

257259
@pytest.mark.django_db(databases=["default", "second"])
258-
def test_django_capture_on_commit_callbacks_multidb(django_capture_on_commit_callbacks) -> None:
260+
def test_django_capture_on_commit_callbacks_multidb(
261+
django_capture_on_commit_callbacks: DjangoCaptureOnCommitCallbacks,
262+
) -> None:
259263
if not connection.features.supports_transactions:
260264
pytest.skip("transactions required for this test")
261265

@@ -282,7 +286,7 @@ def test_django_capture_on_commit_callbacks_multidb(django_capture_on_commit_cal
282286

283287
@pytest.mark.django_db(transaction=True)
284288
def test_django_capture_on_commit_callbacks_transactional(
285-
django_capture_on_commit_callbacks,
289+
django_capture_on_commit_callbacks: DjangoCaptureOnCommitCallbacks,
286290
) -> None:
287291
if not connection.features.supports_transactions:
288292
pytest.skip("transactions required for this test")

0 commit comments

Comments
 (0)