test: close Gate 3 tiny-runtime semantics

This commit is contained in:
Joseph Magly
2026-08-16 07:53:00 -04:00
parent 9683e0be4d
commit acc6b3b254
13 changed files with 638 additions and 43 deletions
+97 -1
View File
@@ -3,8 +3,10 @@
from __future__ import annotations
import builtins
import runpy
import sys
from pathlib import Path
from types import SimpleNamespace
from types import ModuleType, SimpleNamespace
from unittest.mock import MagicMock, Mock
import pytest
@@ -13,6 +15,100 @@ import torch
from obliteratus.models import loader
def _execute_loader_source() -> dict:
return runpy.run_path(str(Path(loader.__file__).resolve()), run_name="_loader_contract_probe")
def test_compatibility_shims_tolerate_independent_missing_or_broken_imports(monkeypatch):
original_import = builtins.__import__
failure_keys = {
("transformers", ("AutoModelForImageTextToText",)): ImportError,
("transformers.utils", ("output_capturing",)): ImportError,
("transformers.utils.import_utils", ()): RuntimeError,
("transformers.utils", ()): RuntimeError,
("transformers.pytorch_utils", ()): RuntimeError,
("transformers.generation_utils", ()): ModuleNotFoundError,
("transformers.generation", ()): RuntimeError,
("transformers.deepspeed", ()): ModuleNotFoundError,
("transformers.integrations.deepspeed", ()): RuntimeError,
("transformers.cache_utils", ("DynamicCache",)): RuntimeError,
("transformers.generation.logits_process", ()): RuntimeError,
("transformers.processing_utils", ()): RuntimeError,
("transformers.file_utils", ()): RuntimeError,
}
def rejecting_import(name, globals=None, locals=None, fromlist=(), level=0):
exception = failure_keys.get((name, tuple(fromlist or ())))
if exception is not None:
raise exception(f"injected loader compatibility failure for {name}")
return original_import(name, globals, locals, fromlist, level)
monkeypatch.setattr(builtins, "__import__", rejecting_import)
namespace = _execute_loader_source()
assert namespace["TASK_MODEL_MAP"]
assert namespace["AutoModelForImageTextToText"] is None
def test_compatibility_shims_tolerate_missing_generic_module(monkeypatch):
original_import = builtins.__import__
def rejecting_import(name, globals=None, locals=None, fromlist=(), level=0):
if name == "transformers.utils.generic":
raise RuntimeError("generic module unavailable")
return original_import(name, globals, locals, fromlist, level)
monkeypatch.setattr(builtins, "__import__", rejecting_import)
assert _execute_loader_source()["TASK_MODEL_MAP"]
def test_compatibility_shims_tolerate_top_level_utils_patch_failure(monkeypatch):
original_import = builtins.__import__
def rejecting_import(name, globals=None, locals=None, fromlist=(), level=0):
if name == "transformers.utils" and not fromlist:
raise RuntimeError("top-level utils module unavailable")
return original_import(name, globals, locals, fromlist, level)
monkeypatch.setattr(builtins, "__import__", rejecting_import)
assert _execute_loader_source()["TASK_MODEL_MAP"]
def test_dynamic_cache_compatibility_alias_is_installed_when_needed(monkeypatch):
original_import = builtins.__import__
class LegacyDynamicCache:
def get_max_cache_shape(self):
return 7
cache_module = SimpleNamespace(DynamicCache=LegacyDynamicCache)
def cache_import(name, globals=None, locals=None, fromlist=(), level=0):
if name == "transformers.cache_utils" and tuple(fromlist or ()) == ("DynamicCache",):
return cache_module
return original_import(name, globals, locals, fromlist, level)
monkeypatch.setattr(builtins, "__import__", cache_import)
assert _execute_loader_source()["TASK_MODEL_MAP"]
assert LegacyDynamicCache().get_max_length() == 7
def test_compatibility_shims_tolerate_rejected_generic_attribute_patch(monkeypatch):
import transformers.utils as transformers_utils
class RejectWorkingDirectory(ModuleType):
def __setattr__(self, name, value):
if name == "working_or_temp_dir":
raise RuntimeError("read-only compatibility module")
super().__setattr__(name, value)
fake_generic = RejectWorkingDirectory("transformers.utils.generic")
monkeypatch.setitem(sys.modules, "transformers.utils.generic", fake_generic)
monkeypatch.setattr(transformers_utils, "generic", fake_generic)
assert _execute_loader_source()["TASK_MODEL_MAP"]
def _config(**overrides):
values = {
"model_type": "gpt2",