attrs icon indicating copy to clipboard operation
attrs copied to clipboard

Alias not available during field transformation

Open AdrianSosic opened this issue 4 months ago • 1 comments

Hi, I'm not sure if this is a bug or a "feature" that is documented somewhere. At least, I haven't encountered it before, so I thought I'd better double-check.

Is it expected that the alias property is None during field transformation? Has this something to do with the fact that the class is not yet fully built at this stage? But since all information is pretty much already available at this point, I'm wondering if this is unintended ... 🤔

from attrs import define, field, fields


def f(cls, x):
    print("During field transformation:\n-------------------------------")
    for y in x:
        print(y.alias)
    return x


@define(field_transformer=f)
class C:
    _auto_stripped: int = field(default=0)
    _auto_stripped_with_alias: int = field(default=0, alias="_auto_stripped_with_alias")
    regular: int = field(default=0)
    regular_with_alias: int = field(default=0, alias="regular_with_alias")


print("\nAfter class creation:\n-------------------------------")
for fld in fields(C):
    print(fld.alias)

Gives:

During field transformation:
-------------------------------
None
_auto_stripped_with_alias
None
regular_with_alias

After class creation:
-------------------------------
auto_stripped
_auto_stripped_with_alias
regular
regular_with_alias

AdrianSosic avatar Oct 13 '25 19:10 AdrianSosic

@sscherfke pls

hynek avatar Oct 15 '25 07:10 hynek