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
Annotatedtypes 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file comfyg-0.1.0.tar.gz.
File metadata
- Download URL: comfyg-0.1.0.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa15f1f29cff4d368e1c5be70b04e2514ab199f145d65beb67be34ae62a73e80
|
|
| MD5 |
a5242cc71ff0a5de235de1f18f54c71a
|
|
| BLAKE2b-256 |
b3336818493a1ffa601688c5b173ced1fdd89e0baefc714f4bd0dace972d22cd
|
File details
Details for the file comfyg-0.1.0-py3-none-any.whl.
File metadata
- Download URL: comfyg-0.1.0-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f17b9420f81ec6b80f217d555e0181b14627bf92f7a1c9999de9cbe916c77128
|
|
| MD5 |
d38856c59e6f5efc003086da0fa9c16e
|
|
| BLAKE2b-256 |
998ec622689011ab6aaef7fbafdf09811cf0f1dd6ebc8e62bd9365d0fab44464
|