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 <cursoragent@cursor.com>
This commit is contained in:
Brian Bjarke Jensen
2026-06-30 15:47:04 +02:00
co-authored by Cursor
parent 9e4b1e2c5e
commit c6eed43d70
6 changed files with 168 additions and 11 deletions
+25 -2
View File
@@ -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