fix djc_test is not happy with ComponentsSettings

- Make it Python 3.8 compatible

Fix #1369
This commit is contained in:
Pavel Pančocha 2025-09-10 13:49:22 +02:00
parent 927ba0c61c
commit f0112f9424
No known key found for this signature in database

View file

@ -404,32 +404,28 @@ def djc_test(
return decorator return decorator
def _merge_django_settings( def _merge_django_settings(
django_settings: Optional[Mapping[str, Any]] = None, django_settings: Optional[Mapping[str, Any]] = None,
components_settings: Optional[Mapping[str, Any] | ComponentsSettings] = None, components_settings: Optional[Union[Mapping[str, Any], "ComponentsSettings"]] = None,
) -> dict[str, Any]: ) -> Dict[str, Any]:
""" """
Merge settings such that the fields in the `COMPONENTS` setting are merged. Merge settings such that the fields in the `COMPONENTS` setting are merged.
Use components_settings to override fields in the django COMPONENTS setting. Use components_settings to override fields in the django COMPONENTS setting.
""" """
# Start from existing django_settings (mapping expected from caller) merged_settings: Dict[str, Any] = dict(django_settings or {})
merged_settings: dict[str, Any] = dict(django_settings or {})
defaults = _components_to_mapping(_django_settings.COMPONENTS if _django_settings.configured else {}) defaults = _components_to_mapping(_django_settings.COMPONENTS if _django_settings.configured else {})
current = _components_to_mapping(merged_settings.get("COMPONENTS")) current = _components_to_mapping(merged_settings.get("COMPONENTS"))
overrides = _components_to_mapping(components_settings) overrides = _components_to_mapping(components_settings)
merged_settings["COMPONENTS"] = { merged_settings["COMPONENTS"] = {**defaults, **current, **overrides}
**defaults,
**current,
**overrides,
}
return merged_settings return merged_settings
def _components_to_mapping( def _components_to_mapping(
value: Optional[Mapping[str, Any] | ComponentsSettings], value: Optional[Union[Mapping[str, Any], "ComponentsSettings"]],
) -> dict[str, Any]: ) -> Dict[str, Any]:
if value is None: if value is None:
return {} return {}
if isinstance(value, Mapping): if isinstance(value, Mapping):
@ -438,7 +434,6 @@ def _components_to_mapping(
return dict(value._asdict()) return dict(value._asdict())
raise TypeError("COMPONENTS must be a mapping or ComponentsSettings") raise TypeError("COMPONENTS must be a mapping or ComponentsSettings")
def _setup_djc_global_state( def _setup_djc_global_state(
gen_id_patcher: GenIdPatcher, gen_id_patcher: GenIdPatcher,
csrf_token_patcher: CsrfTokenPatcher, csrf_token_patcher: CsrfTokenPatcher,