Confection
Confection is a lightweight and flexible configuration system for Python, designed to handle complex nested configurations with features like interpolation, validation, and registry integration. It is currently at version 1.3.3 and maintains an active release cadence, often aligning with updates to related libraries like spaCy and Thinc.
Warnings
- breaking Confection v1.1.0 and later dropped support for Pydantic for validation, reverting to custom validation logic. If you were relying on Pydantic's specific validation behaviors with older versions, your configurations might behave differently or break.
- gotcha Prior to v1.3.3, Python-style boolean (True, False) and None literals in configuration files were silently treated as strings instead of their corresponding Python types. This could lead to type errors, especially with newer Pydantic versions.
- breaking Changes in Pydantic v2 support across Confection versions (v1.0.0, v1.0.0a2, v1.1.0) could lead to compatibility issues. If you're using Pydantic in conjunction with Confection, ensure your Confection version aligns with the Pydantic version you're using.
- gotcha When using `Config.from_str()` for loading configurations, pay close attention to the YAML/INI-like syntax. Incorrect indentation or malformed sections can lead to parsing errors. For example, top-level section references have had bug fixes.
Install
-
pip install confection
Imports
- Config
from confection import Config
- set_registry
from confection import set_registry
Quickstart
from confection import Config, set_registry
@set_registry("my_functions", "add")
def add_function(a: int, b: int):
return a + b
config = Config({"model": {"func": {"@my_functions": "add"}, "a": 1, "b": 2}})
resolved = config.resolve()
result = resolved["model"]["func"](resolved["model"]["a"], resolved["model"]["b"])
print(f"Result: {result}")
# Example with direct config creation
config_direct = Config().from_str("""
[model]
func = @my_functions.add
a = 10
b = 20
""")
resolved_direct = config_direct.resolve()
result_direct = resolved_direct["model"]["func"](resolved_direct["model"]["a"], resolved_direct["model"]["b"])
print(f"Direct Config Result: {result_direct}")