11"""All pytest-django fixtures"""
2- from typing import List , Any
2+ from typing import Any , Generator , List
33import os
44from contextlib import contextmanager
55from functools import partial
1010from .django_compat import is_django_unittest
1111from .lazy_django import skip_if_no_django
1212
13+ TYPE_CHECKING = False
14+ if TYPE_CHECKING :
15+ import django
16+
17+
1318__all__ = [
1419 "django_db_setup" ,
1520 "db" ,
3237
3338
3439@pytest .fixture (scope = "session" )
35- def django_db_modify_db_settings_tox_suffix ():
40+ def django_db_modify_db_settings_tox_suffix () -> None :
3641 skip_if_no_django ()
3742
3843 tox_environment = os .getenv ("TOX_PARALLEL_ENV" )
@@ -42,7 +47,7 @@ def django_db_modify_db_settings_tox_suffix():
4247
4348
4449@pytest .fixture (scope = "session" )
45- def django_db_modify_db_settings_xdist_suffix (request ):
50+ def django_db_modify_db_settings_xdist_suffix (request ) -> None :
4651 skip_if_no_django ()
4752
4853 xdist_suffix = getattr (request .config , "workerinput" , {}).get ("workerid" )
@@ -53,42 +58,44 @@ def django_db_modify_db_settings_xdist_suffix(request):
5358
5459@pytest .fixture (scope = "session" )
5560def django_db_modify_db_settings_parallel_suffix (
56- django_db_modify_db_settings_tox_suffix ,
57- django_db_modify_db_settings_xdist_suffix ,
58- ):
61+ django_db_modify_db_settings_tox_suffix : None ,
62+ django_db_modify_db_settings_xdist_suffix : None ,
63+ ) -> None :
5964 skip_if_no_django ()
6065
6166
6267@pytest .fixture (scope = "session" )
63- def django_db_modify_db_settings (django_db_modify_db_settings_parallel_suffix ):
68+ def django_db_modify_db_settings (
69+ django_db_modify_db_settings_parallel_suffix : None ,
70+ ) -> None :
6471 skip_if_no_django ()
6572
6673
6774@pytest .fixture (scope = "session" )
68- def django_db_use_migrations (request ):
75+ def django_db_use_migrations (request ) -> bool :
6976 return not request .config .getvalue ("nomigrations" )
7077
7178
7279@pytest .fixture (scope = "session" )
73- def django_db_keepdb (request ):
80+ def django_db_keepdb (request ) -> bool :
7481 return request .config .getvalue ("reuse_db" )
7582
7683
7784@pytest .fixture (scope = "session" )
78- def django_db_createdb (request ):
85+ def django_db_createdb (request ) -> bool :
7986 return request .config .getvalue ("create_db" )
8087
8188
8289@pytest .fixture (scope = "session" )
8390def django_db_setup (
8491 request ,
85- django_test_environment ,
92+ django_test_environment : None ,
8693 django_db_blocker ,
87- django_db_use_migrations ,
88- django_db_keepdb ,
89- django_db_createdb ,
90- django_db_modify_db_settings ,
91- ):
94+ django_db_use_migrations : bool ,
95+ django_db_keepdb : bool ,
96+ django_db_createdb : bool ,
97+ django_db_modify_db_settings : None ,
98+ ) -> None :
9299 """Top level fixture to ensure test databases are available"""
93100 from django .test .utils import setup_databases , teardown_databases
94101
@@ -107,7 +114,7 @@ def django_db_setup(
107114 ** setup_databases_args
108115 )
109116
110- def teardown_database ():
117+ def teardown_database () -> None :
111118 with django_db_blocker .unblock ():
112119 try :
113120 teardown_databases (db_cfg , verbosity = request .config .option .verbose )
@@ -123,8 +130,11 @@ def teardown_database():
123130
124131
125132def _django_db_fixture_helper (
126- request , django_db_blocker , transactional = False , reset_sequences = False
127- ):
133+ request ,
134+ django_db_blocker ,
135+ transactional : bool = False ,
136+ reset_sequences : bool = False ,
137+ ) -> None :
128138 if is_django_unittest (request ):
129139 return
130140
@@ -149,7 +159,7 @@ class ResetSequenceTestCase(django_case):
149159 from django .db import transaction
150160 transaction .Atomic ._ensure_durability = False
151161
152- def reset_durability ():
162+ def reset_durability () -> None :
153163 transaction .Atomic ._ensure_durability = True
154164 request .addfinalizer (reset_durability )
155165
@@ -158,15 +168,15 @@ def reset_durability():
158168 request .addfinalizer (test_case ._post_teardown )
159169
160170
161- def _disable_native_migrations ():
171+ def _disable_native_migrations () -> None :
162172 from django .conf import settings
163173 from django .core .management .commands import migrate
164174
165175 class DisableMigrations :
166- def __contains__ (self , item ) :
176+ def __contains__ (self , item : str ) -> bool :
167177 return True
168178
169- def __getitem__ (self , item ) :
179+ def __getitem__ (self , item : str ) -> None :
170180 return None
171181
172182 settings .MIGRATION_MODULES = DisableMigrations ()
@@ -179,7 +189,7 @@ def handle(self, *args, **kwargs):
179189 migrate .Command = MigrateSilentCommand
180190
181191
182- def _set_suffix_to_test_databases (suffix ) :
192+ def _set_suffix_to_test_databases (suffix : str ) -> None :
183193 from django .conf import settings
184194
185195 for db_settings in settings .DATABASES .values ():
@@ -201,7 +211,11 @@ def _set_suffix_to_test_databases(suffix):
201211
202212
203213@pytest .fixture (scope = "function" )
204- def db (request , django_db_setup , django_db_blocker ):
214+ def db (
215+ request ,
216+ django_db_setup : None ,
217+ django_db_blocker ,
218+ ) -> None :
205219 """Require a django test database.
206220
207221 This database will be setup with the default fixtures and will have
@@ -227,7 +241,11 @@ def db(request, django_db_setup, django_db_blocker):
227241
228242
229243@pytest .fixture (scope = "function" )
230- def transactional_db (request , django_db_setup , django_db_blocker ):
244+ def transactional_db (
245+ request ,
246+ django_db_setup : None ,
247+ django_db_blocker ,
248+ ) -> None :
231249 """Require a django test database with transaction support.
232250
233251 This will re-initialise the django database for each test and is
@@ -246,7 +264,11 @@ def transactional_db(request, django_db_setup, django_db_blocker):
246264
247265
248266@pytest .fixture (scope = "function" )
249- def django_db_reset_sequences (request , django_db_setup , django_db_blocker ):
267+ def django_db_reset_sequences (
268+ request ,
269+ django_db_setup : None ,
270+ django_db_blocker ,
271+ ) -> None :
250272 """Require a transactional test database with sequence reset support.
251273
252274 This behaves like the ``transactional_db`` fixture, with the addition
@@ -264,7 +286,7 @@ def django_db_reset_sequences(request, django_db_setup, django_db_blocker):
264286
265287
266288@pytest .fixture ()
267- def client ():
289+ def client () -> "django.test.client.Client" :
268290 """A Django test client instance."""
269291 skip_if_no_django ()
270292
@@ -274,7 +296,7 @@ def client():
274296
275297
276298@pytest .fixture ()
277- def async_client ():
299+ def async_client () -> "django.test.client.AsyncClient" :
278300 """A Django test async client instance."""
279301 skip_if_no_django ()
280302
@@ -284,21 +306,25 @@ def async_client():
284306
285307
286308@pytest .fixture ()
287- def django_user_model (db ):
309+ def django_user_model (db : None ):
288310 """The class of Django's user model."""
289311 from django .contrib .auth import get_user_model
290312
291313 return get_user_model ()
292314
293315
294316@pytest .fixture ()
295- def django_username_field (django_user_model ):
317+ def django_username_field (django_user_model ) -> str :
296318 """The fieldname for the username used with Django's user model."""
297319 return django_user_model .USERNAME_FIELD
298320
299321
300322@pytest .fixture ()
301- def admin_user (db , django_user_model , django_username_field ):
323+ def admin_user (
324+ db : None ,
325+ django_user_model ,
326+ django_username_field : str ,
327+ ):
302328 """A Django admin user.
303329
304330 This uses an existing user with username "admin", or creates a new one with
@@ -325,7 +351,10 @@ def admin_user(db, django_user_model, django_username_field):
325351
326352
327353@pytest .fixture ()
328- def admin_client (db , admin_user ):
354+ def admin_client (
355+ db : None ,
356+ admin_user ,
357+ ) -> "django.test.client.Client" :
329358 """A Django test client logged in as an admin user."""
330359 from django .test .client import Client
331360
@@ -335,7 +364,7 @@ def admin_client(db, admin_user):
335364
336365
337366@pytest .fixture ()
338- def rf ():
367+ def rf () -> "django.test.client.RequestFactory" :
339368 """RequestFactory instance"""
340369 skip_if_no_django ()
341370
@@ -345,7 +374,7 @@ def rf():
345374
346375
347376@pytest .fixture ()
348- def async_rf ():
377+ def async_rf () -> "django.test.client.AsyncRequestFactory" :
349378 """AsyncRequestFactory instance"""
350379 skip_if_no_django ()
351380
@@ -357,7 +386,7 @@ def async_rf():
357386class SettingsWrapper :
358387 _to_restore = [] # type: List[Any]
359388
360- def __delattr__ (self , attr ) :
389+ def __delattr__ (self , attr : str ) -> None :
361390 from django .test import override_settings
362391
363392 override = override_settings ()
@@ -368,19 +397,19 @@ def __delattr__(self, attr):
368397
369398 self ._to_restore .append (override )
370399
371- def __setattr__ (self , attr , value ):
400+ def __setattr__ (self , attr : str , value ) -> None :
372401 from django .test import override_settings
373402
374403 override = override_settings (** {attr : value })
375404 override .enable ()
376405 self ._to_restore .append (override )
377406
378- def __getattr__ (self , item ):
407+ def __getattr__ (self , attr : str ):
379408 from django .conf import settings
380409
381- return getattr (settings , item )
410+ return getattr (settings , attr )
382411
383- def finalize (self ):
412+ def finalize (self ) -> None :
384413 for override in reversed (self ._to_restore ):
385414 override .disable ()
386415
@@ -429,7 +458,7 @@ def live_server(request):
429458
430459
431460@pytest .fixture (autouse = True , scope = "function" )
432- def _live_server_helper (request ):
461+ def _live_server_helper (request ) -> None :
433462 """Helper to make live_server work, internal to pytest-django.
434463
435464 This helper will dynamically request the transactional_db fixture
@@ -455,14 +484,22 @@ def _live_server_helper(request):
455484
456485
457486@contextmanager
458- def _assert_num_queries (config , num , exact = True , connection = None , info = None ):
487+ def _assert_num_queries (
488+ config ,
489+ num : int ,
490+ exact : bool = True ,
491+ connection = None ,
492+ info = None ,
493+ ) -> Generator ["django.test.utils.CaptureQueriesContext" , None , None ]:
459494 from django .test .utils import CaptureQueriesContext
460495
461496 if connection is None :
462- from django .db import connection
497+ from django .db import connection as conn
498+ else :
499+ conn = connection
463500
464501 verbose = config .getoption ("verbose" ) > 0
465- with CaptureQueriesContext (connection ) as context :
502+ with CaptureQueriesContext (conn ) as context :
466503 yield context
467504 num_performed = len (context )
468505 if exact :
0 commit comments