Config API Reference¶
Auto-generated API documentation for the config module.
config
¶
Unified configuration loader — zero dependencies, stdlib only, Python 3.10+.
Part of zerodep: https://github.com/Oaklight/zerodep Copyright (c) 2026 Peng Ding. MIT License.
Drop-in replacement for python-decouple / dynaconf core functionality. Loads settings from environment variables, .env files, and config files (JSON, JSONC, YAML, TOML, INI) with type coercion and prefix support.
Example::
from config import config, setup
# Auto-discovers .env, reads env vars
setup(prefix="MYAPP_")
debug = config("DEBUG", default=False, cast=bool)
port = config("PORT", default=8000, cast=int)
hosts = config("ALLOWED_HOSTS", cast=Csv())
# Or use Config directly
cfg = Config(config_path="settings.yaml", prefix="MYAPP_")
db_host = cfg("DATABASE__HOST", default="localhost")
ConfigError
¶
UndefinedValueError
¶
Bases: ConfigError
Raised when a required configuration value is missing.
Csv
¶
Parse comma-separated values with optional per-item casting.
Example::
Csv()("a, b, c") # ["a", "b", "c"]
Csv(cast=int)("1,2,3") # [1, 2, 3]
Csv(delimiter=";")("a;b") # ["a", "b"]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cast
|
Callable[[str], Any]
|
Callable applied to each item after splitting. |
str
|
delimiter
|
str
|
String to split on. |
','
|
strip
|
str
|
Characters to strip from each item. Use |
' %s'
|
post_process
|
Callable[[list[Any]], Any]
|
Callable applied to the final list (e.g. |
list
|
Source code in config/config.py
Choices
¶
Validate that a value belongs to a fixed set.
Example::
Choices(["dev", "staging", "prod"])("dev") # "dev"
Choices([1, 2, 3], cast=int)("2") # 2
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
choices
|
Sequence[Any]
|
Allowed values (after casting). |
required |
cast
|
Callable[[str], Any]
|
Callable applied before validation. |
str
|
Source code in config/config.py
Config
¶
Unified configuration loader with multi-source support.
Sources are checked in priority order (highest first):
- Environment variables (
os.environ), optionally prefixed .envfile values (via sibling dotenv module)- Config file values (JSON, JSONC, YAML, TOML, INI)
- Default value passed to
get()/__call__()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dotenv_path
|
str | PathLike[str] | None | _Auto
|
Path to |
_AUTO
|
config_path
|
str | PathLike[str] | None
|
Path to a config file. Format is detected from the file extension. |
None
|
prefix
|
str
|
Prefix for environment variable lookups. For example,
|
''
|
separator
|
str
|
Separator for nested key access. Default |
'__'
|
loaders
|
dict[str, Callable[..., dict[str, Any]]] | None | _Unset
|
Override the file-format loader registry. Defaults to
|
_UNSET
|
dotenv_loader
|
Callable[[], tuple[Callable[..., Any], Callable[..., Any]]] | None | _Unset
|
Override the dotenv loading mechanism. Defaults to
|
_UNSET
|
Example::
cfg = Config(config_path="settings.yaml", prefix="MYAPP_")
debug = cfg("DEBUG", default=False, cast=bool)
db_host = cfg("DATABASE__HOST", default="localhost")
Source code in config/config.py
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 | |
get(key, *, default=MISSING, cast=None)
¶
Retrieve a configuration value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Configuration key to look up. |
required |
default
|
Any
|
Default value if key is not found. If not provided and
the key is missing, |
MISSING
|
cast
|
type | Callable[..., Any] | None
|
Type or callable to apply to the value. Built-in support
for |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The resolved and optionally cast configuration value. |
Raises:
| Type | Description |
|---|---|
UndefinedValueError
|
If key is missing and no default is given. |
Source code in config/config.py
__call__(key, *, default=MISSING, cast=None)
¶
Shorthand for get(). See :meth:get for details.
has(key)
¶
as_dict()
¶
Return a merged view of all config sources (flattened).
Lower-priority sources are merged first, so higher-priority sources overwrite them.
Source code in config/config.py
setup(*, dotenv_path=_AUTO, config_path=None, prefix='', separator='__')
¶
Initialize the module-level :class:Config instance.
Call this once at application startup to configure sources and prefix.
Subsequent calls to :func:config will use this instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dotenv_path
|
str | PathLike[str] | None | _Auto
|
Path to |
_AUTO
|
config_path
|
str | PathLike[str] | None
|
Path to a config file (JSON/YAML/TOML/INI). |
None
|
prefix
|
str
|
Prefix for environment variable lookups. |
''
|
separator
|
str
|
Separator for nested key access. |
'__'
|
Returns:
| Type | Description |
|---|---|
Config
|
The newly created :class: |
Source code in config/config.py
config(key, *, default=MISSING, cast=None)
¶
Look up a configuration value using the module-level instance.
If :func:setup has not been called, a default :class:Config is
created automatically (auto-discovers .env, no config file,
no prefix).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Configuration key. |
required |
default
|
Any
|
Fallback value if missing. |
MISSING
|
cast
|
type | Callable[..., Any] | None
|
Type or callable to coerce the value. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The resolved configuration value. |
Raises:
| Type | Description |
|---|---|
UndefinedValueError
|
If key is missing and no default. |