Coverage for /home/admin/Documents/AI/applications/lexigram-dev/lexigram/experimental/ai/lexigram-ai-rag/src/lexigram/ai/rag/deps/optional.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-25 07:19 +0800

1from __future__ import annotations 

2 

3from importlib.util import find_spec 

4from typing import TYPE_CHECKING, Any 

5 

6if TYPE_CHECKING: 

7 from collections.abc import Iterable 

8 

9 

10class MissingOptionalDependencyError(ImportError): 

11 _code: str = "LEX_ERR_RAG_017" 

12 

13 

14def ensure_packages( 

15 packages: Iterable[str], 

16 hint: str | None = None, 

17) -> dict[str, Any | None]: 

18 """Ensure the given packages are importable.""" 

19 missing: list[str] = [] 

20 found: dict[str, Any | None] = {} 

21 

22 for pkg in packages: 

23 spec = find_spec(pkg) 

24 if spec is None: 

25 missing.append(pkg) 

26 found[pkg] = None 

27 else: 

28 found[pkg] = None 

29 

30 if missing: 

31 names = ", ".join(missing) 

32 hint_msg = f" Install with: {hint}" if hint else "" 

33 raise MissingOptionalDependencyError( 

34 f"Missing optional dependencies: {names}.{hint_msg}", 

35 ) 

36 

37 return found