Skip to content

Alias not available during field transformation #1479

@AdrianSosic

Description

@AdrianSosic

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions