Resolve field aliases before calling field_transformer#1509
Open
veeceey wants to merge 4 commits intopython-attrs:mainfrom
Open
Resolve field aliases before calling field_transformer#1509veeceey wants to merge 4 commits intopython-attrs:mainfrom
veeceey wants to merge 4 commits intopython-attrs:mainfrom
Conversation
Previously, field_transformer received attributes with alias=None for fields without an explicit alias. The default alias (e.g., stripping leading underscores) was only resolved after the transformer ran, making it impossible for transformers to access or use alias values. This moves alias resolution to before the field_transformer call, so transformers receive fully populated Attribute objects. A second pass after the transformer handles any new fields the transformer may have added. Additionally, Attribute.evolve() now automatically updates the alias when the name changes, if the alias was auto-generated (matching the default for the old name). Explicit aliases are preserved. Fixes python-attrs#1479 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Update extending.md doctest to reflect that field_transformer now
receives pre-resolved aliases (use alias == name.lstrip("_") instead
of `not field.alias` to detect auto-generated aliases)
- Add changelog entry for python-attrs#1479
- Add test_hook_new_field_without_alias to cover the post-transformer
alias resolution path (line 496 of _make.py)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
for more information, see https://pre-commit.ci
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.
Summary
Fixes #1479
_privatetoprivate) to before thefield_transformercallback, so transformers receive fully populatedAttributeobjects with usablealiasvalues instead ofNone.Attribute.evolve()to automatically update the alias when thenamechanges, if the alias was auto-generated (matches the default for the old name). Explicit aliases are preserved.Before (broken)
After (fixed)
Test plan
test_hook_alias_available- verifies transformer sees resolved aliasestest_hook_evolve_name_updates_auto_alias- verifiesevolve(name=...)updates auto-generated aliastest_hook_evolve_name_keeps_explicit_alias- verifiesevolve(name=...)preserves explicit aliasNone🤖 Generated with Claude Code