Skip to content

Commit e2f8851

Browse files
committed
f
1 parent cebed06 commit e2f8851

1 file changed

Lines changed: 18 additions & 29 deletions

File tree

tests/others/test_dependencies.py

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import inspect
1616
from importlib import import_module
17-
from pathlib import Path
1817

1918
import pytest
2019

@@ -53,44 +52,34 @@ def test_pipeline_imports(self):
5352
_ = import_module(pipeline_folder_module, str(cls_name))
5453

5554
def test_pipeline_module_imports(self):
56-
"""Import every pipeline submodule whose folder-level dependency guards
57-
are satisfied, to catch unguarded optional-dep imports.
55+
"""Import every pipeline submodule whose dependencies are satisfied,
56+
to catch unguarded optional-dep imports (e.g., torchvision).
5857
59-
Each pipeline folder's __init__.py evaluates guards like
60-
is_torch_available() and populates _import_structure only for submodules
61-
whose deps are met. We use _import_structure as the source of truth:
62-
if a submodule is listed there, its declared deps are installed, so any
63-
ImportError from importing it is a real bug (e.g., unguarded torchvision).
58+
Uses inspect.getmembers to discover classes that the lazy loader can
59+
actually resolve (same self-filtering as test_pipeline_imports), then
60+
imports the full module path instead of truncating to the folder level.
6461
"""
62+
import diffusers
6563
import diffusers.pipelines
6664

67-
pipelines_dir = Path(diffusers.pipelines.__file__).parent
6865
failures = []
66+
all_classes = inspect.getmembers(diffusers, inspect.isclass)
6967

70-
for subdir in sorted(pipelines_dir.iterdir()):
71-
if not subdir.is_dir() or not (subdir / "__init__.py").exists():
68+
for cls_name, cls_module in all_classes:
69+
if not hasattr(diffusers.pipelines, cls_name):
70+
continue
71+
if "dummy_" in cls_module.__module__:
7272
continue
7373

74-
# Import the pipeline package to trigger its guard evaluation
75-
package_module_path = f"diffusers.pipelines.{subdir.name}"
74+
full_module_path = cls_module.__module__
7675
try:
77-
package_module = import_module(package_module_path)
76+
import_module(full_module_path)
77+
except ImportError as e:
78+
failures.append(f"{full_module_path}: {e}")
7879
except Exception:
79-
continue
80-
81-
# _import_structure keys are the submodules whose deps are satisfied
82-
import_structure = getattr(package_module, "_import_structure", {})
83-
84-
for submodule_name in import_structure:
85-
full_module_path = f"{package_module_path}.{submodule_name}"
86-
try:
87-
import_module(full_module_path)
88-
except ImportError as e:
89-
failures.append(f"{full_module_path}: {e}")
90-
except Exception:
91-
# Non-import errors (e.g., missing config) are fine; we only
92-
# care about unguarded import statements.
93-
pass
80+
# Non-import errors (e.g., missing config) are fine; we only
81+
# care about unguarded import statements.
82+
pass
9483

9584
if failures:
9685
pytest.fail("Unguarded optional-dependency imports found:\n" + "\n".join(failures))

0 commit comments

Comments
 (0)