perf: lazy-import in CLI commands to speed up startup (#335)

This commit is contained in:
d3vyce
2026-07-02 22:40:48 +02:00
committed by GitHub
parent fe2c0f3eff
commit 44ba5bdd4b
3 changed files with 23 additions and 5 deletions
@@ -6,7 +6,7 @@ import typer
from rich.console import Console from rich.console import Console
from rich.table import Table from rich.table import Table
from ...fixtures import Context, LoadStrategy, load_fixtures_by_context from ...fixtures import Context, LoadStrategy
from ...logger import get_logger from ...logger import get_logger
from ..config import get_db_context, get_fixtures_registry from ..config import get_db_context, get_fixtures_registry
from ..utils import async_command from ..utils import async_command
@@ -71,6 +71,8 @@ async def load(
] = False, ] = False,
) -> None: ) -> None:
"""Load fixtures into the database.""" """Load fixtures into the database."""
from ...fixtures import load_fixtures_by_context
registry = get_fixtures_registry() registry = get_fixtures_registry()
db_context = get_db_context() db_context = get_db_context()
+2 -1
View File
@@ -1,6 +1,5 @@
"""CLI utility functions.""" """CLI utility functions."""
import asyncio
import functools import functools
from collections.abc import Callable, Coroutine from collections.abc import Callable, Coroutine
from typing import Any, ParamSpec, TypeVar from typing import Any, ParamSpec, TypeVar
@@ -24,6 +23,8 @@ def async_command(func: Callable[P, Coroutine[Any, Any, T]]) -> Callable[P, T]:
@functools.wraps(func) @functools.wraps(func)
def wrapper(*args: P.args, **kwargs: P.kwargs) -> T: def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
import asyncio
return asyncio.run(func(*args, **kwargs)) return asyncio.run(func(*args, **kwargs))
return wrapper return wrapper
+18 -3
View File
@@ -1,8 +1,6 @@
"""Fixture system for seeding databases with dependency resolution.""" """Fixture system for seeding databases with dependency resolution."""
from .enum import LoadStrategy from .enum import Context, LoadStrategy
from .registry import Context, FixtureRegistry
from .utils import load_fixtures, load_fixtures_by_context
__all__ = [ __all__ = [
"Context", "Context",
@@ -11,3 +9,20 @@ __all__ = [
"load_fixtures", "load_fixtures",
"load_fixtures_by_context", "load_fixtures_by_context",
] ]
_LAZY = {
"FixtureRegistry": ".registry",
"load_fixtures": ".utils",
"load_fixtures_by_context": ".utils",
}
def __getattr__(name: str):
module_name = _LAZY.get(name)
if module_name is None:
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
import importlib
module = importlib.import_module(module_name, __name__)
return getattr(module, name)