granite_assets.factory

The factory function inspects the configuration type and returns the correct IAssetRepository implementation. It uses @overload signatures so that type checkers (mypy, pyright) can infer the concrete return type when the config type is known statically.

Lightweight factory for building asset repositories from config objects.

The factory pattern is intentionally minimal: it avoids string-based dispatch and keeps full type safety — the return type is narrowed to the concrete class so IDEs and type checkers benefit without needing cast.

granite_assets.factory.build_asset_repository(config: LocalNginxAssetRepositoryConfig) LocalNginxAssetRepository[source]
granite_assets.factory.build_asset_repository(config: S3AssetRepositoryConfig) S3AssetRepository

Instantiate the correct repository for the given configuration object.

The returned type is narrowed by overloads — type checkers will infer the concrete class when the config type is known at the call site.

Parameters:

config – A configuration dataclass. Pass a LocalNginxAssetRepositoryConfig for the local backend or a S3AssetRepositoryConfig for AWS S3.

Returns:

A ready-to-use repository instance.

Raises:
  • TypeError – If the config type is not recognised.

  • ImportError – If the S3 extra is not installed and an S3 config is given.

Example:

repo = build_asset_repository(
    S3AssetRepositoryConfig(bucket="my-bucket", region="eu-west-1")
)