main.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. from __future__ import annotations as _annotations
  2. import asyncio
  3. import inspect
  4. import threading
  5. import warnings
  6. from argparse import Namespace
  7. from collections.abc import Mapping
  8. from types import SimpleNamespace
  9. from typing import Any, ClassVar, TypeVar
  10. from pydantic import ConfigDict
  11. from pydantic._internal._config import config_keys
  12. from pydantic._internal._signature import _field_name_for_signature
  13. from pydantic._internal._utils import deep_update, is_model_class
  14. from pydantic.dataclasses import is_pydantic_dataclass
  15. from pydantic.main import BaseModel
  16. from .exceptions import SettingsError
  17. from .sources import (
  18. ENV_FILE_SENTINEL,
  19. CliSettingsSource,
  20. DefaultSettingsSource,
  21. DotEnvSettingsSource,
  22. DotenvType,
  23. EnvSettingsSource,
  24. InitSettingsSource,
  25. JsonConfigSettingsSource,
  26. PathType,
  27. PydanticBaseSettingsSource,
  28. PydanticModel,
  29. PyprojectTomlConfigSettingsSource,
  30. SecretsSettingsSource,
  31. TomlConfigSettingsSource,
  32. YamlConfigSettingsSource,
  33. get_subcommand,
  34. )
  35. T = TypeVar('T')
  36. class SettingsConfigDict(ConfigDict, total=False):
  37. case_sensitive: bool
  38. nested_model_default_partial_update: bool | None
  39. env_prefix: str
  40. env_file: DotenvType | None
  41. env_file_encoding: str | None
  42. env_ignore_empty: bool
  43. env_nested_delimiter: str | None
  44. env_nested_max_split: int | None
  45. env_parse_none_str: str | None
  46. env_parse_enums: bool | None
  47. cli_prog_name: str | None
  48. cli_parse_args: bool | list[str] | tuple[str, ...] | None
  49. cli_parse_none_str: str | None
  50. cli_hide_none_type: bool
  51. cli_avoid_json: bool
  52. cli_enforce_required: bool
  53. cli_use_class_docs_for_groups: bool
  54. cli_exit_on_error: bool
  55. cli_prefix: str
  56. cli_flag_prefix_char: str
  57. cli_implicit_flags: bool | None
  58. cli_ignore_unknown_args: bool | None
  59. cli_kebab_case: bool | None
  60. cli_shortcuts: Mapping[str, str | list[str]] | None
  61. secrets_dir: PathType | None
  62. json_file: PathType | None
  63. json_file_encoding: str | None
  64. yaml_file: PathType | None
  65. yaml_file_encoding: str | None
  66. yaml_config_section: str | None
  67. """
  68. Specifies the top-level key in a YAML file from which to load the settings.
  69. If provided, the settings will be loaded from the nested section under this key.
  70. This is useful when the YAML file contains multiple configuration sections
  71. and you only want to load a specific subset into your settings model.
  72. """
  73. pyproject_toml_depth: int
  74. """
  75. Number of levels **up** from the current working directory to attempt to find a pyproject.toml
  76. file.
  77. This is only used when a pyproject.toml file is not found in the current working directory.
  78. """
  79. pyproject_toml_table_header: tuple[str, ...]
  80. """
  81. Header of the TOML table within a pyproject.toml file to use when filling variables.
  82. This is supplied as a `tuple[str, ...]` instead of a `str` to accommodate for headers
  83. containing a `.`.
  84. For example, `toml_table_header = ("tool", "my.tool", "foo")` can be used to fill variable
  85. values from a table with header `[tool."my.tool".foo]`.
  86. To use the root table, exclude this config setting or provide an empty tuple.
  87. """
  88. toml_file: PathType | None
  89. enable_decoding: bool
  90. # Extend `config_keys` by pydantic settings config keys to
  91. # support setting config through class kwargs.
  92. # Pydantic uses `config_keys` in `pydantic._internal._config.ConfigWrapper.for_model`
  93. # to extract config keys from model kwargs, So, by adding pydantic settings keys to
  94. # `config_keys`, they will be considered as valid config keys and will be collected
  95. # by Pydantic.
  96. config_keys |= set(SettingsConfigDict.__annotations__.keys())
  97. class BaseSettings(BaseModel):
  98. """
  99. Base class for settings, allowing values to be overridden by environment variables.
  100. This is useful in production for secrets you do not wish to save in code, it plays nicely with docker(-compose),
  101. Heroku and any 12 factor app design.
  102. All the below attributes can be set via `model_config`.
  103. Args:
  104. _case_sensitive: Whether environment and CLI variable names should be read with case-sensitivity.
  105. Defaults to `None`.
  106. _nested_model_default_partial_update: Whether to allow partial updates on nested model default object fields.
  107. Defaults to `False`.
  108. _env_prefix: Prefix for all environment variables. Defaults to `None`.
  109. _env_file: The env file(s) to load settings values from. Defaults to `Path('')`, which
  110. means that the value from `model_config['env_file']` should be used. You can also pass
  111. `None` to indicate that environment variables should not be loaded from an env file.
  112. _env_file_encoding: The env file encoding, e.g. `'latin-1'`. Defaults to `None`.
  113. _env_ignore_empty: Ignore environment variables where the value is an empty string. Default to `False`.
  114. _env_nested_delimiter: The nested env values delimiter. Defaults to `None`.
  115. _env_nested_max_split: The nested env values maximum nesting. Defaults to `None`, which means no limit.
  116. _env_parse_none_str: The env string value that should be parsed (e.g. "null", "void", "None", etc.)
  117. into `None` type(None). Defaults to `None` type(None), which means no parsing should occur.
  118. _env_parse_enums: Parse enum field names to values. Defaults to `None.`, which means no parsing should occur.
  119. _cli_prog_name: The CLI program name to display in help text. Defaults to `None` if _cli_parse_args is `None`.
  120. Otherwise, defaults to sys.argv[0].
  121. _cli_parse_args: The list of CLI arguments to parse. Defaults to None.
  122. If set to `True`, defaults to sys.argv[1:].
  123. _cli_settings_source: Override the default CLI settings source with a user defined instance. Defaults to None.
  124. _cli_parse_none_str: The CLI string value that should be parsed (e.g. "null", "void", "None", etc.) into
  125. `None` type(None). Defaults to _env_parse_none_str value if set. Otherwise, defaults to "null" if
  126. _cli_avoid_json is `False`, and "None" if _cli_avoid_json is `True`.
  127. _cli_hide_none_type: Hide `None` values in CLI help text. Defaults to `False`.
  128. _cli_avoid_json: Avoid complex JSON objects in CLI help text. Defaults to `False`.
  129. _cli_enforce_required: Enforce required fields at the CLI. Defaults to `False`.
  130. _cli_use_class_docs_for_groups: Use class docstrings in CLI group help text instead of field descriptions.
  131. Defaults to `False`.
  132. _cli_exit_on_error: Determines whether or not the internal parser exits with error info when an error occurs.
  133. Defaults to `True`.
  134. _cli_prefix: The root parser command line arguments prefix. Defaults to "".
  135. _cli_flag_prefix_char: The flag prefix character to use for CLI optional arguments. Defaults to '-'.
  136. _cli_implicit_flags: Whether `bool` fields should be implicitly converted into CLI boolean flags.
  137. (e.g. --flag, --no-flag). Defaults to `False`.
  138. _cli_ignore_unknown_args: Whether to ignore unknown CLI args and parse only known ones. Defaults to `False`.
  139. _cli_kebab_case: CLI args use kebab case. Defaults to `False`.
  140. _cli_shortcuts: Mapping of target field name to alias names. Defaults to `None`.
  141. _secrets_dir: The secret files directory or a sequence of directories. Defaults to `None`.
  142. """
  143. def __init__(
  144. __pydantic_self__,
  145. _case_sensitive: bool | None = None,
  146. _nested_model_default_partial_update: bool | None = None,
  147. _env_prefix: str | None = None,
  148. _env_file: DotenvType | None = ENV_FILE_SENTINEL,
  149. _env_file_encoding: str | None = None,
  150. _env_ignore_empty: bool | None = None,
  151. _env_nested_delimiter: str | None = None,
  152. _env_nested_max_split: int | None = None,
  153. _env_parse_none_str: str | None = None,
  154. _env_parse_enums: bool | None = None,
  155. _cli_prog_name: str | None = None,
  156. _cli_parse_args: bool | list[str] | tuple[str, ...] | None = None,
  157. _cli_settings_source: CliSettingsSource[Any] | None = None,
  158. _cli_parse_none_str: str | None = None,
  159. _cli_hide_none_type: bool | None = None,
  160. _cli_avoid_json: bool | None = None,
  161. _cli_enforce_required: bool | None = None,
  162. _cli_use_class_docs_for_groups: bool | None = None,
  163. _cli_exit_on_error: bool | None = None,
  164. _cli_prefix: str | None = None,
  165. _cli_flag_prefix_char: str | None = None,
  166. _cli_implicit_flags: bool | None = None,
  167. _cli_ignore_unknown_args: bool | None = None,
  168. _cli_kebab_case: bool | None = None,
  169. _cli_shortcuts: Mapping[str, str | list[str]] | None = None,
  170. _secrets_dir: PathType | None = None,
  171. **values: Any,
  172. ) -> None:
  173. super().__init__(
  174. **__pydantic_self__._settings_build_values(
  175. values,
  176. _case_sensitive=_case_sensitive,
  177. _nested_model_default_partial_update=_nested_model_default_partial_update,
  178. _env_prefix=_env_prefix,
  179. _env_file=_env_file,
  180. _env_file_encoding=_env_file_encoding,
  181. _env_ignore_empty=_env_ignore_empty,
  182. _env_nested_delimiter=_env_nested_delimiter,
  183. _env_nested_max_split=_env_nested_max_split,
  184. _env_parse_none_str=_env_parse_none_str,
  185. _env_parse_enums=_env_parse_enums,
  186. _cli_prog_name=_cli_prog_name,
  187. _cli_parse_args=_cli_parse_args,
  188. _cli_settings_source=_cli_settings_source,
  189. _cli_parse_none_str=_cli_parse_none_str,
  190. _cli_hide_none_type=_cli_hide_none_type,
  191. _cli_avoid_json=_cli_avoid_json,
  192. _cli_enforce_required=_cli_enforce_required,
  193. _cli_use_class_docs_for_groups=_cli_use_class_docs_for_groups,
  194. _cli_exit_on_error=_cli_exit_on_error,
  195. _cli_prefix=_cli_prefix,
  196. _cli_flag_prefix_char=_cli_flag_prefix_char,
  197. _cli_implicit_flags=_cli_implicit_flags,
  198. _cli_ignore_unknown_args=_cli_ignore_unknown_args,
  199. _cli_kebab_case=_cli_kebab_case,
  200. _cli_shortcuts=_cli_shortcuts,
  201. _secrets_dir=_secrets_dir,
  202. )
  203. )
  204. @classmethod
  205. def settings_customise_sources(
  206. cls,
  207. settings_cls: type[BaseSettings],
  208. init_settings: PydanticBaseSettingsSource,
  209. env_settings: PydanticBaseSettingsSource,
  210. dotenv_settings: PydanticBaseSettingsSource,
  211. file_secret_settings: PydanticBaseSettingsSource,
  212. ) -> tuple[PydanticBaseSettingsSource, ...]:
  213. """
  214. Define the sources and their order for loading the settings values.
  215. Args:
  216. settings_cls: The Settings class.
  217. init_settings: The `InitSettingsSource` instance.
  218. env_settings: The `EnvSettingsSource` instance.
  219. dotenv_settings: The `DotEnvSettingsSource` instance.
  220. file_secret_settings: The `SecretsSettingsSource` instance.
  221. Returns:
  222. A tuple containing the sources and their order for loading the settings values.
  223. """
  224. return init_settings, env_settings, dotenv_settings, file_secret_settings
  225. def _settings_build_values(
  226. self,
  227. init_kwargs: dict[str, Any],
  228. _case_sensitive: bool | None = None,
  229. _nested_model_default_partial_update: bool | None = None,
  230. _env_prefix: str | None = None,
  231. _env_file: DotenvType | None = None,
  232. _env_file_encoding: str | None = None,
  233. _env_ignore_empty: bool | None = None,
  234. _env_nested_delimiter: str | None = None,
  235. _env_nested_max_split: int | None = None,
  236. _env_parse_none_str: str | None = None,
  237. _env_parse_enums: bool | None = None,
  238. _cli_prog_name: str | None = None,
  239. _cli_parse_args: bool | list[str] | tuple[str, ...] | None = None,
  240. _cli_settings_source: CliSettingsSource[Any] | None = None,
  241. _cli_parse_none_str: str | None = None,
  242. _cli_hide_none_type: bool | None = None,
  243. _cli_avoid_json: bool | None = None,
  244. _cli_enforce_required: bool | None = None,
  245. _cli_use_class_docs_for_groups: bool | None = None,
  246. _cli_exit_on_error: bool | None = None,
  247. _cli_prefix: str | None = None,
  248. _cli_flag_prefix_char: str | None = None,
  249. _cli_implicit_flags: bool | None = None,
  250. _cli_ignore_unknown_args: bool | None = None,
  251. _cli_kebab_case: bool | None = None,
  252. _cli_shortcuts: Mapping[str, str | list[str]] | None = None,
  253. _secrets_dir: PathType | None = None,
  254. ) -> dict[str, Any]:
  255. # Determine settings config values
  256. case_sensitive = _case_sensitive if _case_sensitive is not None else self.model_config.get('case_sensitive')
  257. env_prefix = _env_prefix if _env_prefix is not None else self.model_config.get('env_prefix')
  258. nested_model_default_partial_update = (
  259. _nested_model_default_partial_update
  260. if _nested_model_default_partial_update is not None
  261. else self.model_config.get('nested_model_default_partial_update')
  262. )
  263. env_file = _env_file if _env_file != ENV_FILE_SENTINEL else self.model_config.get('env_file')
  264. env_file_encoding = (
  265. _env_file_encoding if _env_file_encoding is not None else self.model_config.get('env_file_encoding')
  266. )
  267. env_ignore_empty = (
  268. _env_ignore_empty if _env_ignore_empty is not None else self.model_config.get('env_ignore_empty')
  269. )
  270. env_nested_delimiter = (
  271. _env_nested_delimiter
  272. if _env_nested_delimiter is not None
  273. else self.model_config.get('env_nested_delimiter')
  274. )
  275. env_nested_max_split = (
  276. _env_nested_max_split
  277. if _env_nested_max_split is not None
  278. else self.model_config.get('env_nested_max_split')
  279. )
  280. env_parse_none_str = (
  281. _env_parse_none_str if _env_parse_none_str is not None else self.model_config.get('env_parse_none_str')
  282. )
  283. env_parse_enums = _env_parse_enums if _env_parse_enums is not None else self.model_config.get('env_parse_enums')
  284. cli_prog_name = _cli_prog_name if _cli_prog_name is not None else self.model_config.get('cli_prog_name')
  285. cli_parse_args = _cli_parse_args if _cli_parse_args is not None else self.model_config.get('cli_parse_args')
  286. cli_settings_source = (
  287. _cli_settings_source if _cli_settings_source is not None else self.model_config.get('cli_settings_source')
  288. )
  289. cli_parse_none_str = (
  290. _cli_parse_none_str if _cli_parse_none_str is not None else self.model_config.get('cli_parse_none_str')
  291. )
  292. cli_parse_none_str = cli_parse_none_str if not env_parse_none_str else env_parse_none_str
  293. cli_hide_none_type = (
  294. _cli_hide_none_type if _cli_hide_none_type is not None else self.model_config.get('cli_hide_none_type')
  295. )
  296. cli_avoid_json = _cli_avoid_json if _cli_avoid_json is not None else self.model_config.get('cli_avoid_json')
  297. cli_enforce_required = (
  298. _cli_enforce_required
  299. if _cli_enforce_required is not None
  300. else self.model_config.get('cli_enforce_required')
  301. )
  302. cli_use_class_docs_for_groups = (
  303. _cli_use_class_docs_for_groups
  304. if _cli_use_class_docs_for_groups is not None
  305. else self.model_config.get('cli_use_class_docs_for_groups')
  306. )
  307. cli_exit_on_error = (
  308. _cli_exit_on_error if _cli_exit_on_error is not None else self.model_config.get('cli_exit_on_error')
  309. )
  310. cli_prefix = _cli_prefix if _cli_prefix is not None else self.model_config.get('cli_prefix')
  311. cli_flag_prefix_char = (
  312. _cli_flag_prefix_char
  313. if _cli_flag_prefix_char is not None
  314. else self.model_config.get('cli_flag_prefix_char')
  315. )
  316. cli_implicit_flags = (
  317. _cli_implicit_flags if _cli_implicit_flags is not None else self.model_config.get('cli_implicit_flags')
  318. )
  319. cli_ignore_unknown_args = (
  320. _cli_ignore_unknown_args
  321. if _cli_ignore_unknown_args is not None
  322. else self.model_config.get('cli_ignore_unknown_args')
  323. )
  324. cli_kebab_case = _cli_kebab_case if _cli_kebab_case is not None else self.model_config.get('cli_kebab_case')
  325. cli_shortcuts = _cli_shortcuts if _cli_shortcuts is not None else self.model_config.get('cli_shortcuts')
  326. secrets_dir = _secrets_dir if _secrets_dir is not None else self.model_config.get('secrets_dir')
  327. # Configure built-in sources
  328. default_settings = DefaultSettingsSource(
  329. self.__class__, nested_model_default_partial_update=nested_model_default_partial_update
  330. )
  331. init_settings = InitSettingsSource(
  332. self.__class__,
  333. init_kwargs=init_kwargs,
  334. nested_model_default_partial_update=nested_model_default_partial_update,
  335. )
  336. env_settings = EnvSettingsSource(
  337. self.__class__,
  338. case_sensitive=case_sensitive,
  339. env_prefix=env_prefix,
  340. env_nested_delimiter=env_nested_delimiter,
  341. env_nested_max_split=env_nested_max_split,
  342. env_ignore_empty=env_ignore_empty,
  343. env_parse_none_str=env_parse_none_str,
  344. env_parse_enums=env_parse_enums,
  345. )
  346. dotenv_settings = DotEnvSettingsSource(
  347. self.__class__,
  348. env_file=env_file,
  349. env_file_encoding=env_file_encoding,
  350. case_sensitive=case_sensitive,
  351. env_prefix=env_prefix,
  352. env_nested_delimiter=env_nested_delimiter,
  353. env_nested_max_split=env_nested_max_split,
  354. env_ignore_empty=env_ignore_empty,
  355. env_parse_none_str=env_parse_none_str,
  356. env_parse_enums=env_parse_enums,
  357. )
  358. file_secret_settings = SecretsSettingsSource(
  359. self.__class__, secrets_dir=secrets_dir, case_sensitive=case_sensitive, env_prefix=env_prefix
  360. )
  361. # Provide a hook to set built-in sources priority and add / remove sources
  362. sources = self.settings_customise_sources(
  363. self.__class__,
  364. init_settings=init_settings,
  365. env_settings=env_settings,
  366. dotenv_settings=dotenv_settings,
  367. file_secret_settings=file_secret_settings,
  368. ) + (default_settings,)
  369. custom_cli_sources = [source for source in sources if isinstance(source, CliSettingsSource)]
  370. if not any(custom_cli_sources):
  371. if isinstance(cli_settings_source, CliSettingsSource):
  372. sources = (cli_settings_source,) + sources
  373. elif cli_parse_args is not None:
  374. cli_settings = CliSettingsSource[Any](
  375. self.__class__,
  376. cli_prog_name=cli_prog_name,
  377. cli_parse_args=cli_parse_args,
  378. cli_parse_none_str=cli_parse_none_str,
  379. cli_hide_none_type=cli_hide_none_type,
  380. cli_avoid_json=cli_avoid_json,
  381. cli_enforce_required=cli_enforce_required,
  382. cli_use_class_docs_for_groups=cli_use_class_docs_for_groups,
  383. cli_exit_on_error=cli_exit_on_error,
  384. cli_prefix=cli_prefix,
  385. cli_flag_prefix_char=cli_flag_prefix_char,
  386. cli_implicit_flags=cli_implicit_flags,
  387. cli_ignore_unknown_args=cli_ignore_unknown_args,
  388. cli_kebab_case=cli_kebab_case,
  389. cli_shortcuts=cli_shortcuts,
  390. case_sensitive=case_sensitive,
  391. )
  392. sources = (cli_settings,) + sources
  393. # We ensure that if command line arguments haven't been parsed yet, we do so.
  394. elif cli_parse_args not in (None, False) and not custom_cli_sources[0].env_vars:
  395. custom_cli_sources[0](args=cli_parse_args) # type: ignore
  396. self._settings_warn_unused_config_keys(sources, self.model_config)
  397. if sources:
  398. state: dict[str, Any] = {}
  399. states: dict[str, dict[str, Any]] = {}
  400. for source in sources:
  401. if isinstance(source, PydanticBaseSettingsSource):
  402. source._set_current_state(state)
  403. source._set_settings_sources_data(states)
  404. source_name = source.__name__ if hasattr(source, '__name__') else type(source).__name__
  405. source_state = source()
  406. states[source_name] = source_state
  407. state = deep_update(source_state, state)
  408. return state
  409. else:
  410. # no one should mean to do this, but I think returning an empty dict is marginally preferable
  411. # to an informative error and much better than a confusing error
  412. return {}
  413. @staticmethod
  414. def _settings_warn_unused_config_keys(sources: tuple[object, ...], model_config: SettingsConfigDict) -> None:
  415. """
  416. Warns if any values in model_config were set but the corresponding settings source has not been initialised.
  417. The list alternative sources and their config keys can be found here:
  418. https://docs.pydantic.dev/latest/concepts/pydantic_settings/#other-settings-source
  419. Args:
  420. sources: The tuple of configured sources
  421. model_config: The model config to check for unused config keys
  422. """
  423. def warn_if_not_used(source_type: type[PydanticBaseSettingsSource], keys: tuple[str, ...]) -> None:
  424. if not any(isinstance(source, source_type) for source in sources):
  425. for key in keys:
  426. if model_config.get(key) is not None:
  427. warnings.warn(
  428. f'Config key `{key}` is set in model_config but will be ignored because no '
  429. f'{source_type.__name__} source is configured. To use this config key, add a '
  430. f'{source_type.__name__} source to the settings sources via the '
  431. 'settings_customise_sources hook.',
  432. UserWarning,
  433. stacklevel=3,
  434. )
  435. warn_if_not_used(JsonConfigSettingsSource, ('json_file', 'json_file_encoding'))
  436. warn_if_not_used(PyprojectTomlConfigSettingsSource, ('pyproject_toml_depth', 'pyproject_toml_table_header'))
  437. warn_if_not_used(TomlConfigSettingsSource, ('toml_file',))
  438. warn_if_not_used(YamlConfigSettingsSource, ('yaml_file', 'yaml_file_encoding', 'yaml_config_section'))
  439. model_config: ClassVar[SettingsConfigDict] = SettingsConfigDict(
  440. extra='forbid',
  441. arbitrary_types_allowed=True,
  442. validate_default=True,
  443. case_sensitive=False,
  444. env_prefix='',
  445. nested_model_default_partial_update=False,
  446. env_file=None,
  447. env_file_encoding=None,
  448. env_ignore_empty=False,
  449. env_nested_delimiter=None,
  450. env_nested_max_split=None,
  451. env_parse_none_str=None,
  452. env_parse_enums=None,
  453. cli_prog_name=None,
  454. cli_parse_args=None,
  455. cli_parse_none_str=None,
  456. cli_hide_none_type=False,
  457. cli_avoid_json=False,
  458. cli_enforce_required=False,
  459. cli_use_class_docs_for_groups=False,
  460. cli_exit_on_error=True,
  461. cli_prefix='',
  462. cli_flag_prefix_char='-',
  463. cli_implicit_flags=False,
  464. cli_ignore_unknown_args=False,
  465. cli_kebab_case=False,
  466. cli_shortcuts=None,
  467. json_file=None,
  468. json_file_encoding=None,
  469. yaml_file=None,
  470. yaml_file_encoding=None,
  471. yaml_config_section=None,
  472. toml_file=None,
  473. secrets_dir=None,
  474. protected_namespaces=('model_validate', 'model_dump', 'settings_customise_sources'),
  475. enable_decoding=True,
  476. )
  477. class CliApp:
  478. """
  479. A utility class for running Pydantic `BaseSettings`, `BaseModel`, or `pydantic.dataclasses.dataclass` as
  480. CLI applications.
  481. """
  482. @staticmethod
  483. def _get_base_settings_cls(model_cls: type[Any]) -> type[BaseSettings]:
  484. if issubclass(model_cls, BaseSettings):
  485. return model_cls
  486. class CliAppBaseSettings(BaseSettings, model_cls): # type: ignore
  487. __doc__ = model_cls.__doc__
  488. model_config = SettingsConfigDict(
  489. nested_model_default_partial_update=True,
  490. case_sensitive=True,
  491. cli_hide_none_type=True,
  492. cli_avoid_json=True,
  493. cli_enforce_required=True,
  494. cli_implicit_flags=True,
  495. cli_kebab_case=True,
  496. )
  497. return CliAppBaseSettings
  498. @staticmethod
  499. def _run_cli_cmd(model: Any, cli_cmd_method_name: str, is_required: bool) -> Any:
  500. command = getattr(type(model), cli_cmd_method_name, None)
  501. if command is None:
  502. if is_required:
  503. raise SettingsError(f'Error: {type(model).__name__} class is missing {cli_cmd_method_name} entrypoint')
  504. return model
  505. # If the method is asynchronous, we handle its execution based on the current event loop status.
  506. if inspect.iscoroutinefunction(command):
  507. # For asynchronous methods, we have two execution scenarios:
  508. # 1. If no event loop is running in the current thread, run the coroutine directly with asyncio.run().
  509. # 2. If an event loop is already running in the current thread, run the coroutine in a separate thread to avoid conflicts.
  510. try:
  511. # Check if an event loop is currently running in this thread.
  512. loop = asyncio.get_running_loop()
  513. except RuntimeError:
  514. loop = None
  515. if loop and loop.is_running():
  516. # We're in a context with an active event loop (e.g., Jupyter Notebook).
  517. # Running asyncio.run() here would cause conflicts, so we use a separate thread.
  518. exception_container = []
  519. def run_coro() -> None:
  520. try:
  521. # Execute the coroutine in a new event loop in this separate thread.
  522. asyncio.run(command(model))
  523. except Exception as e:
  524. exception_container.append(e)
  525. thread = threading.Thread(target=run_coro)
  526. thread.start()
  527. thread.join()
  528. if exception_container:
  529. # Propagate exceptions from the separate thread.
  530. raise exception_container[0]
  531. else:
  532. # No event loop is running; safe to run the coroutine directly.
  533. asyncio.run(command(model))
  534. else:
  535. # For synchronous methods, call them directly.
  536. command(model)
  537. return model
  538. @staticmethod
  539. def run(
  540. model_cls: type[T],
  541. cli_args: list[str] | Namespace | SimpleNamespace | dict[str, Any] | None = None,
  542. cli_settings_source: CliSettingsSource[Any] | None = None,
  543. cli_exit_on_error: bool | None = None,
  544. cli_cmd_method_name: str = 'cli_cmd',
  545. **model_init_data: Any,
  546. ) -> T:
  547. """
  548. Runs a Pydantic `BaseSettings`, `BaseModel`, or `pydantic.dataclasses.dataclass` as a CLI application.
  549. Running a model as a CLI application requires the `cli_cmd` method to be defined in the model class.
  550. Args:
  551. model_cls: The model class to run as a CLI application.
  552. cli_args: The list of CLI arguments to parse. If `cli_settings_source` is specified, this may
  553. also be a namespace or dictionary of pre-parsed CLI arguments. Defaults to `sys.argv[1:]`.
  554. cli_settings_source: Override the default CLI settings source with a user defined instance.
  555. Defaults to `None`.
  556. cli_exit_on_error: Determines whether this function exits on error. If model is subclass of
  557. `BaseSettings`, defaults to BaseSettings `cli_exit_on_error` value. Otherwise, defaults to
  558. `True`.
  559. cli_cmd_method_name: The CLI command method name to run. Defaults to "cli_cmd".
  560. model_init_data: The model init data.
  561. Returns:
  562. The ran instance of model.
  563. Raises:
  564. SettingsError: If model_cls is not subclass of `BaseModel` or `pydantic.dataclasses.dataclass`.
  565. SettingsError: If model_cls does not have a `cli_cmd` entrypoint defined.
  566. """
  567. if not (is_pydantic_dataclass(model_cls) or is_model_class(model_cls)):
  568. raise SettingsError(
  569. f'Error: {model_cls.__name__} is not subclass of BaseModel or pydantic.dataclasses.dataclass'
  570. )
  571. cli_settings = None
  572. cli_parse_args = True if cli_args is None else cli_args
  573. if cli_settings_source is not None:
  574. if isinstance(cli_parse_args, (Namespace, SimpleNamespace, dict)):
  575. cli_settings = cli_settings_source(parsed_args=cli_parse_args)
  576. else:
  577. cli_settings = cli_settings_source(args=cli_parse_args)
  578. elif isinstance(cli_parse_args, (Namespace, SimpleNamespace, dict)):
  579. raise SettingsError('Error: `cli_args` must be list[str] or None when `cli_settings_source` is not used')
  580. model_init_data['_cli_parse_args'] = cli_parse_args
  581. model_init_data['_cli_exit_on_error'] = cli_exit_on_error
  582. model_init_data['_cli_settings_source'] = cli_settings
  583. if not issubclass(model_cls, BaseSettings):
  584. base_settings_cls = CliApp._get_base_settings_cls(model_cls)
  585. model = base_settings_cls(**model_init_data)
  586. model_init_data = {}
  587. for field_name, field_info in base_settings_cls.model_fields.items():
  588. model_init_data[_field_name_for_signature(field_name, field_info)] = getattr(model, field_name)
  589. return CliApp._run_cli_cmd(model_cls(**model_init_data), cli_cmd_method_name, is_required=False)
  590. @staticmethod
  591. def run_subcommand(
  592. model: PydanticModel, cli_exit_on_error: bool | None = None, cli_cmd_method_name: str = 'cli_cmd'
  593. ) -> PydanticModel:
  594. """
  595. Runs the model subcommand. Running a model subcommand requires the `cli_cmd` method to be defined in
  596. the nested model subcommand class.
  597. Args:
  598. model: The model to run the subcommand from.
  599. cli_exit_on_error: Determines whether this function exits with error if no subcommand is found.
  600. Defaults to model_config `cli_exit_on_error` value if set. Otherwise, defaults to `True`.
  601. cli_cmd_method_name: The CLI command method name to run. Defaults to "cli_cmd".
  602. Returns:
  603. The ran subcommand model.
  604. Raises:
  605. SystemExit: When no subcommand is found and cli_exit_on_error=`True` (the default).
  606. SettingsError: When no subcommand is found and cli_exit_on_error=`False`.
  607. """
  608. subcommand = get_subcommand(model, is_required=True, cli_exit_on_error=cli_exit_on_error)
  609. return CliApp._run_cli_cmd(subcommand, cli_cmd_method_name, is_required=True)
  610. @staticmethod
  611. def serialize(model: PydanticModel) -> list[str]:
  612. """
  613. Serializes the CLI arguments for a Pydantic data model.
  614. Args:
  615. model: The data model to serialize.
  616. Returns:
  617. The serialized CLI arguments for the data model.
  618. """
  619. base_settings_cls = CliApp._get_base_settings_cls(type(model))
  620. return CliSettingsSource[Any](base_settings_cls)._serialized_args(model)