Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using field transformer breaks integration with hypothesis #821

Closed
jakub-borusewicz opened this issue Jun 9, 2021 · 2 comments · Fixed by #824
Closed

Using field transformer breaks integration with hypothesis #821

jakub-borusewicz opened this issue Jun 9, 2021 · 2 comments · Fixed by #824
Labels
Bug Typing Typing/stub/Mypy/PyRight related bugs.

Comments

@jakub-borusewicz
Copy link

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!

@hynek
Copy link
Member

hynek commented Jun 10, 2021

@sscherfke? 😇

@sscherfke
Copy link
Contributor

I will take a look, but I can’t tell when. I’m currently quite busy with work. :)

sscherfke added a commit that referenced this issue Jun 13, 2021
@wsanchez wsanchez added Bug Typing Typing/stub/Mypy/PyRight related bugs. labels Aug 23, 2021
hynek added a commit that referenced this issue Sep 22, 2021
* Convert transformed attrs to AttrsClass

Fixes: #821

* Add cangelog entry

* Only call AttrsClass once

* Calm mypy by inline the AttrsClass call

* Defer AttrsClass creation as long as possible

Co-authored-by: Hynek Schlawack <[email protected]>
swhmirror pushed a commit to SoftwareHeritage/swh-model that referenced this issue Oct 17, 2022
When using attr < 21.3.0, adding field transformer breaks attrs
integration with hypothesis, because attributes transformed with
such function are not casted to generated AttrsClass, but remains
just an list of attributes. This causes error in hypothesis by
raising an AttributeError.

As we use attr 21.2.0 in production and when building debian buster
package, add a workaround for that issue as explained here:
python-attrs/attrs#821.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Typing Typing/stub/Mypy/PyRight related bugs.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants