Managing configuration files in Python has traditionally been a pain point. You parse a YAML or JSON file, get a dictionary, and then manually extract values with no type safety or IDE autocompletion. The dataconf library changes this by allowing you to parse configuration files directly into Python dataclasses with full type validation.
The Problem with Traditional Config Parsing
Consider a typical approach to loading configuration:
| |
This approach has several issues:
- No IDE autocompletion
- Runtime errors if keys are missing
- No type validation
- Difficult to refactor
Enter dataconf
The dataconf library provides type-safe parsing of configuration files into Python dataclasses. For those coming from Scala, this mirrors the experience of using case classes with PureConfig.
Installation
| |
Note: dataconf requires Python >= 3.9 due to typing features not available in earlier versions. I’m also a contributor to this library.
Defining Your Configuration Schema
First, define your configuration structure using dataclasses:
| |
Loading Configuration Files
dataconf supports multiple formats: HOCON, JSON, YAML, and properties files.
HOCON Example
# config.hocon
database {
host = "localhost"
port = 5432
name = "myapp"
user = "admin"
password = ${DB_PASSWORD} # Environment variable substitution
}
cache {
enabled = true
ttl_seconds = 3600
max_size = 1000
}
debug = false
| |
JSON Example
| |
| |
YAML Example
| |
| |
Advanced Features
Environment Variable Substitution
HOCON format supports environment variable substitution:
database {
password = ${DB_PASSWORD}
password = ${?DB_PASSWORD} # Optional - won't fail if missing
}
Nested Configurations
dataconf handles deeply nested structures naturally:
| |
Writing Configurations
You can also serialize dataclasses back to configuration files:
| |
Benefits Over Traditional Approaches
| Feature | Traditional | dataconf |
|---|---|---|
| Type safety | None | Full |
| IDE autocompletion | No | Yes |
| Validation | Manual | Automatic |
| Refactoring | Error-prone | Safe |
| Documentation | External | In code |
Immutable Configurations
For truly immutable configurations (like Scala case classes), use frozen dataclasses:
| |
Conclusion
Using dataconf with Python dataclasses provides:
- Type safety: Catch configuration errors at load time
- IDE support: Full autocompletion and type hints
- Validation: Automatic type checking and required field validation
- Maintainability: Configuration schema is self-documenting
- Flexibility: Support for multiple file formats
For anyone coming from Scala or TypeScript, this brings the configuration experience you expect to Python.
Originally published on Towards Data Science