fix(compression): drop cached deprecated exports on package reload

importlib.reload() reuses the module dict, so exports cached on first access survived a reload
and kept pointing at old objects. The package now pops them when it executes. Adds a reload
regression test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Assisted-by: claude-code:claude-opus-5-5
Headline-Session: 6e77a807-8dd4-4a77-a2fa-ddce6dc2515e
Headline-AI-Share: 71% strict / 71% weighted
This commit is contained in:
Tejas Chopra
2026-10-09 18:44:02 -07:00
co-authored by Claude Opus 5.5
parent 85361b78fb
commit 4a4306740c
2 changed files with 19 additions and 0 deletions
+6
View File
@@ -50,6 +50,12 @@ _DEPRECATED_EXPORTS: dict[str, tuple[str, str]] = {
"StructureMask": ("headroom.compression.masks", "StructureMask"),
}
# On importlib.reload() the module dict is reused; drop cached exports so the
# next access resolves (and warns) against the current submodules.
for _name in _DEPRECATED_EXPORTS:
globals().pop(_name, None)
del _name
def __getattr__(name: str) -> object:
try:
@@ -67,3 +67,16 @@ def test_detector_import_stays_quiet(monkeypatch: pytest.MonkeyPatch) -> None:
warnings.simplefilter("error", DeprecationWarning)
detector = importlib.import_module("headroom.compression.detector")
assert callable(detector._magika_available)
@pytest.mark.usefixtures("fresh")
def test_reload_drops_cached_exports(monkeypatch: pytest.MonkeyPatch) -> None:
import headroom.compression.detector as detector
with pytest.warns(DeprecationWarning):
old_content_type = compression.ContentType
monkeypatch.setattr(detector, "ContentType", type("ReloadedContentType", (), {}))
importlib.reload(compression)
with pytest.warns(DeprecationWarning):
assert compression.ContentType is detector.ContentType
assert compression.ContentType is not old_content_type