Coverage for src / vivarium / _compat / _compat.py: 69%
52 statements
« prev ^ index » next coverage.py v7.14.0, created at 2026-05-14 20:05 +0000
« prev ^ index » next coverage.py v7.14.0, created at 2026-05-14 20:05 +0000
1"""Backward-compatible import redirects for the vivarium monorepo migration.
3Intercepts old-style imports (e.g. ``import layered_config_tree``,
4``from vivarium_public_health.disease import DiseaseModel``) and transparently
5redirects them to the new ``vivarium.*`` namespace, emitting a DeprecationWarning.
7Activated at interpreter startup via ``vivarium_compat.pth`` so the hook is in
8place before any user code runs.
10To add a redirect when a package migrates, add an entry to ``_REDIRECTS`` and
11bump the ``vivarium-compat`` version in ``pyproject.toml``. Entries are safe
12to add *before* the target package is released: if the new module isn't
13importable, the hook falls back to letting the old on-disk package resolve
14normally, so installations during the transition window aren't broken.
16Remove this module once all downstream packages have released versions that use
17the new import paths and the deprecation period has ended.
18"""
20import importlib
21import importlib.abc
22import importlib.machinery
23import sys
24import warnings
25from types import ModuleType
27# Old import root -> new import root.
28# Entries here are safe to populate ahead of the target package's release:
29# if the new target isn't installed yet, the hook falls back to the old
30# package's normal on-disk location (so existing installations during the
31# transition window keep working).
32_REDIRECTS: dict[str, str] = {
33 # Renamed top-level packages
34 "vivarium_profiling": "vivarium.profiling",
35 "layered_config_tree": "vivarium.config_tree",
36 # "vivarium_public_health": "vivarium.public_health",
37 # "vivarium_cluster_tools": "vivarium.cluster_tools",
38 # "vivarium_testing_utils": "vivarium.testing_utils",
39 # "vivarium.examples": "vivarium.core.examples",
40 # "vivarium.framework": "vivarium.core.framework",
41 # "vivarium.interface": "vivarium.core.interface",
42 # "vivarium.component": "vivarium.core.component",
43 # "vivarium.exceptions": "vivarium.core.exceptions",
44 # "vivarium.manager": "vivarium.core.manager",
45 # "vivarium.testing_utilities": "vivarium.core.testing_utilities",
46 # "vivarium.types": "vivarium.core.types",
47 # "vivarium_helpers": "vivarium.helpers",
48 # "risk_distributions": "vivarium.risk_distributions",
49 # "gbd_mapping": "vivarium.gbd_mapping",
50}
52# Tracks which old names are currently being resolved to prevent infinite
53# recursion if a redirect target somehow re-triggers the same old-name import.
54_resolving: set[str] = set()
57def _match(fullname: str) -> tuple[str, str] | None:
58 """Return (old_prefix, new_prefix) if fullname matches a redirect, else None.
60 Longer prefixes take precedence so more-specific entries win over broader ones.
61 """
62 for old, new in sorted(_REDIRECTS.items(), key=lambda x: -len(x[0])):
63 if fullname == old or fullname.startswith(old + "."):
64 return old, new
65 return None
68class _CompatFinder(importlib.abc.MetaPathFinder):
69 """Meta-path finder that redirects deprecated import paths to new locations."""
71 def find_spec(
72 self,
73 fullname: str,
74 path: object,
75 target: ModuleType | None = None,
76 ) -> importlib.machinery.ModuleSpec | None:
77 match = _match(fullname)
78 if match is None:
79 return None
81 old_prefix, new_prefix = match
82 new_name = new_prefix + fullname[len(old_prefix) :]
84 # Only warn on first import. After exec_module runs, sys.modules[fullname]
85 # is set to the real module, so subsequent imports return it directly without
86 # ever reaching find_spec again. This branch fires only in unusual re-entry
87 # scenarios (e.g. reload()).
88 if fullname not in sys.modules:
89 # stacklevel=2 points into importlib internals rather than the caller's
90 # import statement - the exact depth is CPython-version-dependent.
91 # The warning message itself is the actionable part.
92 warnings.warn(
93 f"'{fullname}' has moved to '{new_name}'. "
94 "Update your imports. This redirect will be removed in a future release.",
95 DeprecationWarning,
96 stacklevel=2,
97 )
98 # submodule_search_locations is intentionally not set on the spec. CPython's
99 # import machinery calls exec_module before checking __path__ on any child import,
100 # and exec_module replaces sys.modules[fullname] with the real module (which already
101 # has the correct __path__). Setting it to [] would be a no-op at best and misleading
102 # at worst since we don't know at spec-creation time whether the target is a package.
103 return importlib.machinery.ModuleSpec(
104 fullname, _CompatLoader(fullname, new_name)
105 )
108class _CompatLoader(importlib.abc.Loader):
109 """Loads the real module at the new location and aliases it under the old name."""
111 def __init__(self, old_name: str, new_name: str) -> None:
112 self._old_name = old_name
113 self._new_name = new_name
115 def exec_module(self, module: ModuleType) -> None:
116 if self._old_name in _resolving:
117 raise ImportError(
118 f"Circular redirect detected: '{self._old_name}' -> '{self._new_name}'"
119 )
120 _resolving.add(self._old_name)
121 try:
122 try:
123 real = importlib.import_module(self._new_name)
124 except ModuleNotFoundError:
125 # FIXME: MIC-7100 Revert when all packages are migrated
126 # New target isn't installed. Fall back to the old name's actual
127 # on-disk location so installations during the transition window
128 # — old package still installed, new package not yet released —
129 # keep working. The DeprecationWarning was already emitted in
130 # find_spec(); users will see it whether or not the fallback fires.
131 real = _import_bypassing_compat(self._old_name)
132 # Register under the old name so subsequent imports hit sys.modules directly.
133 sys.modules[self._old_name] = real
134 # Defensive: covers the case where another import hook holds a direct
135 # reference to the placeholder module object rather than re-reading sys.modules.
136 module.__dict__.update(real.__dict__)
137 # __spec__.name will show the new name (e.g. "vivarium.config_tree"), not the
138 # old one. This is intentional: sys.modules[old_name] IS real, so its metadata
139 # correctly describes its actual location. Users inspecting __spec__ after
140 # migrating their imports will see the right thing.
141 module.__spec__ = real.__spec__
142 finally:
143 _resolving.discard(self._old_name)
146def _import_bypassing_compat(name: str) -> ModuleType:
147 """Import `name` without going through our compat finder.
149 Used to fall back to a name's real on-disk location when the redirect target
150 isn't installed. Temporarily removes our finder from sys.meta_path so the
151 standard PathFinder resolves the name directly. Also clears any placeholder
152 sys.modules entry the import system set when our find_spec returned a spec.
153 """
154 sys.modules.pop(name, None)
155 saved: list[tuple[int, _CompatFinder]] = [
156 (i, f) for i, f in enumerate(sys.meta_path) if isinstance(f, _CompatFinder)
157 ]
158 for _, f in saved:
159 sys.meta_path.remove(f)
160 try:
161 return importlib.import_module(name)
162 finally:
163 # Reinsert in original order so any other meta_path entries stay in place.
164 for i, f in saved:
165 sys.meta_path.insert(i, f)
168def install_compat_finder() -> None:
169 """Install the compat finder into sys.meta_path (idempotent)."""
170 if not any(isinstance(f, _CompatFinder) for f in sys.meta_path):
171 sys.meta_path.insert(0, _CompatFinder())