From 0c6bad23650a41eac31dfb6bb4b3616e29cda6c5 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Mon, 27 Jul 2026 09:23:09 +0000 Subject: [PATCH] Fix two tests that only fail in a full-suite run The 3.10 CI leg resolves PyAV 17, where av.container.OutputContainer is an immutable C type, so the no-libopus export test died on "cannot set 'add_stream' attribute of immutable type" before it asserted anything. Inject the refusal by wrapping the container av.open() returns instead; modules stay patchable on every build. Removing the injection makes the test fail again, so it still covers the branch it is named for. The Xet shim's degraded-path tests drop utils.hf_xet_fallback from sys.modules and import a throwaway copy. Restoring only the sys.modules entry left the utils package attribute bound to the throwaway, and the two disagreed for the rest of the process: a later monkeypatch of the dotted target patched one copy while the code under test imported the other, so the patch did nothing and test_fetch_te_prequant_only_reports_what_it_downloaded reached the real Hub and got a 401. Restore both bindings. --- studio/backend/tests/test_hf_xet_fallback.py | 26 +++++++++++++ studio/backend/tests/test_video_gallery.py | 41 ++++++++++++++------ 2 files changed, 56 insertions(+), 11 deletions(-) diff --git a/studio/backend/tests/test_hf_xet_fallback.py b/studio/backend/tests/test_hf_xet_fallback.py index 5e38796bf8..310cd6377f 100644 --- a/studio/backend/tests/test_hf_xet_fallback.py +++ b/studio/backend/tests/test_hf_xet_fallback.py @@ -44,6 +44,32 @@ import utils.hf_xet_fallback as xf DL_REPO, FILE = "ztest/xet-dl", "model-Q4_K_XL.gguf" +@pytest.fixture(autouse = True) +def _restore_shim_module_identity(): + """Put BOTH bindings of the shim back after every test in this file. + + The degraded-path tests below drop ``utils.hf_xet_fallback`` from ``sys.modules`` and import a + throwaway copy. Restoring only the ``sys.modules`` entry is not enough: the import machinery + also rebinds the module as an attribute of the ``utils`` PACKAGE, and that binding keeps + pointing at the throwaway. The two then disagree, and a later test in the same process + monkeypatches one copy (pytest resolves a dotted target through the package attribute) while + the code under test imports the other, so the patch silently does nothing and the real + downloader runs against the network. Caught by + tests/test_video_backend.py::test_fetch_te_prequant_only_reports_what_it_downloaded, which + reached the Hub and got a 401 when it ran after this file.""" + import utils as _utils_pkg + + original = sys.modules.get("utils.hf_xet_fallback") + original_attr = getattr(_utils_pkg, "hf_xet_fallback", None) + try: + yield + finally: + if original is not None: + sys.modules["utils.hf_xet_fallback"] = original + if original_attr is not None: + _utils_pkg.hf_xet_fallback = original_attr + + def _requires_shared(): if shared is None: pytest.skip("unsloth_zoo.hf_xet_fallback is not installed in this environment") diff --git a/studio/backend/tests/test_video_gallery.py b/studio/backend/tests/test_video_gallery.py index 85bbdc389b..91ab06f2f4 100644 --- a/studio/backend/tests/test_video_gallery.py +++ b/studio/backend/tests/test_video_gallery.py @@ -434,19 +434,38 @@ def test_webm_export_still_works_without_an_audio_encoder(monkeypatch): av = pytest.importorskip("av") import io - real_add_stream = av.container.OutputContainer.add_stream + # The refusal is injected by wrapping the container av.open() hands back, NOT by patching + # av.container.OutputContainer.add_stream: that is a C extension type, and on PyAV 17 (what the + # 3.10 CI leg resolves) setting an attribute on it raises "cannot set 'add_stream' attribute of + # immutable type". Modules stay patchable on every build. + real_open = av.open - def _no_opus( - self, - codec_name = None, - *args, - **kwargs, - ): - if codec_name == "libopus": - raise ValueError("unknown encoder 'libopus'") - return real_add_stream(self, codec_name, *args, **kwargs) + class _NoOpusContainer: + """Delegates to the real output container, but refuses the Opus encoder.""" - monkeypatch.setattr(av.container.OutputContainer, "add_stream", _no_opus) + def __init__(self, inner): + self._inner = inner + + def add_stream(self, codec_name = None, *args, **kwargs): + if codec_name == "libopus": + raise ValueError("unknown encoder 'libopus'") + return self._inner.add_stream(codec_name, *args, **kwargs) + + def __getattr__(self, name): + return getattr(self._inner, name) + + def __enter__(self): + self._inner.__enter__() + return self + + def __exit__(self, *exc): + return self._inner.__exit__(*exc) + + def _open(file, mode = "r", *args, **kwargs): + inner = real_open(file, mode, *args, **kwargs) + return _NoOpusContainer(inner) if mode == "w" else inner + + monkeypatch.setattr(av, "open", _open) record = gallery.save(_real_mp4_with_audio(), _meta()) webm = gallery.transcode(record["id"], "webm") assert webm is not None and webm[:4] == b"\x1a\x45\xdf\xa3"