Skip to content

Commit f7e0058

Browse files
committed
Stop using mutable global _report_header
1 parent 0612297 commit f7e0058

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

pytest_django/plugin.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@
5757
CONFIGURATION_ENV = "DJANGO_CONFIGURATION"
5858
INVALID_TEMPLATE_VARS_ENV = "FAIL_INVALID_TEMPLATE_VARS"
5959

60-
_report_header = []
61-
6260

6361
# ############### pytest hooks ################
6462

@@ -257,6 +255,9 @@ def _get_boolean_value(
257255
)
258256

259257

258+
report_header_key = pytest.StashKey[List[str]]()
259+
260+
260261
@pytest.hookimpl()
261262
def pytest_load_initial_conftests(
262263
early_config: pytest.Config,
@@ -330,12 +331,15 @@ def _get_option_with_source(
330331
ds, ds_source = _get_option_with_source(options.ds, SETTINGS_MODULE_ENV)
331332
dc, dc_source = _get_option_with_source(options.dc, CONFIGURATION_ENV)
332333

334+
report_header: List[str] = []
335+
early_config.stash[report_header_key] = report_header
336+
333337
if ds:
334-
_report_header.append(f"settings: {ds} (from {ds_source})")
338+
report_header.append(f"settings: {ds} (from {ds_source})")
335339
os.environ[SETTINGS_MODULE_ENV] = ds
336340

337341
if dc:
338-
_report_header.append(f"configuration: {dc} (from {dc_source})")
342+
report_header.append(f"configuration: {dc} (from {dc_source})")
339343
os.environ[CONFIGURATION_ENV] = dc
340344

341345
# Install the django-configurations importer
@@ -353,20 +357,21 @@ def _get_option_with_source(
353357
_setup_django()
354358

355359

356-
@pytest.hookimpl()
357-
def pytest_report_header() -> Optional[List[str]]:
358-
if _report_header:
359-
return ["django: " + ", ".join(_report_header)]
360-
return None
361-
362-
363360
@pytest.hookimpl(trylast=True)
364361
def pytest_configure() -> None:
365362
# Allow Django settings to be configured in a user pytest_configure call,
366363
# but make sure we call django.setup()
367364
_setup_django()
368365

369366

367+
@pytest.hookimpl()
368+
def pytest_report_header(config: pytest.Config) -> Optional[List[str]]:
369+
report_header = config.stash[report_header_key]
370+
if report_header:
371+
return ["django: " + ", ".join(report_header)]
372+
return None
373+
374+
370375
@pytest.hookimpl(tryfirst=True)
371376
def pytest_collection_modifyitems(items: List[pytest.Item]) -> None:
372377
# If Django is not configured we don't need to bother

0 commit comments

Comments
 (0)