66from collections .abc import Generator , Iterable , Sequence
77from contextlib import AbstractContextManager , contextmanager
88from functools import partial
9- from typing import TYPE_CHECKING , Any , Callable , Literal , Optional , Protocol , Union
9+ from typing import TYPE_CHECKING , Protocol
1010
1111import pytest
1212
1616
1717
1818if TYPE_CHECKING :
19+ from typing import Any , Callable , Literal , Optional , Union
20+
1921 import django
2022 import django .test
2123
2224 from . import DjangoDbBlocker
25+ from .django_compat import _User , _UserModel
2326
24-
25- _DjangoDbDatabases = Optional [Union [Literal ["__all__" ], Iterable [str ]]]
26- _DjangoDbAvailableApps = Optional [list [str ]]
27- # transaction, reset_sequences, databases, serialized_rollback, available_apps
28- _DjangoDb = tuple [bool , bool , _DjangoDbDatabases , bool , _DjangoDbAvailableApps ]
27+ _DjangoDbDatabases = Optional [Union [Literal ["__all__" ], Iterable [str ]]]
28+ _DjangoDbAvailableApps = Optional [list [str ]]
29+ # transaction, reset_sequences, databases, serialized_rollback, available_apps
30+ _DjangoDb = tuple [bool , bool , _DjangoDbDatabases , bool , _DjangoDbAvailableApps ]
2931
3032
3133__all__ = [
@@ -337,7 +339,7 @@ def __getitem__(self, item: str) -> None:
337339 settings .MIGRATION_MODULES = DisableMigrations ()
338340
339341 class MigrateSilentCommand (migrate .Command ):
340- def handle (self , * args , ** kwargs ) :
342+ def handle (self , * args : Any , ** kwargs : Any ) -> Any :
341343 kwargs ["verbosity" ] = 0
342344 return super ().handle (* args , ** kwargs )
343345
@@ -456,15 +458,15 @@ def async_client() -> django.test.AsyncClient:
456458
457459
458460@pytest .fixture
459- def django_user_model (db : None ):
461+ def django_user_model (db : None ) -> _UserModel :
460462 """The class of Django's user model."""
461463 from django .contrib .auth import get_user_model
462464
463- return get_user_model ()
465+ return get_user_model () # type: ignore[no-any-return]
464466
465467
466468@pytest .fixture
467- def django_username_field (django_user_model ) -> str :
469+ def django_username_field (django_user_model : _UserModel ) -> str :
468470 """The fieldname for the username used with Django's user model."""
469471 field : str = django_user_model .USERNAME_FIELD
470472 return field
@@ -473,9 +475,9 @@ def django_username_field(django_user_model) -> str:
473475@pytest .fixture
474476def admin_user (
475477 db : None ,
476- django_user_model ,
478+ django_user_model : _User ,
477479 django_username_field : str ,
478- ):
480+ ) -> _User :
479481 """A Django admin user.
480482
481483 This uses an existing user with username "admin", or creates a new one with
@@ -504,7 +506,7 @@ def admin_user(
504506@pytest .fixture
505507def admin_client (
506508 db : None ,
507- admin_user ,
509+ admin_user : _User ,
508510) -> django .test .Client :
509511 """A Django test client logged in as an admin user."""
510512 from django .test import Client
@@ -550,14 +552,14 @@ def __delattr__(self, attr: str) -> None:
550552
551553 self ._to_restore .append (override )
552554
553- def __setattr__ (self , attr : str , value ) -> None :
555+ def __setattr__ (self , attr : str , value : Any ) -> None :
554556 from django .test import override_settings
555557
556558 override = override_settings (** {attr : value })
557559 override .enable ()
558560 self ._to_restore .append (override )
559561
560- def __getattr__ (self , attr : str ):
562+ def __getattr__ (self , attr : str ) -> Any :
561563 from django .conf import settings
562564
563565 return getattr (settings , attr )
@@ -570,7 +572,7 @@ def finalize(self) -> None:
570572
571573
572574@pytest .fixture
573- def settings ():
575+ def settings () -> Generator [ SettingsWrapper , None , None ] :
574576 """A Django settings object which restores changes after the testrun"""
575577 skip_if_no_django ()
576578
@@ -580,7 +582,9 @@ def settings():
580582
581583
582584@pytest .fixture (scope = "session" )
583- def live_server (request : pytest .FixtureRequest ):
585+ def live_server (
586+ request : pytest .FixtureRequest ,
587+ ) -> Generator [live_server_helper .LiveServer , None , None ]:
584588 """Run a live Django server in the background during tests
585589
586590 The address the server is started from is taken from the
0 commit comments