Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions python/kubeflow/model_registry/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Re-export ModelRegistry from the standalone model-registry package
# This allows users to import from kubeflow.model_registry while
# maintaining backward compatibility with the standalone package

try:
from model_registry import ModelRegistry as ModelRegistryClient
except ImportError as e:
raise ImportError(
"model-registry package is required. Install with: pip install model-registry"
) from e

__all__ = [
"ModelRegistryClient",
]
102 changes: 102 additions & 0 deletions python/kubeflow/model_registry/tests/test_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import pytest
import sys


@pytest.mark.model_registry
def test_import_model_registry_available():
"""Test that ModelRegistry can be imported when model-registry package is available."""
try:
from kubeflow.model_registry import ModelRegistry, ModelRegistryClient
from model_registry import ModelRegistry as OriginalModelRegistry

# Verify they are the same class
assert ModelRegistry is OriginalModelRegistry
assert ModelRegistryClient is OriginalModelRegistry

except ImportError:
pytest.skip("model-registry package is not installed")


@pytest.mark.model_registry
def test_import_model_registry_client_available():
"""Test that ModelRegistryClient is an alias for ModelRegistry."""
try:
from kubeflow.model_registry import ModelRegistryClient, ModelRegistry

# Verify ModelRegistryClient is an alias for ModelRegistry
assert ModelRegistryClient is ModelRegistry

except ImportError:
pytest.skip("model-registry package is not installed")


@pytest.mark.model_registry
def test_import_error_when_model_registry_not_installed(monkeypatch):
"""Test that a helpful error is raised when model-registry is not available."""
# Remove the module from sys.modules if it exists
modules_to_remove = ["kubeflow.model_registry", "model_registry", "model_registry._client"]
original_modules = {}

for module_name in modules_to_remove:
if module_name in sys.modules:
original_modules[module_name] = sys.modules[module_name]
del sys.modules[module_name]

# Store the original import function to avoid recursion
original_import = __import__

def mock_import(name, *args, **kwargs):
if name == "model_registry":
raise ImportError("No module named 'model_registry'")
return original_import(name, *args, **kwargs)

monkeypatch.setattr("builtins.__import__", mock_import)

try:
with pytest.raises(ImportError) as exc_info:
import kubeflow.model_registry # noqa: F401

# Verify the error message is helpful
assert "model-registry package is required" in str(exc_info.value)
assert "pip install model-registry" in str(exc_info.value)

finally:
# Restore the original modules
for module_name, module in original_modules.items():
sys.modules[module_name] = module


@pytest.mark.model_registry
def test_all_exports_available():
"""Test that all expected exports are available in the module."""
try:
import kubeflow.model_registry as mr_module

# Check that __all__ contains expected items
expected_exports = ["ModelRegistry", "ModelRegistryClient"]
assert hasattr(mr_module, "__all__")
assert set(mr_module.__all__) == set(expected_exports)

# Check that all items in __all__ are actually available
for export in expected_exports:
assert hasattr(mr_module, export)

except ImportError:
pytest.skip("model-registry package is not installed")


@pytest.mark.model_registry
def test_import_via_star_import():
"""Test that star imports work correctly."""
try:
# This is a bit tricky to test directly, so we'll check the namespace
import kubeflow.model_registry as mr_module

# Simulate what 'from kubeflow.model_registry import *' would do
star_imports = {name: getattr(mr_module, name) for name in mr_module.__all__}

assert "ModelRegistryClient" in star_imports
assert star_imports["ModelRegistry"] is star_imports["ModelRegistryClient"]

except ImportError:
pytest.skip("model-registry package is not installed")
3 changes: 3 additions & 0 deletions python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ dependencies = [
"kubeflow-trainer-api>=2.0.0",
]
[project.optional-dependencies]
model-registry = [
"model-registry>=0.2.21",
]
dev = [
"pytest>=7.0",
"pytest-mock>=3.10",
Expand Down
Loading