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

Convert transformed attrs to AttrsClass #824

Merged
merged 9 commits into from Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/824.changes.rst
@@ -0,0 +1 @@
Attributes transformed via ``field_transformer`` are wrapped with ``AttrsClass`` again.
2 changes: 1 addition & 1 deletion src/attr/_make.py
Expand Up @@ -599,7 +599,7 @@ def _transform_attrs(
had_default = True

if field_transformer is not None:
attrs = field_transformer(cls, attrs)
attrs = AttrsClass(field_transformer(cls, attrs))
sscherfke marked this conversation as resolved.
Show resolved Hide resolved
return _Attributes((attrs, base_attrs, base_attr_map))


Expand Down
16 changes: 16 additions & 0 deletions tests/test_hooks.py
Expand Up @@ -117,6 +117,22 @@ class Sub(Base):

assert attr.asdict(Sub(2)) == {"y": 2}

def test_attrs_attrclass(self):
"""
The list of attrs returned by a field_transformer is converted to
"AttrsClass" again.

Regression test for #821.
"""

@attr.s(auto_attribs=True, field_transformer=lambda c, a: list(a))
class C:
x: int

fields_type = type(attr.fields(C))
assert fields_type.__name__ == "CAttributes"
assert issubclass(fields_type, tuple)


class TestAsDictHook:
def test_asdict(self):
Expand Down