-
-
Notifications
You must be signed in to change notification settings - Fork 31
EnvWizard v1 — Quickstart (Opt‐In)
EnvWizard v1 is a typed configuration loader for Python dataclasses with:
- explicit environment precedence
- nested dataclass support
- first-class dotenv and secrets integration
- powerful field-level aliasing
As of dataclass-wizard v0.38, v1 is opt-in only and does not affect existing users unless explicitly enabled.
pip install 'dataclass-wizard>=0.38,<1'Optional extras:
pip install dataclass-wizard[dotenv] # .env support
pip install dataclass-wizard[tz] # Windows timezone dataThis example demonstrates the core v1 features:
Env, Alias(env=...), type coercion, and defaults.
import os
from dataclass_wizard.v1 import EnvWizard, Env, Alias
os.environ.update({
'APP_NAME': 'Hogwarts',
'DEBUG': 'true',
})
class Apprentice(EnvWizard):
school: str = Env('APP_NAME')
debug: bool
knows_magic: bool = Alias(
env='KNOWS_WINGARDIUM_LEVIOSA',
default=False,
)
a = Apprentice()
assert a.school == 'Hogwarts'
assert a.debug is True
assert a.knows_magic is False-
Env('APP_NAME')binds a field to a specific env var -
Alias(env=...)accepts alternate env names - Values are parsed from strings into typed fields
- Defaults prevent hard failures when env vars are missing
No decorators. No boilerplate. Just dataclasses.
EnvWizard v1 resolves values using explicit precedence:
Secrets → Environment → Dotenv
This order is configurable, testable, and predictable.
If a required value cannot be resolved, EnvWizard raises a
MissingVars error with actionable diagnostics.
Example:
class Apprentice(EnvWizard):
knows_magic: bool
Apprentice()Error:
MissingVars:
Apprentice has 1 required field missing:
- knows_magic → KNOWS_MAGIC
Create a .env file:
KNOWS_MAGIC=trueEnable dotenv loading:
class Apprentice(EnvWizard):
class _(EnvWizard.Meta):
env_file = True
knows_magic: boolOr, using the helper:
from dataclass_wizard import EnvMeta
EnvMeta(env_file=True).bind_to(Apprentice)Later dotenv files override earlier ones.
EnvWizard v1 supports secret directories, where:
- filenames = keys
- file contents = values
class Secrets(EnvWizard):
class _(EnvWizard.Meta):
secrets_dir = '/run/secrets'
api_key: strThis integrates naturally with containerized deployments.
You may pass values directly via __env__:
a = Apprentice(__env__={
'mapping': {'KNOWS_WINGARDIUM_LEVIOSA': True}
})For better IDE type hints, use env_config:
from dataclass_wizard.v1 import env_config
a = Apprentice(
__env__=env_config(
mapping={'KNOWS_WINGARDIUM_LEVIOSA': True}
)
)This is useful for tests and dynamic configuration.
EnvWizard v1 also supports alias paths for hierarchical mappings:
from dataclass_wizard.v1 import AliasPath
class Config(EnvWizard):
database_url: str = AliasPath('DATABASE', 'URL')See the reference docs for full details.
EnvWizard v1 adds:
-
Explicit, configurable environment precedence
-
Nested
EnvWizarddataclasses -
Load-only vs dump-only aliasing:
v1_field_to_env_loadv1_field_to_alias_dump
-
Field helpers:
Env(...),Alias(env=...),AliasPath(...) -
v1_pre_decoderfor JSON / delimited env values -
Cached secrets & dotenv paths
-
Full support for
kw_only=True -
__post_init__()support in generated__init__
- 📘 Reference docs (RTD): https://dcw.ritviknag.com/
- 🧭 Migration guide: v0 → v1
- 🧪 Test coverage: v1 EnvWizard has 90%+ coverage
- 💬 Feedback welcome via GitHub Issues & Discussions