Fix optional dependency handling for Redis and MinIO adapters.
Lazy-load adapters at package boundaries and fail fast with install hints when extras are missing. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
co-authored by
Cursor
parent
9e4b1e2c5e
commit
c6eed43d70
@@ -1,6 +1,11 @@
|
||||
"""python_repositories: Unified repository interfaces and adapters."""
|
||||
|
||||
from .adapters import MinioAdapter, RedisAdapter
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
# Interfaces are always available; they have no optional backend dependencies.
|
||||
from .interfaces import (
|
||||
ConnectionAwareInterface,
|
||||
ContextAwareInterface,
|
||||
@@ -8,6 +13,18 @@ from .interfaces import (
|
||||
ObjectRepositoryInterface,
|
||||
)
|
||||
|
||||
# Adapters are imported only for static type checkers; runtime loading is deferred below.
|
||||
if TYPE_CHECKING:
|
||||
from .adapters.minio_adapter import MinioAdapter
|
||||
from .adapters.redis_adapter import RedisAdapter
|
||||
|
||||
# Map public adapter names to their defining module and class.
|
||||
# Each adapter module fails fast with an install hint if its extra is missing.
|
||||
_LAZY_EXPORTS = {
|
||||
"RedisAdapter": (".adapters.redis_adapter", "RedisAdapter"),
|
||||
"MinioAdapter": (".adapters.minio_adapter", "MinioAdapter"),
|
||||
}
|
||||
|
||||
__all__ = [
|
||||
"ConnectionAwareInterface",
|
||||
"ContextAwareInterface",
|
||||
@@ -16,3 +33,17 @@ __all__ = [
|
||||
"RedisAdapter",
|
||||
"MinioAdapter",
|
||||
]
|
||||
|
||||
|
||||
def __getattr__(name: str) -> object:
|
||||
"""Load adapters on first access so the base package installs without redis/minio."""
|
||||
if name in _LAZY_EXPORTS:
|
||||
module_path, attr = _LAZY_EXPORTS[name]
|
||||
module = importlib.import_module(module_path, __package__)
|
||||
return getattr(module, attr)
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
|
||||
|
||||
def __dir__() -> list[str]:
|
||||
"""Expose lazy adapter names in tab completion and dir()."""
|
||||
return sorted(__all__)
|
||||
|
||||
@@ -4,10 +4,33 @@ Adapters for various backend repositories (e.g., Redis, Minio).
|
||||
This module exposes concrete implementations for repository interfaces.
|
||||
"""
|
||||
|
||||
from .redis_adapter import RedisAdapter
|
||||
from .minio_adapter import MinioAdapter
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .minio_adapter import MinioAdapter
|
||||
from .redis_adapter import RedisAdapter
|
||||
|
||||
_LAZY_EXPORTS = {
|
||||
"RedisAdapter": (".redis_adapter", "RedisAdapter"),
|
||||
"MinioAdapter": (".minio_adapter", "MinioAdapter"),
|
||||
}
|
||||
|
||||
__all__ = [
|
||||
"RedisAdapter",
|
||||
"MinioAdapter",
|
||||
]
|
||||
|
||||
|
||||
def __getattr__(name: str) -> object:
|
||||
if name in _LAZY_EXPORTS:
|
||||
module_path, attr = _LAZY_EXPORTS[name]
|
||||
module = importlib.import_module(module_path, __package__)
|
||||
return getattr(module, attr)
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
|
||||
|
||||
def __dir__() -> list[str]:
|
||||
return sorted(__all__)
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
from __future__ import annotations
|
||||
import os
|
||||
from io import BytesIO
|
||||
from importlib.util import find_spec
|
||||
from typing import Self
|
||||
import structlog
|
||||
from python_utils import check_env
|
||||
@@ -14,9 +13,13 @@ from python_repositories.interfaces import (
|
||||
ObjectRepositoryInterface,
|
||||
)
|
||||
|
||||
# Handle optional dependencies
|
||||
if find_spec("minio") is not None:
|
||||
try:
|
||||
import minio
|
||||
except ImportError as exc:
|
||||
raise ImportError(
|
||||
"MinIO support requires the minio extra. "
|
||||
"Install with: pip install python-repositories[minio]"
|
||||
) from exc
|
||||
|
||||
|
||||
class MinioAdapter(
|
||||
@@ -112,7 +115,7 @@ class MinioAdapter(
|
||||
@property
|
||||
def is_connected(self) -> bool:
|
||||
"""Check if connected to Minio server."""
|
||||
res = bool(isinstance(self._client, minio.Minio))
|
||||
res = self._client is not None
|
||||
self.logger.debug(res)
|
||||
return res
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
from __future__ import annotations
|
||||
from typing import Self, cast
|
||||
from importlib.util import find_spec
|
||||
import os
|
||||
import structlog
|
||||
|
||||
@@ -14,10 +13,14 @@ from python_repositories.interfaces import (
|
||||
JsonRepositoryInterface,
|
||||
)
|
||||
|
||||
# Handle optional dependencies
|
||||
if find_spec("redis") is not None:
|
||||
try:
|
||||
import redis
|
||||
from redis.commands.json.path import Path as RedisPath
|
||||
except ImportError as exc:
|
||||
raise ImportError(
|
||||
"Redis support requires the redis extra. "
|
||||
"Install with: pip install python-repositories[redis]"
|
||||
) from exc
|
||||
|
||||
|
||||
class RedisAdapter(
|
||||
@@ -90,7 +93,7 @@ class RedisAdapter(
|
||||
@property
|
||||
def is_connected(self) -> bool:
|
||||
"""Check if connected to Redis server."""
|
||||
res = bool(isinstance(self._client, redis.Redis))
|
||||
res = self._client is not None
|
||||
self.logger.debug(res)
|
||||
return res
|
||||
|
||||
|
||||
Reference in New Issue
Block a user