env_proxy.env_config¶
env_config ¶
EnvConfig allows you to create configuration objects based on env variables declaratively with auto-documenting approach.
EnvField ¶
EnvField(
alias: str | None = None,
description: str | None = None,
default: Any = UNSET,
env_proxy: EnvProxy | None = None,
env_prefix: str | None = None,
strict: bool | None = None,
allow_set: bool | None = None,
type_hint: TypeHint | None = None,
optional: bool | None = None,
convert_using: Callable[[str], Any] | None = None,
type_name: str | None = None,
default_factory: Callable[[], Any] | None = None,
)
Source code in env_proxy/env_config.py
has_default
property
¶
True if the field declares any kind of default (static, factory, or implicit None).
resolved_type_name
cached
property
¶
The field's type label — the value shown in exported .env files.
Resolution order:
- Explicit
type_name=argument, if given. - The field's annotation, when it is a simple type (
int,str, an enum class, …). convert_using.__name__, unless it would be"<lambda>".- The
type_hint=value, if given. "unknown type"as a last resort.
value_getter
cached
property
¶
The callable that fetches and types this field's env value.
Called internally on every attribute access; usually you don't
need to invoke it yourself. convert_using takes precedence
over type_hint, which takes precedence over the annotation.
resolve_default ¶
Return the default value to use for instance.
For factory-defaulted fields, returns the value cached on instance
at construction time. Otherwise falls back to the static :attr:default.
Source code in env_proxy/env_config.py
EnvConfig ¶
A base class for your configurations based on environment variables.
Use fields along with Field factory to easily describe your configuration in an self-documenting way.
The constructor accepts keyword arguments to override individual fields on a
per-instance basis. Overrides take precedence over the environment, allowing
callers to layer env-derived config with values from any other source — a
config file, CLI arguments, programmatic wiring, fixtures — without mutating
os.environ. Override values are keyed by Python field name (not env-var
key), are used as-is (no type conversion), and shadow the environment for
reads on this instance only::
cfg = MyConfig(timeout=5, services=["a", "b"])
Unknown override keys raise :class:EnvConfigError (also a
:class:ValueError for back-compat). Fields with allow_set=False
can still be initialized via override but cannot be reassigned afterwards.
Source code in env_proxy/env_config.py
is_frozen
property
¶
True if :meth:freeze has been called on this instance, else False.
freeze ¶
Resolve every field once and lock the instance to the resulting values.
After calling :meth:freeze:
- Every attribute read returns the cached value (a single dict lookup).
- Assignment via
cfg.field = ...raises :class:TypeError, even for fields declared withallow_set=True. Any such fields are listed in a :class:UserWarningemitted by this call. - :attr:
is_frozenbecomesTrue.
Calling :meth:freeze again on an already-frozen instance is a no-op
and emits a :class:UserWarning.
Eagerly resolves every non-overridden field, so any exception raised
during resolution propagates from this call. Unlike :meth:validate,
:meth:freeze does not aggregate failures: the first exception
is surfaced as-is.
Raises:
| Type | Description |
|---|---|
EnvKeyMissingError
|
A required field has no env value and no default. |
EnvValueError
|
An env value cannot be converted to the target type. |
EnvConfigError
|
The field is declared incorrectly (e.g. strict-mode annotation problem surfaced at resolution time). |
JSONDecodeError
|
A |
Source code in env_proxy/env_config.py
validate ¶
Eagerly resolve every field and raise if anything is missing or malformed.
Every :class:EnvProxyError raised during field resolution is
captured and aggregated into a single :class:EnvValidationError
whose :attr:errors mapping contains the per-field exceptions.
This includes :class:EnvKeyMissingError (missing required
fields), :class:EnvValueError (failed type conversion or
convert_using callback), and :class:EnvConfigError (strict-mode
annotation problems surfaced at resolution time). Fields supplied
as constructor overrides are already typed Python values, so they
are not re-validated.
Returns None on success. Does not mutate the instance — call
:meth:freeze afterwards if you also want to lock in the values.
Two classes of exception are not aggregated and bubble out of this call:
- :class:
json.JSONDecodeErrorfrom atype_hint="json"field (documented deviation; see :mod:env_proxy.exceptions). - Any non-:class:
EnvProxyErrorexception, treated as a library bug so it can be diagnosed rather than silently swallowed.