Skip to main content

A compositional configuration library for Python

Project description

CompoConf

CompoConf is a Python library for compositional configuration management. It provides a type-safe way to define, parse, and instantiate configurations for complex, modular systems.

Features

  • Type-safe configuration parsing with dataclass support
  • Registry-based class instantiation
  • Inheritance-based interface registration
  • Support for nested configurations
  • Optional OmegaConf integration
  • Strict type checking and validation

Installation

pip install compoconf

Quick Start

Here's a simple example of how to use CompoConf:

from dataclasses import dataclass
from compoconf import (
    RegistrableConfigInterface,
    ConfigInterface,
    register_interface,
    register,
)

# Define an interface
@register_interface
class ModelInterface(RegistrableConfigInterface):
    pass

# Define a configuration
@dataclass
class MLPConfig(ConfigInterface):
    hidden_size: int = 128
    num_layers: int = 2

# Register a class with its configuration
@register
class MLPModel(ModelInterface):
    config_class = MLPConfig

    def __init__(self, config):
        self.config = config
        # Initialize model with config...

# Create and use configurations
config = MLPConfig(hidden_size=256)
model = config.instantiate(ModelInterface)

Advanced Usage

Nested Configurations

CompoConf supports nested configurations through type annotations:

@dataclass
class TrainerConfig(ConfigInterface):
    model: ModelInterface.cfgtype  # References the interface type
    learning_rate: float = 0.001

# Parse nested configuration
config = {
    "model": {
        "class_name": "MLPModel",
        "hidden_size": 256
    },
    "learning_rate": 0.01
}

trainer_config = parse_config(TrainerConfig, config)

Type Safety

The library provides comprehensive type checking:

  • Validates configuration values against their type annotations
  • Ensures registered classes match their interfaces
  • Checks for missing required fields
  • Supports strict mode for catching unknown configuration keys

OmegaConf Integration

CompoConf optionally integrates with OmegaConf for enhanced configuration handling:

from omegaconf import OmegaConf

# Load configuration from YAML
conf = OmegaConf.load('config.yaml')
config = parse_config(ModelConfig, conf)

Registry System

The registry system allows for dynamic class instantiation based on configuration:

# Register multiple implementations
@dataclass
class CNNConfig(ConfigInterface):
    kernel_size: int = 4

@register
class CNNModel(ModelInterface):
    config_class = CNNConfig

@dataclass
class TransformerConfig(ConfigInterface):
    hidden_size: int = 128
    num_heads: int = 4

@register
class TransformerModel(ModelInterface):
    config_class = TransformerConfig

# Configuration automatically creates correct instance
config = {
    "model": {
        "class_name": "TransformerModel",
        "num_heads": 8,
        "hidden_size": 512
    }
}

API Reference

Core Classes

  • RegistrableConfigInterface: Base class for interfaces that can be configured
  • ConfigInterface: Base class for configuration dataclasses
  • Registry: Singleton managing registration of interfaces and implementations

Decorators

  • @register_interface: Register a new interface
  • @register: Register an implementation class

Functions

  • parse_config(config_class, data, strict=True): Parse configuration data into typed objects

Enhanced Functionality

Parsing Module

The parsing module has been enhanced to provide more robust and flexible configuration parsing capabilities. Key improvements include:

  • Improved handling of nested configurations and unions.
  • Enhanced type validation and error reporting.
  • Support for parsing configurations from various data sources (e.g., JSON, YAML).

Util Module

The util module now includes powerful utilities for dynamic configuration and validation:

  • partial_call: Enables the creation of configurable classes from functions, allowing for dynamic modification of function arguments through configuration.
  • from_annotations: Simplifies the creation of configurable classes by automatically extracting configuration parameters from class annotations.
  • validate_literal_field and assert_check_literals: Provide mechanisms for validating Literal type annotations in dataclasses, ensuring that configuration values are within the allowed set of options.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License

Author

Korbinian Pöppel (korbip@korbip.de)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

compoconf-0.1.6.tar.gz (34.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

compoconf-0.1.6-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

Details for the file compoconf-0.1.6.tar.gz.

File metadata

  • Download URL: compoconf-0.1.6.tar.gz
  • Upload date:
  • Size: 34.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for compoconf-0.1.6.tar.gz
Algorithm Hash digest
SHA256 02c4c3204c8dc865238c0e576a4f39a47b6b52c44eccb5828b370c02b154af8e
MD5 5868cd6a0241bedb2026650c00022720
BLAKE2b-256 209b1bfabc44eef0c8fee173e478fa474348318c5114003c9a77ac41b8bad5a2

See more details on using hashes here.

Provenance

The following attestation bundles were made for compoconf-0.1.6.tar.gz:

Publisher: publish.yaml on kpoeppel/compoconf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file compoconf-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: compoconf-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 18.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for compoconf-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 00a265abc1ffbfa42aa944ddc1d9a2406f0e884285acbe322d29e8bb8d0ae1af
MD5 c58aa0dc91fde71a7c1836da1d65b5a8
BLAKE2b-256 18b4b5dde88b441e28b830d4521cf721e2e80d582aa114b51ab4c236dcb806d6

See more details on using hashes here.

Provenance

The following attestation bundles were made for compoconf-0.1.6-py3-none-any.whl:

Publisher: publish.yaml on kpoeppel/compoconf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page