Skip to content

Resolve field aliases before calling field_transformer#1509

Open
veeceey wants to merge 2 commits intopython-attrs:mainfrom
veeceey:fix/issue-1479
Open

Resolve field aliases before calling field_transformer#1509
veeceey wants to merge 2 commits intopython-attrs:mainfrom
veeceey:fix/issue-1479

Conversation

@veeceey
Copy link

@veeceey veeceey commented Feb 8, 2026

Summary

Fixes #1479

  • Moves default alias resolution (e.g., stripping leading underscores from _private to private) to before the field_transformer callback, so transformers receive fully populated Attribute objects with usable alias values instead of None.
  • Adds a second alias resolution pass after the transformer to handle any new fields the transformer may have added without setting an alias.
  • Updates Attribute.evolve() to automatically update the alias when the name changes, if the alias was auto-generated (matches the default for the old name). Explicit aliases are preserved.

Before (broken)

def transformer(cls, fields):
    for f in fields:
        print(f.alias)  # None for auto-generated aliases
    return fields

After (fixed)

def transformer(cls, fields):
    for f in fields:
        print(f.alias)  # 'private' for _private, 'my_alias' for explicit
    return fields

Test plan

  • Added test_hook_alias_available - verifies transformer sees resolved aliases
  • Added test_hook_evolve_name_updates_auto_alias - verifies evolve(name=...) updates auto-generated alias
  • Added test_hook_evolve_name_keeps_explicit_alias - verifies evolve(name=...) preserves explicit alias
  • Updated 2 existing test expectations where error messages now show the resolved alias instead of None
  • Full test suite passes: 1367 passed, 6 skipped, 2 xfailed

@veeceey
Copy link
Author

veeceey commented Feb 19, 2026

Friendly ping - any chance someone could take a look at this when they get a chance? Happy to make any changes if needed.

@AdrianSosic
Copy link

Hi @veeceey 👋🏼 Thank you very much for taking care of this! As the one who opened the issue, I can confirm that it indeed solves the problem for me 👌🏼 Regarding the integration into attrs, I'd hope that @hynek could have a final look and give his approval?

@veeceey
Copy link
Author

veeceey commented Feb 20, 2026

Thanks for confirming @AdrianSosic, really glad it solves the problem on your end! Hopefully @hynek can find a moment to take a look when he gets a chance.

@hynek
Copy link
Member

hynek commented Feb 21, 2026

Sorry for the delays, but I got caught up in a PR slop wave last week and while your PR looks legit on a brief look, given the current legal status I cannot merge it as long as there's a credit for Claude in the history.

This is not meant belligerently in any way; I appreciate that your PR conforms to our current contribution rules and one of the reasons I didn't reply earlier is because I was drafting #1518. I'm happy to take your feedback over there too, FWIW, but until we have more legal clarity, we can't permit the copyright status of attrs to become unclear by having a paper trail of legally questionable contributions.

I'm genuinely sorry for the inconvenience, and I would've preferred to spend my time differently too, but these are crazy times.


There's also the technical issue of whether this change is too breaking, but I don't want to waste your time/split the discussion until we have mergeable PR.

@veeceey
Copy link
Author

veeceey commented Feb 23, 2026

Hey @hynek, good catch - I've cleaned up the git history and removed the co-author attribution. All commits are now solely under my name. Let me know if there's anything else needed!

@hynek
Copy link
Member

hynek commented Feb 23, 2026

amazing, thank you for that @veeceey!

Before I zoom in on the implementation, and given we had some breakage related to field transformers in the past: hey @ashb is this likely to break Airflow? It looks additive/harmless enough but I'm not sure what kind of assumptions y'all are making.

Also cc @sscherfke.

@ashb
Copy link
Contributor

ashb commented Feb 23, 2026

@hynek first thought is that it should be fine as i don't think we use field transformers much if at all.

I'll run this through our tests today

@ashb
Copy link
Contributor

ashb commented Feb 23, 2026

All good on the Airflow side, he have exactly one field_transformer which doesn't make any assumptions about private fields or not.

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
- 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)
@veeceey
Copy link
Author

veeceey commented Feb 24, 2026

Thanks for running the tests @ashb, glad to hear it's all clear on the Airflow side!

@hynek just a heads up - I noticed the commit messages still had co-author lines from earlier, and the PR description had a leftover attribution line too. I've now cleaned both up completely - the git history and PR description are fully clean. Ready for your review whenever you get a chance!

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.

Alias not available during field transformation

4 participants