CodeVetter · Repo Unpack · Behavior traces
pydantic/pydantic-settings
The inspected flows cover ordinary construction, CLI command execution, file-backed secrets, cloud-backed secrets, caching, and serialization. Their execution order explains when I/O happens and which state a source can observe. File and cloud paths load configuration into models; the shown flows do not persist model changes back to their input stores. Cache reuse retains validated instances within the process.
- Construction flow: BaseSettings.__init__ → _settings_init_sources → built-in source constructors → settings_customise_sources → _settings_build_values → BaseModel.__init__; sources normally run init, environment, dotenv, flat secrets, defaults.
- Nested environment flow: EnvSettingsSource._load_env_vars → parse_env_vars → inherited __call__ → get_field_value → prepare_field_value → decode_complex_value/explode_env_vars; nested entries override overlapping decoded JSON keys before model validation.
- Dotenv flow: DotEnvSettingsSource initialization invokes its _load_env_vars override → _read_env_files → _resolve_config_file → dotenv_values → parse_env_vars; later files replace earlier keys, then __call__ applies field extraction and extra-variable filtering.
- Flat-secret flow: SecretsSettingsSource.__call__ expands configured directories, warns for missing paths, rejects existing non-directories, then inherited field traversal calls get_field_value → find_case_path → UTF-8 read_text().strip(); later directories are searched first.
- CLI flow: CliApp.run checks the model type → _settings_init_sources creates/connects CliSettingsSource → arguments load into env_vars → sources merge and validate → _run_cli_cmd invokes the root handler if present; a handler calling run_subcommand selects the annotated child and requires its handler.
- GCP flow: source construction initially returns an empty environment mapping; the merge loop supplies current_state → source.__call__ → _load_env_vars → _resolve_gcp_project → GoogleSecretManagerMapping → field lookup → access_secret_version, using explicit project, prior-source project, then auth defaults.
- Cache flow: settings_cached checks the guarded class cache, obtains a per-class lock, double-checks the entry, constructs cls() outside the global guard, and stores it; settings_clear_cache removes only the exact class entry and cannot revoke held or in-flight references.
- Serialization flow: CliApp.serialize builds a CliSettingsSource → _serialized_args walks model fields and parser metadata → _serialize_value converts enums/containers → _flatten_serialized_args orders optional/positional segments, subcommands, and finally verbatim unknown arguments.