@@ -220,7 +220,7 @@ def find_django_path(args) -> pathlib.Path | None:
220
220
return PROJECT_NOT_FOUND
221
221
222
222
223
- def _setup_django () -> None :
223
+ def _setup_django (config : pytest . Config ) -> None :
224
224
if "django" not in sys .modules :
225
225
return
226
226
@@ -235,7 +235,8 @@ def _setup_django() -> None:
235
235
if not django .apps .apps .ready :
236
236
django .setup ()
237
237
238
- _blocking_manager .block ()
238
+ blocking_manager = config .stash [blocking_manager_key ]
239
+ blocking_manager .block ()
239
240
240
241
241
242
def _get_boolean_value (
@@ -354,14 +355,16 @@ def _get_option_with_source(
354
355
with _handle_import_error (_django_project_scan_outcome ):
355
356
dj_settings .DATABASES # noqa: B018
356
357
357
- _setup_django ()
358
+ early_config .stash [blocking_manager_key ] = DjangoDbBlocker (_ispytest = True )
359
+
360
+ _setup_django (early_config )
358
361
359
362
360
363
@pytest .hookimpl (trylast = True )
361
- def pytest_configure () -> None :
364
+ def pytest_configure (config : pytest . Config ) -> None :
362
365
# Allow Django settings to be configured in a user pytest_configure call,
363
366
# but make sure we call django.setup()
364
- _setup_django ()
367
+ _setup_django (config )
365
368
366
369
367
370
@pytest .hookimpl ()
@@ -478,7 +481,7 @@ def django_test_environment(request: pytest.FixtureRequest) -> Generator[None, N
478
481
we need to follow this model.
479
482
"""
480
483
if django_settings_is_configured ():
481
- _setup_django ()
484
+ _setup_django (request . config )
482
485
from django .test .utils import setup_test_environment , teardown_test_environment
483
486
484
487
debug_ini = request .config .getini ("django_debug_mode" )
@@ -496,7 +499,7 @@ def django_test_environment(request: pytest.FixtureRequest) -> Generator[None, N
496
499
497
500
498
501
@pytest .fixture (scope = "session" )
499
- def django_db_blocker () -> DjangoDbBlocker | None :
502
+ def django_db_blocker (request : pytest . FixtureRequest ) -> DjangoDbBlocker | None :
500
503
"""Wrapper around Django's database access.
501
504
502
505
This object can be used to re-enable database access. This fixture is used
@@ -512,7 +515,8 @@ def django_db_blocker() -> DjangoDbBlocker | None:
512
515
if not django_settings_is_configured ():
513
516
return None
514
517
515
- return _blocking_manager
518
+ blocking_manager = request .config .stash [blocking_manager_key ]
519
+ return blocking_manager
516
520
517
521
518
522
@pytest .fixture (autouse = True )
@@ -813,7 +817,8 @@ def restore(self) -> None:
813
817
self ._dj_db_wrapper .ensure_connection = self ._history .pop ()
814
818
815
819
816
- _blocking_manager = DjangoDbBlocker (_ispytest = True )
820
+ # On Config.stash.
821
+ blocking_manager_key = pytest .StashKey [DjangoDbBlocker ]()
817
822
818
823
819
824
def validate_urls (marker ) -> list [str ]:
0 commit comments