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.

Studio tests: import the new test helpers top-level when a sibling imports the module that way (#17139)

Daniel Han2fcd260ae9fc
diff --git a/studio/backend/tests/test_diffusion_backend.py b/studio/backend/tests/test_diffusion_backend.py
index 539b6633866..3bce326cc99 100644
--- a/studio/backend/tests/test_diffusion_backend.py
+++ b/studio/backend/tests/test_diffusion_backend.py
@@ -18,9 +18,17 @@ from pathlib import Path
import pytest
-from ._sparse_file import extend_sparse
-
-from ._torchao_installed import needs_torchao
+# A sibling test may put this directory on sys.path and import this file top-level.
+try:
+ from ._sparse_file import extend_sparse
+except ImportError:
+ from _sparse_file import extend_sparse
+
+# A sibling test may put this directory on sys.path and import this file top-level.
+try:
+ from ._torchao_installed import needs_torchao
+except ImportError:
+ from _torchao_installed import needs_torchao
from core.inference.diffusion import (
DiffusionBackend,
diff --git a/studio/backend/tests/test_diffusion_pipeline_prequant.py b/studio/backend/tests/test_diffusion_pipeline_prequant.py
index ea06f4537cb..27872b71de0 100644
--- a/studio/backend/tests/test_diffusion_pipeline_prequant.py
+++ b/studio/backend/tests/test_diffusion_pipeline_prequant.py
@@ -13,7 +13,11 @@ import types
import pytest
-from ._torchao_installed import needs_torchao
+# A sibling test may put this directory on sys.path and import this file top-level.
+try:
+ from ._torchao_installed import needs_torchao
+except ImportError:
+ from _torchao_installed import needs_torchao
from core.inference import diffusion as dmod
from core.inference import diffusion_prequant as pqmod
diff --git a/studio/backend/tests/test_diffusion_plan_load_dtype.py b/studio/backend/tests/test_diffusion_plan_load_dtype.py
index 43a2681b3e3..19f7b30ca66 100644
--- a/studio/backend/tests/test_diffusion_plan_load_dtype.py
+++ b/studio/backend/tests/test_diffusion_plan_load_dtype.py
@@ -9,7 +9,11 @@ import types
import pytest
-from ._sparse_file import extend_sparse
+# A sibling test may put this directory on sys.path and import this file top-level.
+try:
+ from ._sparse_file import extend_sparse
+except ImportError:
+ from _sparse_file import extend_sparse
torch = pytest.importorskip("torch")
diff --git a/studio/backend/tests/test_prequant_safetensors.py b/studio/backend/tests/test_prequant_safetensors.py
index f01362eaef1..c2787e47691 100644
--- a/studio/backend/tests/test_prequant_safetensors.py
+++ b/studio/backend/tests/test_prequant_safetensors.py
@@ -11,7 +11,11 @@ import sys
import pytest
-from ._torchao_installed import needs_torchao
+# A sibling test may put this directory on sys.path and import this file top-level.
+try:
+ from ._torchao_installed import needs_torchao
+except ImportError:
+ from _torchao_installed import needs_torchao
import core.inference.diffusion_prequant as pq
import core.inference.prequant_safetensors as ps
diff --git a/studio/backend/tests/test_sibling_imported_tests_import_their_helpers_top_level.py b/studio/backend/tests/test_sibling_imported_tests_import_their_helpers_top_level.py
new file mode 100644
index 00000000000..a35ca76294e
--- /dev/null
+++ b/studio/backend/tests/test_sibling_imported_tests_import_their_helpers_top_level.py
@@ -0,0 +1,74 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""A test module other tests import top-level must not need its package for its own helpers.
+
+Several tests put this directory on ``sys.path`` and run ``from test_diffusion_backend import ...``. That
+imports the file as a top-level module with no parent package, so a bare ``from ._helper import x`` in it
+raises ``ImportError: attempted relative import with no known parent package`` and every importer fails to
+collect. The relative import has to sit in a ``try`` whose ``except ImportError`` imports the helper top-level.
+"""
+
+from __future__ import annotations
+
+import ast
+from pathlib import Path
+
+_TESTS = Path(__file__).resolve().parent
+
+
+def _sibling_imports(tree: ast.Module) -> set[str]:
+ names = set()
+ for node in ast.walk(tree):
+ if (
+ isinstance(node, ast.ImportFrom)
+ and node.level == 0
+ and (node.module or "").startswith("test_")
+ ):
+ names.add(node.module.split(".")[0])
+ elif isinstance(node, ast.Import):
+ names.update(alias.name for alias in node.names if alias.name.startswith("test_"))
+ return names
+
+
+def _bare_relative_imports(tree: ast.Module) -> list[int]:
+ """Top-level relative imports not guarded by a try whose handler catches ImportError."""
+ guarded = set()
+ for node in tree.body:
+ if isinstance(node, ast.Try) and any(
+ isinstance(h.type, ast.Name) and h.type.id in ("ImportError", "ModuleNotFoundError")
+ for h in node.handlers
+ ):
+ guarded.update(id(child) for child in node.body)
+ return [
+ node.lineno
+ for node in tree.body
+ if isinstance(node, ast.ImportFrom) and node.level > 0 and id(node) not in guarded
+ ]
+
+
+def test_modules_imported_top_level_import_their_helpers_without_a_package():
+ trees = {
+ path.stem: ast.parse(path.read_text(encoding = "utf-8")) for path in _TESTS.glob("test_*.py")
+ }
+ imported = set().union(*(_sibling_imports(tree) for tree in trees.values()))
+ assert (
+ "test_diffusion_backend" in imported
+ ), "the sibling-import scan found none of the known importers"
+ bad = {
+ name: lines
+ for name in sorted(imported & trees.keys())
+ if (lines := _bare_relative_imports(trees[name]))
+ }
+ assert (
+ not bad
+ ), f"bare relative imports in modules other tests import top-level (line numbers): {bad}"
+
+
+def test_the_check_tells_a_guarded_import_from_a_bare_one():
+ bare = ast.parse("from ._helper import x\n")
+ guarded = ast.parse(
+ "try:\n from ._helper import x\nexcept ImportError:\n from _helper import x\n"
+ )
+ assert _bare_relative_imports(bare) == [1]
+ assert _bare_relative_imports(guarded) == []
diff --git a/studio/backend/tests/test_video_backend.py b/studio/backend/tests/test_video_backend.py
index 405ffec85e2..01c298d443d 100644
--- a/studio/backend/tests/test_video_backend.py
+++ b/studio/backend/tests/test_video_backend.py
@@ -20,7 +20,9 @@ import pytest
try:
from ._sparse_file import extend_sparse
-except ImportError: # loaded by path, with no parent package (test_h3_native_resident.py)
+except (
+ ImportError
+): # imported top-level by a sibling test, or by file path (test_h3_native_resident.py)
import importlib.util as _importlib_util
_sparse_spec = _importlib_util.spec_from_file_location(
diff --git a/studio/backend/tests/test_video_te_quant_default.py b/studio/backend/tests/test_video_te_quant_default.py
index 208dda62866..ec231d70634 100644
--- a/studio/backend/tests/test_video_te_quant_default.py
+++ b/studio/backend/tests/test_video_te_quant_default.py
@@ -12,7 +12,11 @@ import types
import pytest
import torch
-from ._torchao_installed import needs_torchao
+# A sibling test may put this directory on sys.path and import this file top-level.
+try:
+ from ._torchao_installed import needs_torchao
+except ImportError:
+ from _torchao_installed import needs_torchao
import core.inference.diffusion_te_prequant as tpq
from core.inference.video import video_te_quant_request