Coqpit Config

raw JSON →
0.2.5 verified Mon Apr 27 auth: no python

A simple, light-weight configuration management library that uses Python dataclasses. Version 0.2.5 supports Python >=3.10, Literal types, and CLI argument parsing. Released as needed; maintained by the Idiap research institute.

pip install coqpit-config
error TypeError: __init__() got multiple values for argument 'name'
cause Passing both positional and keyword argument for the same field.
fix
Use only keyword arguments: MyConfig(name='value')
error TypeError: Expected <class 'int'>, but got <class 'str'> for field 'count'
cause Value type does not match the field's type annotation.
fix
Ensure the value matches the declared type (e.g., cast to int).
breaking v0.2.0 raises TypeError if config field value does not match declared type.
fix Ensure all field values conform to the declared type annotation.
gotcha Coqpit's has() method only returns True for predefined fields (set in __annotations__). Dynamically set attributes via setattr may cause has() to return False, and get() may fail.
fix Always define fields in the class body; avoid dynamic attribute assignment.
gotcha Initializing a Coqpit class with positional arguments is deprecated. Always use keyword arguments.
fix Use keyword arguments: MyConfig(name='test', count=2) instead of MyConfig('test', 2).

Define a config dataclass by inheriting from Coqpit, then instantiate and use.

from dataclasses import dataclass
from coqpit import Coqpit

@dataclass
class MyConfig(Coqpit):
    name: str = "default"
    count: int = 1

config = MyConfig()
print(config.to_dict())