Skip to content

config

orchard.core.config

Configuration Package Initialization.

Provides a unified, flat public API for configuration components while avoiding eager imports of heavy or optional dependencies (e.g. torch).

Architecture:

  • Lazy Import Pattern (PEP 562): Uses getattr for on-demand loading
  • Deferred Dependencies: torch and pydantic loaded only when needed
  • Flat API: All configs accessible from orchard.core.config namespace
  • Caching: Loaded modules cached in globals() for performance

Implementation:

  1. __all__: Public API contract listing all available configs
  2. _LAZY_IMPORTS: Mapping from config names to module paths
  3. __getattr__: Dynamic loader triggered on first access
  4. __dir__: IDE/introspection support for auto-completion
Example

from orchard.core.config import Config, HardwareConfig

torch is NOT imported yet (lazy loading)

cfg = Config.from_recipe(Path("recipes/config_mini_cnn.yaml"))

NOW torch is imported (triggered by Config access)

__getattr__(name)

Lazily import configuration components on first access.

Implements PEP 562 module-level getattr to defer heavy dependency imports (torch, pydantic) until the corresponding class is used.

Parameters:

Name Type Description Default
name str

Name of the configuration class to import.

required

Returns:

Type Description
Any

The requested configuration class.

Raises:

Type Description
AttributeError

If name is not in the public API (all).

Source code in orchard/core/config/__init__.py
def __getattr__(name: str) -> Any:
    """
    Lazily import configuration components on first access.

    Implements PEP 562 module-level __getattr__ to defer heavy dependency
    imports (torch, pydantic) until the corresponding class is used.

    Args:
        name: Name of the configuration class to import.

    Returns:
        The requested configuration class.

    Raises:
        AttributeError: If name is not in the public API (__all__).
    """
    if name not in _LAZY_IMPORTS:
        raise AttributeError(f"module {__name__!r} has no attribute {name!r}")

    module = import_module(_LAZY_IMPORTS[name])
    attr = getattr(module, name)

    # Cache on module for future access
    globals()[name] = attr
    return attr

__dir__()

Support for dir() and IDE auto-completion.

Returns:

Type Description
list[str]

Sorted list of public configuration class names

Source code in orchard/core/config/__init__.py
def __dir__() -> list[str]:
    """
    Support for dir() and IDE auto-completion.

    Returns:
        Sorted list of public configuration class names
    """
    return sorted(__all__)