"""Import helpers backed by a module-level dotted-path cache.
`import_class_cached` memoises dotted-path lookups across reloads of the
framework settings. `perform_import` wraps the cache with the dotted-path
conversion rules used for future `IMPORT_STRINGS` entries. The cache is
cleared by `NextFrameworkSettings.reload`.
"""
from __future__ import annotations
from typing import Any
from django.utils.module_loading import import_string
IMPORT_STRINGS: frozenset[str] = frozenset()
_import_class_cache: dict[str, type[Any]] = {}
[docs]
def import_class_cached(dotted_path: str) -> type[Any]:
"""Import a class by dotted path and cache it until the cache is cleared."""
if dotted_path not in _import_class_cache:
_import_class_cache[dotted_path] = import_string(dotted_path)
return _import_class_cache[dotted_path]
[docs]
def clear_import_cache() -> None:
"""Drop every cached dotted-path import."""
_import_class_cache.clear()