Pydantic-Einstellungen
IncompleteFieldDefinitionWarning ¶
Bases: UserWarning
Warning emitted when a field with an incomplete definition is used during settings resolution.
A field definition is incomplete when its annotation contains unresolved forward references, in which case settings sources may fail to correctly resolve its value.
SettingsError ¶
Bases: ValueError
Base exception for settings-related errors.
BaseSettings ¶
BaseSettings(
__pydantic_self__,
_case_sensitive: bool | None = None,
_nested_model_default_partial_update: (
bool | None
) = None,
_env_prefix: str | None = None,
_env_prefix_target: EnvPrefixTarget | None = None,
_env_file: DotenvType | None = ENV_FILE_SENTINEL,
_env_file_encoding: str | None = None,
_env_ignore_empty: bool | None = None,
_env_nested_delimiter: str | None = None,
_env_nested_max_split: int | None = None,
_env_parse_none_str: str | None = None,
_env_parse_enums: bool | None = None,
_cli_prog_name: str | None = None,
_cli_parse_args: (
bool | list[str] | tuple[str, ...] | None
) = None,
_cli_settings_source: (
CliSettingsSource[Any] | None
) = None,
_cli_parse_none_str: str | None = None,
_cli_hide_none_type: bool | None = None,
_cli_avoid_json: bool | None = None,
_cli_enforce_required: bool | None = None,
_cli_use_class_docs_for_groups: bool | None = None,
_cli_show_env_vars: bool | None = None,
_cli_exit_on_error: bool | None = None,
_cli_prefix: str | None = None,
_cli_flag_prefix_char: str | None = None,
_cli_implicit_flags: (
bool | Literal["dual", "toggle"] | None
) = None,
_cli_ignore_unknown_args: bool | None = None,
_cli_kebab_case: (
bool | Literal["all", "no_enums"] | None
) = None,
_cli_shortcuts: (
Mapping[str, str | list[str]] | None
) = None,
_secrets_dir: PathType | None = None,
_build_sources: (
tuple[
tuple[PydanticBaseSettingsSource, ...],
dict[str, Any],
]
| None
) = None,
**values: Any
)
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 and CLI variable names should be read with case-sensitivity.
Defaults to |
None
|
_nested_model_default_partial_update |
bool | None
|
Whether to allow partial updates on nested model default object fields.
Defaults to |
None
|
_env_prefix |
str | None
|
Prefix for all environment variables. Defaults to |
None
|
_env_prefix_target |
EnvPrefixTarget | None
|
Targets to which |
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_nested_max_split |
int | None
|
The nested env values maximum nesting. 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
|
_env_parse_enums |
bool | None
|
Parse enum field names to values. Defaults to |
None
|
_cli_prog_name |
str | None
|
The CLI program name to display in help text. Defaults to |
None
|
_cli_parse_args |
bool | list[str] | tuple[str, ...] | None
|
The list of CLI arguments to parse. Defaults to None.
If set to |
None
|
_cli_settings_source |
CliSettingsSource[Any] | None
|
Override the default CLI settings source with a user defined instance. Defaults to None. |
None
|
_cli_parse_none_str |
str | None
|
The CLI string value that should be parsed (e.g. "null", "void", "None", etc.) into
|
None
|
_cli_hide_none_type |
bool | None
|
Hide |
None
|
_cli_avoid_json |
bool | None
|
Avoid complex JSON objects in CLI help text. Defaults to |
None
|
_cli_enforce_required |
bool | None
|
Enforce required fields at the CLI. Defaults to |
None
|
_cli_use_class_docs_for_groups |
bool | None
|
Use class docstrings in CLI group help text instead of field descriptions.
Defaults to |
None
|
_cli_show_env_vars |
bool | None
|
Show resolved environment variable names in CLI help text. Defaults to |
None
|
_cli_exit_on_error |
bool | None
|
Determines whether or not the internal parser exits with error info when an error occurs.
Defaults to |
None
|
_cli_prefix |
str | None
|
The root parser command line arguments prefix. Defaults to "". |
None
|
_cli_flag_prefix_char |
str | None
|
The flag prefix character to use for CLI optional arguments. Defaults to '-'. |
None
|
_cli_implicit_flags |
bool | Literal['dual', 'toggle'] | None
|
Controls how
|
None
|
_cli_ignore_unknown_args |
bool | None
|
Whether to ignore unknown CLI args and parse only known ones. Defaults to |
None
|
_cli_kebab_case |
bool | Literal['all', 'no_enums'] | None
|
CLI args use kebab case. Defaults to |
None
|
_cli_shortcuts |
Mapping[str, str | list[str]] | None
|
Mapping of target field name to alias names. Defaults to |
None
|
_secrets_dir |
PathType | None
|
The secret files directory or a sequence of directories. Defaults to |
None
|
_build_sources |
tuple[tuple[PydanticBaseSettingsSource, ...], dict[str, Any]] | None
|
Pre-initialized sources and init kwargs to use for building instantiation values.
Defaults to |
None
|
Source code in pydantic_settings/main.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | |
settings_customise_sources
classmethod
¶
settings_customise_sources(
settings_cls: type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> tuple[PydanticBaseSettingsSource, ...]
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
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | |
CliApp ¶
A utility class for running Pydantic BaseSettings, BaseModel, or pydantic.dataclasses.dataclass as
CLI applications.
run
staticmethod
¶
run(
model_cls: type[T],
cli_args: (
list[str]
| Namespace
| SimpleNamespace
| dict[str, Any]
| None
) = None,
cli_settings_source: (
CliSettingsSource[Any] | None
) = None,
cli_exit_on_error: bool | None = None,
cli_show_env_vars: bool | None = None,
cli_cmd_method_name: str = "cli_cmd",
**model_init_data: Any
) -> T
Runs a Pydantic BaseSettings, BaseModel, or pydantic.dataclasses.dataclass as a CLI application.
Running a model as a CLI application requires the cli_cmd method to be defined in the model class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_cls |
type[T]
|
The model class to run as a CLI application. |
required |
cli_args |
list[str] | Namespace | SimpleNamespace | dict[str, Any] | None
|
The list of CLI arguments to parse. If |
None
|
cli_settings_source |
CliSettingsSource[Any] | None
|
Override the default CLI settings source with a user defined instance.
Defaults to |
None
|
cli_exit_on_error |
bool | None
|
Determines whether this function exits on error. If model is subclass of
|
None
|
cli_show_env_vars |
bool | None
|
Show resolved environment variable names in CLI help text. Defaults to |
None
|
cli_cmd_method_name |
str
|
The CLI command method name to run. Defaults to "cli_cmd". |
'cli_cmd'
|
model_init_data |
Any
|
The model init data. |
{}
|
Returns:
| Type | Description |
|---|---|
T
|
The ran instance of model. |
Raises:
| Type | Description |
|---|---|
SettingsError
|
If model_cls is not subclass of |
SettingsError
|
If model_cls does not have a |
Source code in pydantic_settings/main.py
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 | |
run_subcommand
staticmethod
¶
run_subcommand(
model: PydanticModel,
cli_exit_on_error: bool | None = None,
cli_show_env_vars: bool | None = None,
cli_cmd_method_name: str = "cli_cmd",
) -> PydanticModel
Runs the model subcommand. Running a model subcommand requires the cli_cmd method to be defined in
the nested model subcommand class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model |
PydanticModel
|
The model to run the subcommand from. |
required |
cli_exit_on_error |
bool | None
|
Determines whether this function exits with error if no subcommand is found.
Defaults to model_config |
None
|
cli_show_env_vars |
bool | None
|
Show resolved environment variable names in CLI help text. Defaults to |
None
|
cli_cmd_method_name |
str
|
The CLI command method name to run. Defaults to "cli_cmd". |
'cli_cmd'
|
Returns:
| Type | Description |
|---|---|
PydanticModel
|
The ran subcommand model. |
Raises:
| Type | Description |
|---|---|
SystemExit
|
When no subcommand is found and cli_exit_on_error= |
SettingsError
|
When no subcommand is found and cli_exit_on_error= |
Source code in pydantic_settings/main.py
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 | |
serialize
staticmethod
¶
serialize(
model: PydanticModel,
list_style: Literal[
"json", "argparse", "lazy"
] = "json",
dict_style: Literal["json", "env"] = "json",
positionals_first: bool = False,
) -> list[str]
Serializes the CLI arguments for a Pydantic data model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model |
PydanticModel
|
The data model to serialize. |
required |
list_style |
Literal['json', 'argparse', 'lazy']
|
Controls how list-valued fields are serialized on the command line.
- 'json' (default):
Lists are encoded as a single JSON array.
Example: |
'json'
|
dict_style |
Literal['json', 'env']
|
Controls how dictionary-valued fields are serialized.
- 'json' (default):
The entire dictionary is emitted as a single JSON object.
Example: |
'json'
|
positionals_first |
bool
|
Controls whether positional arguments should be serialized
first compared to optional arguments. Defaults to |
False
|
Returns:
| Type | Description |
|---|---|
list[str]
|
The serialized CLI arguments for the data model. |
Source code in pydantic_settings/main.py
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 | |
format_help
staticmethod
¶
format_help(
model: PydanticModel | type[T],
cli_settings_source: (
CliSettingsSource[Any] | None
) = None,
strip_ansi_color: bool = False,
) -> str
Return a string containing a help message for a Pydantic model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model |
PydanticModel | type[T]
|
The model or model class. |
required |
cli_settings_source |
CliSettingsSource[Any] | None
|
Override the default CLI settings source with a user defined instance.
Defaults to |
None
|
strip_ansi_color |
bool
|
Strips ANSI color codes from the help message when set to |
False
|
Returns:
| Type | Description |
|---|---|
str
|
The help message string for the model. |
Source code in pydantic_settings/main.py
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 | |
print_help
staticmethod
¶
print_help(
model: PydanticModel | type[T],
cli_settings_source: (
CliSettingsSource[Any] | None
) = None,
file: TextIO | None = None,
strip_ansi_color: bool = False,
) -> None
Print a help message for a Pydantic model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model |
PydanticModel | type[T]
|
The model or model class. |
required |
cli_settings_source |
CliSettingsSource[Any] | None
|
Override the default CLI settings source with a user defined instance.
Defaults to |
None
|
file |
TextIO | None
|
A text stream to which the help message is written. If |
None
|
strip_ansi_color |
bool
|
Strips ANSI color codes from the help message when set to |
False
|
Source code in pydantic_settings/main.py
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 | |
SettingsConfigDict ¶
Bases: ConfigDict
yaml_config_section
instance-attribute
¶
yaml_config_section: str | None
Specifies the section in a YAML file from which to load the settings. Supports dot-notation for nested paths (e.g., 'config.app.settings'). If provided, the settings will be loaded from the specified section. This is useful when the YAML file contains multiple configuration sections and you only want to load a specific subset into your settings model.
pyproject_toml_depth
instance-attribute
¶
pyproject_toml_depth: int
Number of levels up from the current working directory to attempt to find a pyproject.toml file.
This is only used when a pyproject.toml file is not found in the current working directory.
pyproject_toml_table_header
instance-attribute
¶
Header of the TOML table within a pyproject.toml file to use when filling variables.
This is supplied as a tuple[str, ...] instead of a str to accommodate for headers
containing a ..
For example, toml_table_header = ("tool", "my.tool", "foo") can be used to fill variable
values from a table with header [tool."my.tool".foo].
To use the root table, exclude this config setting or provide an empty tuple.
CliSettingsSource ¶
CliSettingsSource(
settings_cls: type[BaseSettings],
cli_prog_name: str | None = None,
cli_parse_args: (
bool | list[str] | tuple[str, ...] | None
) = None,
cli_parse_none_str: str | None = None,
cli_hide_none_type: bool | None = None,
cli_avoid_json: bool | None = None,
cli_enforce_required: bool | None = None,
cli_use_class_docs_for_groups: bool | None = None,
cli_show_env_vars: bool | None = None,
cli_exit_on_error: bool | None = None,
cli_prefix: str | None = None,
cli_flag_prefix_char: str | None = None,
cli_implicit_flags: (
bool | Literal["dual", "toggle"] | None
) = None,
cli_ignore_unknown_args: bool | None = None,
cli_kebab_case: (
bool | Literal["all", "no_enums"] | None
) = None,
cli_shortcuts: (
Mapping[str, str | list[str]] | None
) = None,
case_sensitive: bool | None = True,
root_parser: Any = None,
parse_args_method: Callable[..., Any] | None = None,
add_argument_method: (
Callable[..., Any] | None
) = ArgumentParser.add_argument,
add_argument_group_method: (
Callable[..., Any] | None
) = ArgumentParser.add_argument_group,
add_parser_method: (
Callable[..., Any] | None
) = _SubParsersAction.add_parser,
add_subparsers_method: (
Callable[..., Any] | None
) = ArgumentParser.add_subparsers,
format_help_method: (
Callable[..., Any] | None
) = ArgumentParser.format_help,
formatter_class: Any = RawDescriptionHelpFormatter,
_env_settings_source: EnvSettingsSource | None = None,
_init_state: InitState | None = None,
)
Bases: EnvSettingsSource, Generic[T]
Source class for loading settings values from CLI.
Note
A CliSettingsSource connects with a root_parser object by using the parser methods to add
settings_cls fields as command line arguments. The CliSettingsSource internal parser representation
is based upon the argparse parsing library, and therefore, requires the parser methods to support
the same attributes as their argparse library counterparts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cli_prog_name |
str | None
|
The CLI program name to display in help text. Defaults to |
None
|
cli_parse_args |
bool | list[str] | tuple[str, ...] | None
|
The list of CLI arguments to parse. Defaults to None.
If set to |
None
|
cli_parse_none_str |
str | None
|
The CLI string value that should be parsed (e.g. "null", "void", "None", etc.) into |
None
|
cli_hide_none_type |
bool | None
|
Hide |
None
|
cli_avoid_json |
bool | None
|
Avoid complex JSON objects in CLI help text. Defaults to |
None
|
cli_enforce_required |
bool | None
|
Enforce required fields at the CLI. Defaults to |
None
|
cli_use_class_docs_for_groups |
bool | None
|
Use class docstrings in CLI group help text instead of field descriptions.
Defaults to |
None
|
cli_show_env_vars |
bool | None
|
Show resolved environment variable names in CLI help text. Defaults to |
None
|
cli_exit_on_error |
bool | None
|
Determines whether or not the internal parser exits with error info when an error occurs.
Defaults to |
None
|
cli_prefix |
str | None
|
Prefix for command line arguments added under the root parser. Defaults to "". |
None
|
cli_flag_prefix_char |
str | None
|
The flag prefix character to use for CLI optional arguments. Defaults to '-'. |
None
|
cli_implicit_flags |
bool | Literal['dual', 'toggle'] | None
|
Controls how
|
None
|
cli_ignore_unknown_args |
bool | None
|
Whether to ignore unknown CLI args and parse only known ones. Defaults to |
None
|
cli_kebab_case |
bool | Literal['all', 'no_enums'] | None
|
CLI args use kebab case. Defaults to |
None
|
cli_shortcuts |
Mapping[str, str | list[str]] | None
|
Mapping of target field name to alias names. Defaults to |
None
|
case_sensitive |
bool | None
|
Whether CLI "--arg" names should be read with case-sensitivity. Defaults to |
True
|
root_parser |
Any
|
The root parser object. |
None
|
parse_args_method |
Callable[..., Any] | None
|
The root parser parse args method. Defaults to |
None
|
add_argument_method |
Callable[..., Any] | None
|
The root parser add argument method. Defaults to |
add_argument
|
add_argument_group_method |
Callable[..., Any] | None
|
The root parser add argument group method.
Defaults to |
add_argument_group
|
add_parser_method |
Callable[..., Any] | None
|
The root parser add new parser (sub-command) method.
Defaults to |
add_parser
|
add_subparsers_method |
Callable[..., Any] | None
|
The root parser add subparsers (sub-commands) method.
Defaults to |
add_subparsers
|
format_help_method |
Callable[..., Any] | None
|
The root parser format help method. Defaults to |
format_help
|
formatter_class |
Any
|
A class for customizing the root parser help text. Defaults to |
RawDescriptionHelpFormatter
|
Source code in pydantic_settings/sources/providers/cli.py
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 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 432 433 434 435 436 437 438 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 | |
DotEnvSettingsSource ¶
DotEnvSettingsSource(
settings_cls: type[BaseSettings],
env_file: DotenvType | None = ENV_FILE_SENTINEL,
env_file_encoding: str | None = None,
dotenv_filtering: DotenvFiltering | None = None,
case_sensitive: bool | None = None,
env_prefix: str | None = None,
env_prefix_target: EnvPrefixTarget | None = None,
env_nested_delimiter: str | None = None,
env_nested_max_split: int | None = None,
env_ignore_empty: bool | None = None,
env_parse_none_str: str | None = None,
env_parse_enums: bool | None = None,
_init_state: InitState | None = None,
)
Bases: EnvSettingsSource
Source class for loading settings values from env files.
Source code in pydantic_settings/sources/providers/dotenv.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
EnvSettingsSource ¶
EnvSettingsSource(
settings_cls: type[BaseSettings],
case_sensitive: bool | None = None,
env_prefix: str | None = None,
env_prefix_target: EnvPrefixTarget | None = None,
env_nested_delimiter: str | None = None,
env_nested_max_split: int | None = None,
env_ignore_empty: bool | None = None,
env_parse_none_str: str | None = None,
env_parse_enums: bool | None = None,
_init_state: InitState | None = None,
)
Bases: PydanticBaseEnvSettingsSource
Source class for loading settings values from environment variables.
Source code in pydantic_settings/sources/providers/env.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |
get_field_value ¶
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 that contains the value ( |
Source code in pydantic_settings/sources/providers/env.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
prepare_field_value ¶
prepare_field_value(
field_name: str,
field: FieldInfo,
value: Any,
value_is_complex: bool,
) -> Any
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/providers/env.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
next_field ¶
next_field(
field: FieldInfo | Any | None,
key: str,
case_sensitive: bool | None = None,
) -> FieldInfo | None
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 |
case_sensitive |
bool | None
|
Whether to search for key case sensitively. |
None
|
Returns:
| Type | Description |
|---|---|
FieldInfo | None
|
Field if it finds the next field otherwise |
Source code in pydantic_settings/sources/providers/env.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | |
explode_env_vars ¶
explode_env_vars(
field_name: str,
field: FieldInfo,
env_vars: Mapping[str, str | None],
) -> dict[str, Any]
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/providers/env.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | |
ForceDecode ¶
Annotation to force decoding of a field value.
GoogleSecretManagerSettingsSource ¶
GoogleSecretManagerSettingsSource(
settings_cls: type[BaseSettings],
credentials: Credentials | None = None,
project_id: str | None = None,
env_prefix: str | None = None,
env_parse_none_str: str | None = None,
env_parse_enums: bool | None = None,
secret_client: SecretManagerServiceClient | None = None,
case_sensitive: bool | None = True,
project_id_field: str = "project_id",
_init_state: InitState | None = None,
)
Bases: EnvSettingsSource
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_id |
str | None
|
The GCP project to read secrets from. If not provided, it is resolved lazily (see below). |
None
|
project_id_field |
str
|
The key, populated by a previous (higher priority)
settings source, to use as the |
'project_id'
|
The project_id is resolved lazily in :meth:__call__ rather than at
construction time so that it can be sourced from settings resolved by previous
sources (only available via current_state once the source is called).
Resolution order is:
- the explicit
project_idargument - the
project_id_fieldvalue from previous settings sources google.auth.default()
Source code in pydantic_settings/sources/providers/gcp.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | |
get_field_value ¶
Override get_field_value to get the secret value from GCP Secret Manager. Look for a SecretVersion metadata field to specify a particular SecretVersion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field |
FieldInfo
|
The field to get the value for |
required |
field_name |
str
|
The declared name of the field |
required |
Returns:
| Type | Description |
|---|---|
Any
|
A tuple of (value, key, value_is_complex), where |
str
|
to populate the model (either the field name or an alias, depending on |
bool
|
configuration). |
Source code in pydantic_settings/sources/providers/gcp.py
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | |
InitSettingsSource ¶
InitSettingsSource(
settings_cls: type[BaseSettings],
init_kwargs: dict[str, Any],
nested_model_default_partial_update: bool | None = None,
_init_state: InitState | None = None,
)
Bases: PydanticBaseSettingsSource
Source class for loading values provided during settings class initialization.
Source code in pydantic_settings/sources/base.py
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | |
JsonConfigSettingsSource ¶
JsonConfigSettingsSource(
settings_cls: type[BaseSettings],
json_file: ConfigFileSourceType | None = DEFAULT_PATH,
json_file_encoding: str | None = None,
deep_merge: bool = False,
_init_state: InitState | None = None,
)
Bases: InitSettingsSource, ConfigFileSourceMixin
A source class that loads variables from a JSON file
Source code in pydantic_settings/sources/providers/json.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
NestedSecretsSettingsSource ¶
NestedSecretsSettingsSource(
file_secret_settings: (
PydanticBaseSettingsSource | SecretsSettingsSource
),
secrets_dir: Optional[PathType] = None,
secrets_dir_missing: (
Literal["ok", "warn", "error"] | None
) = None,
secrets_dir_max_size: int | None = None,
secrets_case_sensitive: bool | None = None,
secrets_prefix: str | None = None,
secrets_nested_delimiter: str | None = None,
secrets_nested_subdir: bool | None = None,
case_sensitive: bool | None = None,
env_prefix: str | None = None,
_init_state: InitState | None = None,
)
Bases: EnvSettingsSource
Source code in pydantic_settings/sources/providers/nested_secrets.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
NoDecode ¶
Annotation to prevent decoding of a field value.
PydanticBaseSettingsSource ¶
PydanticBaseSettingsSource(
settings_cls: type[BaseSettings],
_init_state: InitState | None = None,
)
Bases: ABC
Abstract base class for settings sources, every settings source classes should inherit from it.
Source code in pydantic_settings/sources/base.py
111 112 113 114 115 116 | |
current_state
property
¶
The current state of the settings, populated by the previous settings sources.
settings_sources_data
property
¶
The state of all previous settings sources.
get_field_value
abstractmethod
¶
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 that contains the value, key and a flag to determine whether value is complex. |
Source code in pydantic_settings/sources/base.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |
field_is_complex ¶
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/base.py
161 162 163 164 165 166 167 168 169 170 171 | |
prepare_field_value ¶
prepare_field_value(
field_name: str,
field: FieldInfo,
value: Any,
value_is_complex: bool,
) -> Any
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/base.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | |
decode_complex_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/base.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
PyprojectTomlConfigSettingsSource ¶
PyprojectTomlConfigSettingsSource(
settings_cls: type[BaseSettings],
toml_file: Path | None = None,
_init_state: InitState | None = None,
)
Bases: TomlConfigSettingsSource
A source class that loads variables from a pyproject.toml file.
Source code in pydantic_settings/sources/providers/pyproject.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
SecretsSettingsSource ¶
SecretsSettingsSource(
settings_cls: type[BaseSettings],
secrets_dir: PathType | None = None,
case_sensitive: bool | None = None,
env_prefix: str | None = None,
env_prefix_target: EnvPrefixTarget | None = None,
env_ignore_empty: bool | None = None,
env_parse_none_str: str | None = None,
env_parse_enums: bool | None = None,
_init_state: InitState | None = None,
)
Bases: PydanticBaseEnvSettingsSource
Source class for loading settings values from secret files.
Source code in pydantic_settings/sources/providers/secrets.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
find_case_path
classmethod
¶
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/providers/secrets.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
get_field_value ¶
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 that contains the value ( |
Source code in pydantic_settings/sources/providers/secrets.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
TomlConfigSettingsSource ¶
TomlConfigSettingsSource(
settings_cls: type[BaseSettings],
toml_file: ConfigFileSourceType | None = DEFAULT_PATH,
toml_table_header: tuple[str, ...] = (),
deep_merge: bool = False,
_init_state: InitState | None = None,
)
Bases: InitSettingsSource, ConfigFileSourceMixin
A source class that loads variables from a TOML file
Source code in pydantic_settings/sources/providers/toml.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
YamlConfigSettingsSource ¶
YamlConfigSettingsSource(
settings_cls: type[BaseSettings],
yaml_file: ConfigFileSourceType | None = DEFAULT_PATH,
yaml_file_encoding: str | None = None,
yaml_config_section: str | None = None,
deep_merge: bool = False,
_init_state: InitState | None = None,
)
Bases: InitSettingsSource, ConfigFileSourceMixin
A source class that loads variables from a yaml file
Source code in pydantic_settings/sources/providers/yaml.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
get_subcommand ¶
get_subcommand(
model: PydanticModel,
is_required: bool = True,
cli_exit_on_error: bool | None = None,
_suppress_errors: (
list[SettingsError | SystemExit] | None
) = None,
) -> PydanticModel | None
Get the subcommand from a model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model |
PydanticModel
|
The model to get the subcommand from. |
required |
is_required |
bool
|
Determines whether a model must have subcommand set and raises error if not
found. Defaults to |
True
|
cli_exit_on_error |
bool | None
|
Determines whether this function exits with error if no subcommand is found.
Defaults to model_config |
None
|
Returns:
| Type | Description |
|---|---|
PydanticModel | None
|
The subcommand model if found, otherwise |
Raises:
| Type | Description |
|---|---|
SystemExit
|
When no subcommand is found and is_required= |
SettingsError
|
When no subcommand is found and is_required= |
Source code in pydantic_settings/sources/base.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 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 97 98 99 100 101 102 103 | |
本文总阅读量次