Skip to content

dependencies

ruff_sync.dependencies

Utilities for handling optional dependencies and lazy loading.

__all__ module-attribute

__all__ = [
    "DependencyError",
    "is_installed",
    "require_dependency",
]

DependencyError

Bases: Exception

Raised when a required optional dependency is missing or broken.

Source code in src/ruff_sync/dependencies.py
class DependencyError(Exception):
    """Raised when a required optional dependency is missing or broken."""

is_installed

is_installed(package_name)

Check if a package is installed without importing it.

Parameters:

Name Type Description Default
package_name str

The name of the package to check.

required

Returns:

Type Description
bool

True if the package is available, False otherwise.

Source code in src/ruff_sync/dependencies.py
def is_installed(package_name: str) -> bool:
    """Check if a package is installed without importing it.

    Args:
        package_name: The name of the package to check.

    Returns:
        True if the package is available, False otherwise.
    """
    return importlib.util.find_spec(package_name) is not None

require_dependency

require_dependency(
    package_name,
    extra_name,
    *,
    _is_installed=is_installed,
    _import_module=importlib.import_module,
)

Ensure a dependency is installed and importable, or raise DependencyError.

Parameters:

Name Type Description Default
package_name str

The name of the required package.

required
extra_name str

The name of the ruff-sync extra that provides this package.

required
_is_installed Callable[[str], bool]

Internal use only for testing. Function to check if a package is installed.

is_installed
_import_module Callable[[str], Any]

Internal use only for testing. Function to import a module.

import_module

Raises:

Type Description
DependencyError

If the package is not installed or raises an error during import.

Source code in src/ruff_sync/dependencies.py
def require_dependency(
    package_name: str,
    extra_name: str,
    *,
    _is_installed: Callable[[str], bool] = is_installed,
    _import_module: Callable[[str], Any] = importlib.import_module,
) -> None:
    """Ensure a dependency is installed and importable, or raise DependencyError.

    Args:
        package_name: The name of the required package.
        extra_name: The name of the ruff-sync extra that provides this package.
        _is_installed: Internal use only for testing. Function to check if a package is installed.
        _import_module: Internal use only for testing. Function to import a module.

    Raises:
        DependencyError: If the package is not installed or raises an error during import.
    """
    msg = (
        f"The '{package_name}' package is required for this feature. "
        f"Install it with: pip install 'ruff-sync[{extra_name}]'"
    )

    # 1. Fast check (dry) to see if it exists at all
    if not _is_installed(package_name):
        raise DependencyError(msg)

    # 2. Wet check (real import) to ensure it's functional
    try:
        _import_module(package_name)
    except (ImportError, ModuleNotFoundError) as e:
        # If it failed here, it's installed but BROKEN (e.g. missing sub-dependencies)
        raise DependencyError(msg) from e