Pydantic Settings
BaseSettings ¶
BaseSettings(
__pydantic_self__,
_case_sensitive=None,
_env_prefix=None,
_env_file=ENV_FILE_SENTINEL,
_env_file_encoding=None,
_env_ignore_empty=None,
_env_nested_delimiter=None,
_env_parse_none_str=None,
_secrets_dir=None,
**values
)
Bases: BaseModel
Base class for settings, allowing values to be overridden by environment variables.
This is useful in production for secrets you do not wish to save in code, it plays nicely with docker(-compose), Heroku and any 12 factor app design.
All the below attributes can be set via model_config
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
_case_sensitive |
bool | None
|
Whether environment variables names should be read with case-sensitivity. Defaults to |
None
|
_env_prefix |
str | None
|
Prefix for all environment variables. Defaults to |
None
|
_env_file |
DotenvType | None
|
The env file(s) to load settings values from. Defaults to |
ENV_FILE_SENTINEL
|
_env_file_encoding |
str | None
|
The env file encoding, e.g. |
None
|
_env_ignore_empty |
bool | None
|
Ignore environment variables where the value is an empty string. Default to |
None
|
_env_nested_delimiter |
str | None
|
The nested env values delimiter. Defaults to |
None
|
_env_parse_none_str |
str | None
|
The env string value that should be parsed (e.g. "null", "void", "None", etc.)
into |
None
|
_secrets_dir |
str | Path | None
|
The secret files directory. Defaults to |
None
|
Source code in pydantic_settings/main.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
settings_customise_sources
classmethod
¶
settings_customise_sources(
settings_cls,
init_settings,
env_settings,
dotenv_settings,
file_secret_settings,
)
Define the sources and their order for loading the settings values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
settings_cls |
type[BaseSettings]
|
The Settings class. |
required |
init_settings |
PydanticBaseSettingsSource
|
The |
required |
env_settings |
PydanticBaseSettingsSource
|
The |
required |
dotenv_settings |
PydanticBaseSettingsSource
|
The |
required |
file_secret_settings |
PydanticBaseSettingsSource
|
The |
required |
Returns:
Type | Description |
---|---|
tuple[PydanticBaseSettingsSource, ...]
|
A tuple containing the sources and their order for loading the settings values. |
Source code in pydantic_settings/main.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
|
DotEnvSettingsSource ¶
DotEnvSettingsSource(
settings_cls,
env_file=ENV_FILE_SENTINEL,
env_file_encoding=None,
case_sensitive=None,
env_prefix=None,
env_nested_delimiter=None,
env_ignore_empty=None,
env_parse_none_str=None,
)
Bases: EnvSettingsSource
Source class for loading settings values from env files.
Source code in pydantic_settings/sources.py
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 |
|
EnvSettingsSource ¶
EnvSettingsSource(
settings_cls,
case_sensitive=None,
env_prefix=None,
env_nested_delimiter=None,
env_ignore_empty=None,
env_parse_none_str=None,
)
Bases: PydanticBaseEnvSettingsSource
Source class for loading settings values from environment variables.
Source code in pydantic_settings/sources.py
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
|
get_field_value ¶
get_field_value(field, field_name)
Gets the value for field from environment variables and a flag to determine whether value is complex.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field |
FieldInfo
|
The field. |
required |
field_name |
str
|
The field name. |
required |
Returns:
Type | Description |
---|---|
tuple[Any, str, bool]
|
A tuple contains the key, value if the file exists otherwise |
Source code in pydantic_settings/sources.py
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
|
prepare_field_value ¶
prepare_field_value(
field_name, field, value, value_is_complex
)
Prepare value for the field.
- Extract value for nested field.
- Deserialize value to python object for complex field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field |
FieldInfo
|
The field. |
required |
field_name |
str
|
The field name. |
required |
Returns:
Type | Description |
---|---|
Any
|
A tuple contains prepared value for the field. |
Raises:
Type | Description |
---|---|
ValuesError
|
When There is an error in deserializing value for complex field. |
Source code in pydantic_settings/sources.py
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 |
|
next_field
staticmethod
¶
next_field(field, key)
Find the field in a sub model by key(env name)
By having the following models:
```py
class SubSubModel(BaseSettings):
dvals: Dict
class SubModel(BaseSettings):
vals: list[str]
sub_sub_model: SubSubModel
class Cfg(BaseSettings):
sub_model: SubModel
```
Then
next_field(sub_model, 'vals') Returns the vals
field of SubModel
class
next_field(sub_model, 'sub_sub_model') Returns sub_sub_model
field of SubModel
class
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field |
FieldInfo | Any | None
|
The field. |
required |
key |
str
|
The key (env name). |
required |
Returns:
Type | Description |
---|---|
FieldInfo | None
|
Field if it finds the next field otherwise |
Source code in pydantic_settings/sources.py
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 |
|
explode_env_vars ¶
explode_env_vars(field_name, field, env_vars)
Process env_vars and extract the values of keys containing env_nested_delimiter into nested dictionaries.
This is applied to a single field, hence filtering by env_var prefix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field_name |
str
|
The field name. |
required |
field |
FieldInfo
|
The field. |
required |
env_vars |
Mapping[str, str | None]
|
Environment variables. |
required |
Returns:
Type | Description |
---|---|
dict[str, Any]
|
A dictionary contains extracted values from nested env values. |
Source code in pydantic_settings/sources.py
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 |
|
InitSettingsSource ¶
InitSettingsSource(settings_cls, init_kwargs)
Bases: PydanticBaseSettingsSource
Source class for loading values provided during settings class initialization.
Source code in pydantic_settings/sources.py
159 160 161 |
|
JsonConfigSettingsSource ¶
JsonConfigSettingsSource(
settings_cls,
json_file=DEFAULT_PATH,
json_file_encoding=None,
)
Bases: InitSettingsSource
, ConfigFileSourceMixin
A source class that loads variables from a JSON file
Source code in pydantic_settings/sources.py
738 739 740 741 742 743 744 745 746 747 748 749 750 751 |
|
PydanticBaseSettingsSource ¶
PydanticBaseSettingsSource(settings_cls)
Bases: ABC
Abstract base class for settings sources, every settings source classes should inherit from it.
Source code in pydantic_settings/sources.py
86 87 88 |
|
get_field_value
abstractmethod
¶
get_field_value(field, field_name)
Gets the value, the key for model creation, and a flag to determine whether value is complex.
This is an abstract method that should be overridden in every settings source classes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field |
FieldInfo
|
The field. |
required |
field_name |
str
|
The field name. |
required |
Returns:
Type | Description |
---|---|
tuple[Any, str, bool]
|
A tuple contains the key, value and a flag to determine whether value is complex. |
Source code in pydantic_settings/sources.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
|
field_is_complex ¶
field_is_complex(field)
Checks whether a field is complex, in which case it will attempt to be parsed as JSON.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field |
FieldInfo
|
The field. |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether the field is complex. |
Source code in pydantic_settings/sources.py
106 107 108 109 110 111 112 113 114 115 116 |
|
prepare_field_value ¶
prepare_field_value(
field_name, field, value, value_is_complex
)
Prepares the value of a field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field_name |
str
|
The field name. |
required |
field |
FieldInfo
|
The field. |
required |
value |
Any
|
The value of the field that has to be prepared. |
required |
value_is_complex |
bool
|
A flag to determine whether value is complex. |
required |
Returns:
Type | Description |
---|---|
Any
|
The prepared value. |
Source code in pydantic_settings/sources.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
decode_complex_value ¶
decode_complex_value(field_name, field, value)
Decode the value for a complex field
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field_name |
str
|
The field name. |
required |
field |
FieldInfo
|
The field. |
required |
value |
Any
|
The value of the field that has to be prepared. |
required |
Returns:
Type | Description |
---|---|
Any
|
The decoded value for further preparation |
Source code in pydantic_settings/sources.py
135 136 137 138 139 140 141 142 143 144 145 146 147 |
|
SecretsSettingsSource ¶
SecretsSettingsSource(
settings_cls,
secrets_dir=None,
case_sensitive=None,
env_prefix=None,
env_ignore_empty=None,
env_parse_none_str=None,
)
Bases: PydanticBaseEnvSettingsSource
Source class for loading settings values from secret files.
Source code in pydantic_settings/sources.py
352 353 354 355 356 357 358 359 360 361 362 |
|
find_case_path
classmethod
¶
find_case_path(dir_path, file_name, case_sensitive)
Find a file within path's directory matching filename, optionally ignoring case.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dir_path |
Path
|
Directory path. |
required |
file_name |
str
|
File name. |
required |
case_sensitive |
bool
|
Whether to search for file name case sensitively. |
required |
Returns:
Type | Description |
---|---|
Path | None
|
Whether file path or |
Source code in pydantic_settings/sources.py
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
|
get_field_value ¶
get_field_value(field, field_name)
Gets the value for field from secret file and a flag to determine whether value is complex.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field |
FieldInfo
|
The field. |
required |
field_name |
str
|
The field name. |
required |
Returns:
Type | Description |
---|---|
tuple[Any, str, bool]
|
A tuple contains the key, value if the file exists otherwise |
Source code in pydantic_settings/sources.py
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
|
TomlConfigSettingsSource ¶
TomlConfigSettingsSource(
settings_cls, toml_file=DEFAULT_PATH
)
Bases: InitSettingsSource
, ConfigFileSourceMixin
A source class that loads variables from a JSON file
Source code in pydantic_settings/sources.py
763 764 765 766 767 768 769 770 |
|
YamlConfigSettingsSource ¶
YamlConfigSettingsSource(
settings_cls,
yaml_file=DEFAULT_PATH,
yaml_file_encoding=None,
)
Bases: InitSettingsSource
, ConfigFileSourceMixin
A source class that loads variables from a yaml file
Source code in pydantic_settings/sources.py
785 786 787 788 789 790 791 792 793 794 795 796 797 798 |
|