fix: Only override preferred_key when no value was found#767
Merged
hramezani merged 3 commits intopydantic:mainfrom Jan 29, 2026
Merged
fix: Only override preferred_key when no value was found#767hramezani merged 3 commits intopydantic:mainfrom
hramezani merged 3 commits intopydantic:mainfrom
Conversation
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
767788f to
05f6de1
Compare
hramezani
approved these changes
Jan 29, 2026
Member
|
Thanks @chbndrhnns |
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>
This was referenced Mar 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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