Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Tests: stub the new num_proc helpers in the no-torch venv, and read with utf-8 in the metadata cache test (#16386)

Daniel Han277f84df81d2
diff --git a/tests/python/test_studio_import_no_torch.py b/tests/python/test_studio_import_no_torch.py
index 3cc4e03f544..f6ac05b4d8a 100644
--- a/tests/python/test_studio_import_no_torch.py
+++ b/tests/python/test_studio_import_no_torch.py
@@ -4,6 +4,7 @@ from __future__ import annotations
import ast
import os
+import re
import shutil
import subprocess
import sys
@@ -348,6 +349,44 @@ class TestFormatConversionAST:
)
+def test_the_hardware_stub_covers_what_format_conversion_imports():
+ """The venv tests below replace utils.hardware with a stub. A name format_conversion.py
+ imports from it that the stub lacks only surfaces as an ImportError inside the venv,
+ after the venv build, so check the two lists against each other here."""
+ imported = set()
+ for node in ast.walk(ast.parse(FORMAT_CONVERSION.read_text(encoding = "utf-8"))):
+ if isinstance(node, ast.ImportFrom) and node.module == "utils.hardware":
+ imported.update(alias.name for alias in node.names)
+ assert imported, "format_conversion.py no longer imports from utils.hardware; drop the stubs"
+ stubbed_per_test = [
+ set(re.findall(r"\bhardware_mod\.(\w+)\s*=", text)) for text in _venv_test_sources()
+ ]
+ assert stubbed_per_test, "no utils.hardware stub found in TestFormatConversionNoTorchVenv"
+ for stubbed in stubbed_per_test:
+ assert imported <= stubbed, f"utils.hardware stub lacks {sorted(imported - stubbed)}"
+
+
+def _venv_test_sources() -> list:
+ """The child scripts of TestFormatConversionNoTorchVenv that stub utils.hardware, as text."""
+ tree = ast.parse(Path(__file__).read_text(encoding = "utf-8"))
+ out = []
+ for node in tree.body:
+ if isinstance(node, ast.ClassDef) and node.name == "TestFormatConversionNoTorchVenv":
+ for call in ast.walk(node):
+ if (
+ isinstance(call, ast.Call)
+ and getattr(call.func, "attr", "") == "dedent"
+ and call.args
+ and isinstance(call.args[0], ast.JoinedStr)
+ ):
+ text = "".join(
+ part.value for part in call.args[0].values if isinstance(part, ast.Constant)
+ )
+ if "hardware_mod" in text:
+ out.append(text)
+ return out
+
+
class TestFormatConversionNoTorchVenv:
"""Run format_conversion.py functions in a no-torch venv."""
@@ -378,6 +417,9 @@ class TestFormatConversionNoTorchVenv:
utils_mod = types.ModuleType('utils')
hardware_mod = types.ModuleType('utils.hardware')
hardware_mod.dataset_map_num_proc = lambda n=None: 1
+ # In process, as the real one answers for an in-memory dataset with no cache files.
+ hardware_mod.light_map_num_proc = lambda dataset, desired=None: None
+ hardware_mod.safe_thread_num_proc = lambda desired=None: 1
utils_mod.hardware = hardware_mod
sys.modules['utils'] = utils_mod
sys.modules['utils.hardware'] = hardware_mod
@@ -441,6 +483,9 @@ class TestFormatConversionNoTorchVenv:
utils_mod = types.ModuleType('utils')
hardware_mod = types.ModuleType('utils.hardware')
hardware_mod.dataset_map_num_proc = lambda n=None: 1
+ # In process, as the real one answers for an in-memory dataset with no cache files.
+ hardware_mod.light_map_num_proc = lambda dataset, desired=None: None
+ hardware_mod.safe_thread_num_proc = lambda desired=None: 1
utils_mod.hardware = hardware_mod
sys.modules['utils'] = utils_mod
sys.modules['utils.hardware'] = hardware_mod
diff --git a/tests/test_import_metadata_cache.py b/tests/test_import_metadata_cache.py
index f40b87ce317..605fb06a013 100644
--- a/tests/test_import_metadata_cache.py
+++ b/tests/test_import_metadata_cache.py
@@ -294,7 +294,7 @@ def test_unreadable_entries_do_not_break_it(cache_module, counted, monkeypatch):
def test_helper_is_stdlib_only():
- tree = ast.parse((_UNSLOTH / "_import_metadata_cache.py").read_text())
+ tree = ast.parse((_UNSLOTH / "_import_metadata_cache.py").read_text(encoding = "utf-8"))
imported = set()
for node in ast.walk(tree):
if isinstance(node, ast.Import):
@@ -306,7 +306,7 @@ def test_helper_is_stdlib_only():
def test_init_installs_first_and_removes_last():
- source = (_UNSLOTH / "__init__.py").read_text()
+ source = (_UNSLOTH / "__init__.py").read_text(encoding = "utf-8")
tree = ast.parse(source)
calls = [
node.func.id