Skip to main content

Lightweight, easy config loading and type validation

Project description

comfyg

A Python utility to define configuration easily from dataclasses, with support for rich type annotation, automatic type validation, casting from string, and per-option documentation.

Also provides import/export features from and to INI files.

Features

  • Type validation: Strict type validation on instantiation (no silent casting)
  • Rich annotations: Support for Annotated types with validators and documentation
  • Automatic casting: Convert string values to their target types from config files
  • INI file support: Seamless import/export to/from INI configuration files
  • Recursive validation: Nest ConfigValidator objects as much as needed
  • Scoped parameters: Mark options with visibility levels (BASIC, ADVANCED, INTERNAL, HIDDEN)
  • Dependencies: Express conditional visibility based on other parameter values
  • Zero dependencies: Implements its own type validation and casting logic

Quick Start

Basic Configuration

from dataclasses import dataclass
from comfyg import ConfigValidator

@dataclass
class MyConfig(ConfigValidator):
    my_option: int = 42
    name: str = "default"

config = MyConfig()

Rich Type Annotations

Use Annotated to add documentation and validators:

from typing import Annotated
from comfyg import ConfigValidator, Range, Choices

@dataclass
class ASection(ConfigValidator):
    an_int: Annotated[int, "This is the doc string", Range(0, 100)] = 42
    a_choice: Annotated[str, "Another doc string", Choices("Fizz", "Buzz", "FizzBuzz")] = "Fizz"

Casting from Strings

Use import_from_configparser or import_from_ini_file to automatically cast string values:

from comfyg import import_from_ini_file

config = import_from_ini_file(MyConfig, "config.ini")

Nested Sections

ConfigValidators can contain other ConfigValidators for multi-level organization:

@dataclass
class NestedConfig(ConfigValidator):
    section_one: ASection = field(default_factory=ASection)
    section_two: AnotherSection = field(default_factory=AnotherSection)

Scopes

Use the DefaultScopes annotation to define how parameters should be exposed:

from comfyg import DefaultScopes

@dataclass
class Config(ConfigValidator):
    basic_param: Annotated[int, DefaultScopes.BASIC] = 1
    advanced_param: Annotated[int, DefaultScopes.ADVANCED] = 2
    internal_param: Annotated[int, DefaultScopes.INTERNAL] = 3
    hidden_param: Annotated[int, DefaultScopes.HIDDEN] = 4
  • BASIC: Always shown to users
  • ADVANCED: Advanced/developer level features
  • INTERNAL: For internal use, exported to config files
  • HIDDEN: Never exported, reserved for internal state

Export & Import

To INI File

from comfyg import export_to_ini_file

export_to_ini_file(config, "config.ini")

To Dictionary

from comfyg import DefaultScopes

# With all parameters
config_dict = config.dict_export(scope=DefaultScopes.HIDDEN)

# Flattened (dot-separated keys)
flat_dict = config.flat_dict_export(scope=DefaultScopes.HIDDEN)

With Documentation

Export configuration with metadata:

meta = config.meta_export(scope=DefaultScopes.BASIC)
# Includes type hints, defaults, validators, and documentation

ConfigLoader

For convenient config management with optional persistence:

from comfyg import ConfigLoader

loader = ConfigLoader(MyConfig, config_path="config.ini", autosave=True)
config = loader.load()

# Make changes...
loader.save()

Design Philosophy

This module implements its own type validation and casting logic without external dependencies. While similar libraries exist (like attrs and pydantic), this approach prioritizes:

  • Lightweight: Only one dependency (exhausterr, to provide a railway-programming style API).
  • Standard: Works with plain, stdlib Python dataclasses (@dataclass) does not introduce a 3rd-party dataclass wrapper.
  • Declarative: Focus on type annotations as the source of truth
  • Explicit: Validation and casting are two separated pipelines, which you can individually control.

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

comfyg-0.1.2.tar.gz (29.6 kB view details)

Uploaded Source

Built Distribution

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

comfyg-0.1.2-py3-none-any.whl (26.0 kB view details)

Uploaded Python 3

File details

Details for the file comfyg-0.1.2.tar.gz.

File metadata

  • Download URL: comfyg-0.1.2.tar.gz
  • Upload date:
  • Size: 29.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.27 {"installer":{"name":"uv","version":"0.11.27","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for comfyg-0.1.2.tar.gz
Algorithm Hash digest
SHA256 384652f39ba63ed3447539809ca5696924e0955f9009feeaec5170ef2c9fc6e1
MD5 4a5e1cab0c64532e246f87870800f022
BLAKE2b-256 d57169c24c55bd9a9da0460ffb96c0240e32a81b9c594740923f76e778d23571

See more details on using hashes here.

File details

Details for the file comfyg-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: comfyg-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 26.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.27 {"installer":{"name":"uv","version":"0.11.27","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for comfyg-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 01758ff1fd3bb0d95ccac37da697c62c7adbe5eb51bacfbce0fdb0d9c3fc87dd
MD5 8e968790603e5db623b7e7637602bf13
BLAKE2b-256 db81d9452a31deb20b65dba3bea2a805df8861a897d6cf4c6276a6c421b2d4e3

See more details on using hashes here.

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