refactor: implement generic optional import mechanisms

this project is actively using optional imports, which leads to
duplicate handling here and there. This commit implements logic in
special method to reduce complexity and simplify tests paths
This commit is contained in:
2026-07-22 16:33:59 +03:00
parent bd26e8da68
commit f37c5bd2a2
16 changed files with 83 additions and 83 deletions
@@ -2,7 +2,7 @@ import ahriman.web.views
from pathlib import Path
from ahriman.core.module_loader import _modules, implementations
from ahriman.core.module_loader import _modules, implementations, optional_module
from ahriman.web.views.base import BaseView
@@ -23,3 +23,17 @@ def test_implementations() -> None:
assert routes
assert all(isinstance(view, type) for view in routes)
assert all(issubclass(view, BaseView) for view in routes)
def test_optional_module() -> None:
"""
must import an available module
"""
assert optional_module("ahriman.web.views") is ahriman.web.views
def test_optional_module_fallback() -> None:
"""
must return none when the module cannot be imported
"""
assert optional_module("missing_ahriman_module") is None