Skip to content

fix: Only override preferred_key when no value was found#767

Merged
hramezani merged 3 commits intopydantic:mainfrom
chbndrhnns:restore-init-kwargs
Jan 29, 2026
Merged

fix: Only override preferred_key when no value was found#767
hramezani merged 3 commits intopydantic:mainfrom
chbndrhnns:restore-init-kwargs

Conversation

@chbndrhnns
Copy link
Copy Markdown
Contributor

When using AliasChoices with AliasPath as the first choice, the env source incorrectly used the first element of the AliasPath as the key in the returned dict, even when a different alias string actually matched.

Fixes #766

Johannes Rueschel added 3 commits January 29, 2026 11:56
When using AliasChoices with AliasPath as the first choice, the env source incorrectly used the first
element of the AliasPath as the key in the returned dict, even when a different alias string actually matched.
The code example at lines 177-235 was setting environment variables via os.environ but never cleaning them up.
This caused test pollution that made test_cli_case_insensitive_arg fail when run after the docs tests.
If populate_by_name is enabled, return field_name to allow multiple fields
with the same alias but different versions to be distinguished
@hramezani
Copy link
Copy Markdown
Member

Thanks @chbndrhnns

@hramezani hramezani merged commit a04b034 into pydantic:main Jan 29, 2026
19 checks passed
hramezani added a commit that referenced this pull request Mar 11, 2026
The fix in #767 (a04b034) stopped normalizing env source keys to the
preferred alias when a value was found. This caused different sources to
emit different keys for the same field, so deep_update couldn't merge
them and _settings_restore_init_kwarg_names picked the wrong value.

Now we normalize to preferred_key unless it comes from an AliasPath
(complex entry), which preserves the fix for #766 while restoring
correct key normalization for string-only AliasChoices.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Weird behavior between custom settings source and EnvSettingsSource

2 participants