Coqui uses the coqpit-config package (via Coqpit) for configuration management. It leverages Python dataclasses to provide static type checking and serialization.
Key features include:
- Mandatory Fields: Use
MISSING as a default value to ensure an error is raised if the field is not explicitly provided. - Optional Fields: Use standard type hints and default values.
- Complex Types: Supports
dict, List[List], and List[Union[...]] using field(default_factory=...). - Value Validation: You can implement a
check_values method within your configuration class to enforce constraints (e.g., min/max values) using check_argument.
from dataclasses import asdict, dataclass, field
from typing import List, Union
from coqpit.coqpit import MISSING, Coqpit, check_argument
@dataclass
class SimpleConfig(Coqpit):
val_a: int = 10
val_b: int = None
val_d: float = 10.21
val_c: str = "Coqpit is great!"
vol_e: bool = True
# mandatory field
val_k: int = MISSING
# optional field
val_dict: dict = field(default_factory=lambda: {"val_aa": 10, "val_ss": "This is in a dict."})
# list of list
val_listoflist: List[List] = field(default_factory=lambda: [[1, 2], [3, 4]])
val_listofunion: List[List[Union[str, int, bool]]] = field(
default_factory=lambda: [[1, 3], [1, "Hi!"], [True, False]]
)
def check_values(self):
"""Check config fields"""
c = asdict(self)
check_argument("val_a", c, restricted=True, min_val=10, max_val=2056)
check_argument("val_b", c, restricted=True, min_val=128, max_val=4058, allow_none=True)
check_argument("val_c", c, restricted=True)