Skip to content

Add type back to attrs.field() for use in attrs.make_class() #1082

@rouge8

Description

@rouge8

I'm using attrs.make_class() to dynamically construct a class based on other attrs classes, and it would be nice to be able to specify the type of the fields on the generated class using attrs.field() instead of attr.ib(). In other words, this works:

fields = {
    field.name: attr.ib(type=field.type)
    for field in itertools.chain.from_iterable(
        attrs.fields(t) for t in SOME_ATTRS_CLASSES
    )
    if not field.name.startswith("_")
}

MyClass = attrs.make_class("MyClass", fields)

but this does not:

fields = {
    field.name: attrs.field(type=field.type)
    for field in itertools.chain.from_iterable(
        attrs.fields(t) for t in SOME_ATTRS_CLASSES
    )
    if not field.name.startswith("_")
}

MyClass = attrs.make_class("MyClass", fields)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions