You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Resolve field aliases before calling field_transformer (#1509)
* Resolve field aliases before calling field_transformer
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#1479
* Fix CI failures: update doctest, add changelog, fix coverage
- 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 #1479
- Add test_hook_new_field_without_alias to cover the post-transformer
alias resolution path (line 496 of _make.py)
* Fix pre-commit
* Change is neat but breaking
Point change at PR, not bug.
* Handle default alias detection explicitly
* Fix docs build
* We actually load 25.3-style
---------
Co-authored-by: Hynek Schlawack <hs@ox.cx>
Field aliases are now resolved *before* calling `field_transformer`, so transformers receive fully populated `Attribute` objects with usable `alias` values instead of `None`.
2
+
The new `Attribute.alias_is_default` flag indicates whether the alias was auto-generated (`True`) or explicitly set by the user (`False`).
Or, perhaps you would prefer to generate dataclass-compatible `__init__` signatures via a default field *alias*.
251
-
Note, *field_transformer* operates on {class}`attrs.Attribute` instances before the default private-attribute handling is applied so explicit user-provided aliases can be detected.
251
+
Note, *field_transformer* receives {class}`attrs.Attribute` instances with default aliases already resolved, so the leading-underscore stripping has already been applied.
252
+
You can use the `attrs.Attribute.alias_is_default` flag to detect whether an alias was explicitly provided by the user or auto-generated.
0 commit comments