Skip to content

Using field transformer breaks integration with hypothesis #821

@jakub-borusewicz

Description

@jakub-borusewicz

Adding field transformer according to documentation (i.e. as a function of type Callable[[type, list[attr.Attribute[Any]]], list[attr.Attribute[Any]]])
breaks attrs integration with hypothesis, because attributes transformed with such function are not casted to generated AttrsClass, but remains just an list of attributes (here)

This causes error in hypothesis here, because fields is an list, not an AttrsClass instance, and therefore getattr(fields, name) raises an AttributeError.

Easy workaround for that is just to create an AttrsClass again inside field transformer, like so:

def transformer(cls: type, fields: list[attr.Attribute[Any]]) -> list[attr.Attribute[Any]]:
    attr_names = [f.name for f in fields]
    AttrsClass = _make_attr_tuple_class(cls.__name__, attr_names)
    
    ... do something with fields ...

    return AttrsClass(fields)

...but it's not very handy nor pretty, and messes with typing. So, I would very thankful if it could be resolved within attrs (it would be quite easy, I guess).

BTW. I love your package, awesome job! Cheers!

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugTypingTyping/stub/Mypy/PyRight related bugs.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions