Pydantic 设置
BaseSettings ¶
BaseSettings(
__pydantic_self__,
_case_sensitive: bool | None = None,
_nested_model_default_partial_update: (
bool | None
) = None,
_env_prefix: str | 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_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_exit_on_error: bool | None = None,
_cli_prefix: str | None = None,
_cli_flag_prefix_char: str | None = None,
_cli_implicit_flags: bool | None = None,
_cli_ignore_unknown_args: bool | None = None,
_cli_kebab_case: bool | None = None,
_secrets_dir: PathType | None = None,
**values: Any
)
基类: BaseModel
设置的基类,允许通过环境变量覆盖值。
这在生产环境中对于您不希望保存在代码中的密钥非常有用,它可以很好地与 docker(-compose)、Heroku 和任何 12 要素应用设计配合使用。
所有以下属性都可以通过 model_config
设置。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
_case_sensitive
|
bool | None
|
环境变量和 CLI 变量名称是否应区分大小写读取。默认为 |
None
|
_nested_model_default_partial_update
|
bool | None
|
是否允许对嵌套模型默认对象字段进行部分更新。默认为 |
None
|
_env_prefix
|
str | None
|
所有环境变量的前缀。默认为 |
None
|
_env_file
|
DotenvType | None
|
要从中加载设置值的 env 文件。默认为 |
ENV_FILE_SENTINEL
|
_env_file_encoding
|
str | None
|
env 文件的编码,例如 |
None
|
_env_ignore_empty
|
bool | None
|
忽略值为空字符串的环境变量。默认为 |
None
|
_env_nested_delimiter
|
str | None
|
嵌套 env 值的分隔符。默认为 |
None
|
_env_parse_none_str
|
str | None
|
应解析为 |
None
|
_env_parse_enums
|
bool | None
|
将枚举字段名称解析为值。默认为 |
None
|
_cli_prog_name
|
str | None
|
CLI 程序名称,用于在帮助文本中显示。如果 _cli_parse_args 为 |
None
|
_cli_parse_args
|
bool | list[str] | tuple[str, ...] | None
|
要解析的 CLI 参数列表。默认为 None。如果设置为 |
None
|
_cli_settings_source
|
CliSettingsSource[Any] | None
|
使用用户定义的实例覆盖默认的 CLI 设置源。默认为 None。 |
None
|
_cli_parse_none_str
|
str | None
|
应解析为 |
None
|
_cli_hide_none_type
|
bool | None
|
在 CLI 帮助文本中隐藏 |
None
|
_cli_avoid_json
|
bool | None
|
避免在 CLI 帮助文本中使用复杂的 JSON 对象。默认为 |
None
|
_cli_enforce_required
|
bool | None
|
在 CLI 强制执行必填字段。默认为 |
None
|
_cli_use_class_docs_for_groups
|
bool | None
|
在 CLI 组帮助文本中使用类文档字符串而不是字段描述。默认为 |
None
|
_cli_exit_on_error
|
bool | None
|
确定当发生错误时内部解析器是否退出并显示错误信息。默认为 |
None
|
_cli_prefix
|
str | None
|
根解析器命令行参数前缀。默认为 ""。 |
None
|
_cli_flag_prefix_char
|
str | None
|
用于 CLI 可选参数的标志前缀字符。默认为 '-'。 |
None
|
_cli_implicit_flags
|
bool | None
|
|
None
|
_cli_ignore_unknown_args
|
bool | None
|
是否忽略未知的 CLI 参数,仅解析已知的参数。默认为 |
None
|
_cli_kebab_case
|
bool | None
|
CLI 参数使用 kebab case。默认为 |
None
|
_secrets_dir
|
PathType | None
|
密钥文件目录或目录序列。默认为 |
None
|
源代码位于 .venv/lib/python3.12/site-packages/pydantic_settings/main.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 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 |
|
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, ...]
定义加载设置值的来源及其顺序。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
settings_cls
|
type[BaseSettings]
|
Settings 类。 |
required |
init_settings
|
PydanticBaseSettingsSource
|
|
required |
env_settings
|
PydanticBaseSettingsSource
|
|
required |
dotenv_settings
|
PydanticBaseSettingsSource
|
|
required |
file_secret_settings
|
PydanticBaseSettingsSource
|
|
required |
返回
类型 | 描述 |
---|---|
tuple[PydanticBaseSettingsSource, ...]
|
包含加载设置值的来源及其顺序的元组。 |
源代码位于 .venv/lib/python3.12/site-packages/pydantic_settings/main.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
|
CliApp ¶
一个实用程序类,用于将 Pydantic BaseSettings
、BaseModel
或 pydantic.dataclasses.dataclass
作为 CLI 应用程序运行。
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_cmd_method_name: str = "cli_cmd",
**model_init_data: Any
) -> T
将 Pydantic BaseSettings
、BaseModel
或 pydantic.dataclasses.dataclass
作为 CLI 应用程序运行。将模型作为 CLI 应用程序运行需要模型类中定义 cli_cmd
方法。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
model_cls
|
type[T]
|
要作为 CLI 应用程序运行的模型类。 |
required |
cli_args
|
list[str] | Namespace | SimpleNamespace | dict[str, Any] | None
|
要解析的 CLI 参数列表。如果指定了 |
None
|
cli_settings_source
|
CliSettingsSource[Any] | None
|
使用用户定义的实例覆盖默认的 CLI 设置源。默认为 |
None
|
cli_exit_on_error
|
bool | None
|
确定此函数是否在错误时退出。如果模型是 |
None
|
cli_cmd_method_name
|
str
|
要运行的 CLI 命令方法名称。默认为 "cli_cmd"。 |
'cli_cmd'
|
model_init_data
|
Any
|
模型初始化数据。 |
{}
|
返回
类型 | 描述 |
---|---|
T
|
运行后的模型实例。 |
引发
类型 | 描述 |
---|---|
SettingsError
|
如果 model_cls 不是 |
SettingsError
|
如果 model_cls 没有定义 |
源代码位于 .venv/lib/python3.12/site-packages/pydantic_settings/main.py
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 |
|
run_subcommand staticmethod
¶
run_subcommand(
model: PydanticModel,
cli_exit_on_error: bool | None = None,
cli_cmd_method_name: str = "cli_cmd",
) -> PydanticModel
运行模型子命令。运行模型子命令需要在嵌套模型子命令类中定义 cli_cmd
方法。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
model
|
PydanticModel
|
从中运行子命令的模型。 |
required |
cli_exit_on_error
|
bool | None
|
确定如果未找到子命令,此函数是否退出并显示错误。如果设置了 model_config |
None
|
cli_cmd_method_name
|
str
|
要运行的 CLI 命令方法名称。默认为 "cli_cmd"。 |
'cli_cmd'
|
返回
类型 | 描述 |
---|---|
PydanticModel
|
运行后的子命令模型。 |
引发
类型 | 描述 |
---|---|
SystemExit
|
当未找到子命令且 cli_exit_on_error= |
SettingsError
|
当未找到子命令且 cli_exit_on_error= |
源代码位于 .venv/lib/python3.12/site-packages/pydantic_settings/main.py
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 |
|
SettingsConfigDict ¶
基类: ConfigDict
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_exit_on_error: bool | None = None,
cli_prefix: str | None = None,
cli_flag_prefix_char: str | None = None,
cli_implicit_flags: bool | None = None,
cli_ignore_unknown_args: bool | None = None,
cli_kebab_case: bool | None = None,
case_sensitive: bool | None = True,
root_parser: Any = None,
parse_args_method: Callable[..., Any] | None = None,
add_argument_method: (
Callable[..., Any] | None
) = add_argument,
add_argument_group_method: (
Callable[..., Any] | None
) = add_argument_group,
add_parser_method: (
Callable[..., Any] | None
) = add_parser,
add_subparsers_method: (
Callable[..., Any] | None
) = add_subparsers,
formatter_class: Any = RawDescriptionHelpFormatter,
)
基类: EnvSettingsSource
, Generic[T]
用于从 CLI 加载设置值的源类。
注意
CliSettingsSource
通过使用解析器方法将 settings_cls
字段作为命令行参数添加,从而与 root_parser
对象连接。 CliSettingsSource
内部解析器表示基于 argparse
解析库,因此,要求解析器方法支持与其 argparse
库对应项相同的属性。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
cli_prog_name
|
str | None
|
CLI 程序名称,用于在帮助文本中显示。如果 cli_parse_args 为 |
None
|
cli_parse_args
|
bool | list[str] | tuple[str, ...] | None
|
要解析的 CLI 参数列表。默认为 None。如果设置为 |
None
|
cli_parse_none_str
|
str | None
|
应解析为 |
None
|
cli_hide_none_type
|
bool | None
|
在 CLI 帮助文本中隐藏 |
None
|
cli_avoid_json
|
bool | None
|
避免在 CLI 帮助文本中使用复杂的 JSON 对象。默认为 |
None
|
cli_enforce_required
|
bool | None
|
在 CLI 强制执行必填字段。默认为 |
None
|
cli_use_class_docs_for_groups
|
bool | None
|
在 CLI 组帮助文本中使用类文档字符串而不是字段描述。默认为 |
None
|
cli_exit_on_error
|
bool | None
|
确定当发生错误时内部解析器是否退出并显示错误信息。默认为 |
None
|
cli_prefix
|
str | None
|
在根解析器下添加的命令行参数的前缀。默认为 ""。 |
None
|
cli_flag_prefix_char
|
str | None
|
用于 CLI 可选参数的标志前缀字符。默认为 '-'。 |
None
|
cli_implicit_flags
|
bool | None
|
|
None
|
cli_ignore_unknown_args
|
bool | None
|
是否忽略未知的 CLI 参数,仅解析已知的参数。默认为 |
None
|
cli_kebab_case
|
bool | None
|
CLI 参数使用 kebab case。默认为 |
None
|
case_sensitive
|
bool | None
|
CLI "--arg" 名称是否应区分大小写读取。默认为 |
True
|
root_parser
|
Any
|
根解析器对象。 |
None
|
parse_args_method
|
Callable[..., Any] | None
|
根解析器 parse args 方法。默认为 |
None
|
add_argument_method
|
Callable[..., Any] | None
|
根解析器 add argument 方法。默认为 |
add_argument
|
add_argument_group_method
|
Callable[..., Any] | None
|
根解析器 add argument group 方法。默认为 |
add_argument_group
|
add_parser_method
|
Callable[..., Any] | None
|
根解析器添加新解析器(子命令)方法。默认为 |
add_parser
|
add_subparsers_method
|
Callable[..., Any] | None
|
根解析器添加子解析器(子命令)方法。默认为 |
add_subparsers
|
formatter_class
|
Any
|
用于自定义根解析器帮助文本的类。默认为 |
RawDescriptionHelpFormatter
|
源代码位于 .venv/lib/python3.12/site-packages/pydantic_settings/sources.py
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 |
|
DotEnvSettingsSource ¶
DotEnvSettingsSource(
settings_cls: type[BaseSettings],
env_file: DotenvType | None = ENV_FILE_SENTINEL,
env_file_encoding: str | None = None,
case_sensitive: bool | None = None,
env_prefix: str | None = None,
env_nested_delimiter: str | None = None,
env_ignore_empty: bool | None = None,
env_parse_none_str: str | None = None,
env_parse_enums: bool | None = None,
)
用于从 env 文件加载设置值的源类。
源代码位于 .venv/lib/python3.12/site-packages/pydantic_settings/sources.py
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 |
|
EnvSettingsSource ¶
EnvSettingsSource(
settings_cls: type[BaseSettings],
case_sensitive: bool | None = None,
env_prefix: str | None = None,
env_nested_delimiter: str | None = None,
env_ignore_empty: bool | None = None,
env_parse_none_str: str | None = None,
env_parse_enums: bool | None = None,
)
基类: PydanticBaseEnvSettingsSource
用于从环境变量加载设置值的源类。
源代码位于 .venv/lib/python3.12/site-packages/pydantic_settings/sources.py
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 |
|
get_field_value ¶
从环境变量获取字段的值,以及一个标志,用于确定值是否复杂。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
field
|
FieldInfo
|
字段。 |
required |
field_name
|
str
|
字段名称。 |
required |
返回
类型 | 描述 |
---|---|
tuple[Any, str, bool]
|
一个元组,包含值(如果未找到则为 |
源代码位于 .venv/lib/python3.12/site-packages/pydantic_settings/sources.py
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 |
|
prepare_field_value ¶
prepare_field_value(
field_name: str,
field: FieldInfo,
value: Any,
value_is_complex: bool,
) -> Any
准备字段的值。
- 提取嵌套字段的值。
- 将值反序列化为复杂字段的 Python 对象。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
field
|
FieldInfo
|
字段。 |
required |
field_name
|
str
|
字段名称。 |
required |
返回
类型 | 描述 |
---|---|
Any
|
一个元组,包含字段的准备好的值。 |
引发
类型 | 描述 |
---|---|
ValuesError
|
当反序列化复杂字段的值时发生错误。 |
源代码位于 .venv/lib/python3.12/site-packages/pydantic_settings/sources.py
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 815 816 817 818 819 820 |
|
next_field ¶
next_field(
field: FieldInfo | Any | None,
key: str,
case_sensitive: bool | None = None,
) -> FieldInfo | None
通过键(env 名称)在子模型中查找字段
通过以下模型
```py
class SubSubModel(BaseSettings):
dvals: Dict
class SubModel(BaseSettings):
vals: list[str]
sub_sub_model: SubSubModel
class Cfg(BaseSettings):
sub_model: SubModel
```
然后
next_field(sub_model, 'vals') 返回 SubModel
类的 vals
字段 next_field(sub_model, 'sub_sub_model') 返回 SubModel
类的 sub_sub_model
字段
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
field
|
FieldInfo | Any | None
|
字段。 |
required |
key
|
str
|
键(env 名称)。 |
required |
case_sensitive
|
bool | None
|
是否区分大小写搜索键。 |
None
|
返回
类型 | 描述 |
---|---|
FieldInfo | None
|
如果找到下一个字段,则为字段,否则为 |
源代码位于 .venv/lib/python3.12/site-packages/pydantic_settings/sources.py
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 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 |
|
explode_env_vars ¶
explode_env_vars(
field_name: str,
field: FieldInfo,
env_vars: Mapping[str, str | None],
) -> dict[str, Any]
处理 env_vars 并将包含 env_nested_delimiter 的键的值提取到嵌套字典中。
这应用于单个字段,因此按 env_var 前缀进行筛选。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
field_name
|
str
|
字段名称。 |
required |
field
|
FieldInfo
|
字段。 |
required |
env_vars
|
Mapping[str, str | None]
|
环境变量。 |
required |
返回
类型 | 描述 |
---|---|
dict[str, Any]
|
一个字典,包含从嵌套 env 值中提取的值。 |
源代码位于 .venv/lib/python3.12/site-packages/pydantic_settings/sources.py
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 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 |
|
ForceDecode ¶
强制解码字段值的注解。
InitSettingsSource ¶
InitSettingsSource(
settings_cls: type[BaseSettings],
init_kwargs: dict[str, Any],
nested_model_default_partial_update: bool | None = None,
)
基类: PydanticBaseSettingsSource
用于加载在设置类初始化期间提供的值的源类。
源代码位于 .venv/lib/python3.12/site-packages/pydantic_settings/sources.py
385 386 387 388 389 390 391 392 393 394 395 396 397 |
|
JsonConfigSettingsSource ¶
JsonConfigSettingsSource(
settings_cls: type[BaseSettings],
json_file: PathType | None = DEFAULT_PATH,
json_file_encoding: str | None = None,
)
基类: InitSettingsSource
, ConfigFileSourceMixin
一个从 JSON 文件加载变量的源类
源代码位于 .venv/lib/python3.12/site-packages/pydantic_settings/sources.py
2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 |
|
NoDecode ¶
防止解码字段值的注解。
PydanticBaseSettingsSource ¶
PydanticBaseSettingsSource(
settings_cls: type[BaseSettings],
)
基类: ABC
设置源的抽象基类,每个设置源类都应从中继承。
源代码位于 .venv/lib/python3.12/site-packages/pydantic_settings/sources.py
236 237 238 239 240 |
|
get_field_value abstractmethod
¶
获取值、模型创建的键以及一个标志,用于确定值是否复杂。
这是一个抽象方法,应在每个设置源类中重写。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
field
|
FieldInfo
|
字段。 |
required |
field_name
|
str
|
字段名称。 |
required |
返回
类型 | 描述 |
---|---|
tuple[Any, str, bool]
|
一个元组,包含值、键以及一个标志,用于确定值是否复杂。 |
源代码位于 .venv/lib/python3.12/site-packages/pydantic_settings/sources.py
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
|
field_is_complex ¶
检查字段是否复杂,如果是,则会尝试将其解析为 JSON。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
field
|
FieldInfo
|
字段。 |
required |
返回
类型 | 描述 |
---|---|
bool
|
字段是否复杂。 |
源代码位于 .venv/lib/python3.12/site-packages/pydantic_settings/sources.py
286 287 288 289 290 291 292 293 294 295 296 |
|
prepare_field_value ¶
prepare_field_value(
field_name: str,
field: FieldInfo,
value: Any,
value_is_complex: bool,
) -> Any
准备字段的值。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
field_name
|
str
|
字段名称。 |
required |
field
|
FieldInfo
|
字段。 |
required |
value
|
Any
|
必须准备的字段的值。 |
required |
value_is_complex
|
bool
|
一个标志,用于确定值是否复杂。 |
required |
返回
类型 | 描述 |
---|---|
Any
|
准备好的值。 |
源代码位于 .venv/lib/python3.12/site-packages/pydantic_settings/sources.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
|
decode_complex_value ¶
解码复杂字段的值
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
field_name
|
str
|
字段名称。 |
required |
field
|
FieldInfo
|
字段。 |
required |
value
|
Any
|
必须准备的字段的值。 |
required |
返回
类型 | 描述 |
---|---|
Any
|
解码后的值,用于进一步准备 |
源代码位于 .venv/lib/python3.12/site-packages/pydantic_settings/sources.py
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
|
PyprojectTomlConfigSettingsSource ¶
PyprojectTomlConfigSettingsSource(
settings_cls: type[BaseSettings],
toml_file: Path | None = None,
)
一个从 pyproject.toml
文件加载变量的源类。
源代码位于 .venv/lib/python3.12/site-packages/pydantic_settings/sources.py
2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 |
|
SecretsSettingsSource ¶
SecretsSettingsSource(
settings_cls: type[BaseSettings],
secrets_dir: PathType | None = None,
case_sensitive: bool | None = None,
env_prefix: str | None = None,
env_ignore_empty: bool | None = None,
env_parse_none_str: str | None = None,
env_parse_enums: bool | None = None,
)
基类: PydanticBaseEnvSettingsSource
用于从密钥文件加载设置值的源类。
源代码位于 .venv/lib/python3.12/site-packages/pydantic_settings/sources.py
629 630 631 632 633 634 635 636 637 638 639 640 641 642 |
|
find_case_path classmethod
¶
在路径目录中查找与文件名匹配的文件,可以选择忽略大小写。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
dir_path
|
Path
|
目录路径。 |
required |
file_name
|
str
|
文件名。 |
required |
case_sensitive
|
bool
|
是否区分大小写搜索文件名。 |
required |
返回
类型 | 描述 |
---|---|
Path | None
|
文件路径,如果目录中不存在该文件,则为 |
源代码位于 .venv/lib/python3.12/site-packages/pydantic_settings/sources.py
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 |
|
get_field_value ¶
从密钥文件获取字段的值,以及一个标志,用于确定值是否复杂。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
field
|
FieldInfo
|
字段。 |
required |
field_name
|
str
|
字段名称。 |
required |
返回
类型 | 描述 |
---|---|
tuple[Any, str, bool]
|
一个元组,包含值(如果文件不存在则为 |
源代码位于 .venv/lib/python3.12/site-packages/pydantic_settings/sources.py
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 |
|
TomlConfigSettingsSource ¶
TomlConfigSettingsSource(
settings_cls: type[BaseSettings],
toml_file: PathType | None = DEFAULT_PATH,
)
基类: InitSettingsSource
, ConfigFileSourceMixin
一个从 TOML 文件加载变量的源类
源代码位于 .venv/lib/python3.12/site-packages/pydantic_settings/sources.py
2043 2044 2045 2046 2047 2048 2049 2050 |
|
YamlConfigSettingsSource ¶
YamlConfigSettingsSource(
settings_cls: type[BaseSettings],
yaml_file: PathType | None = DEFAULT_PATH,
yaml_file_encoding: str | None = None,
)
基类: InitSettingsSource
, ConfigFileSourceMixin
一个从 yaml 文件加载变量的源类
源代码位于 .venv/lib/python3.12/site-packages/pydantic_settings/sources.py
2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 |
|
get_subcommand ¶
get_subcommand(
model: PydanticModel,
is_required: bool = True,
cli_exit_on_error: bool | None = None,
) -> Optional[PydanticModel]
从模型中获取子命令。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
model
|
PydanticModel
|
从中获取子命令的模型。 |
required |
is_required
|
bool
|
确定模型是否必须设置子命令,如果未找到则引发错误。默认为 |
True
|
cli_exit_on_error
|
bool | None
|
确定如果未找到子命令,此函数是否退出并显示错误。如果设置了 model_config |
None
|
返回
类型 | 描述 |
---|---|
Optional[PydanticModel]
|
找到的子命令模型,否则为 |
引发
类型 | 描述 |
---|---|
SystemExit
|
当未找到子命令且 is_required= |
SettingsError
|
当未找到子命令且 is_required= |
源代码位于 .venv/lib/python3.12/site-packages/pydantic_settings/sources.py
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 223 224 |
|