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

asdict(): Optionally retain collection types #69

vortec opened this issue Aug 22, 2016 · 1 comment

asdict(): Optionally retain collection types #69

vortec opened this issue Aug 22, 2016 · 1 comment


Copy link

@vortec vortec commented Aug 22, 2016

When calling attr.asdict(), it converts attributes of type tuple, set to type list and subclasses of dict to dict. This is being done for JSON compatibility, however, there are use cases where it's required to preserve the original type.

Therefore I request an optional argument called retain_collection_types, which is set to False by default (to not break the existing API). If enabled, the function should not convert any collection type.

@hynek hynek added the Feature label Aug 22, 2016
Copy link

@Tinche Tinche commented Aug 22, 2016

How about we just use the __class__ of the collection by default?

            elif isinstance(v, (tuple, list, set)):
                rv[] = v.__class__(
                    asdict(i, recurse=True, filter=filter,
                    if has(i.__class__) else i
                    for i in v
>>> @attr.s
... class T:
...     x = attr.ib()
>>> attr.asdict(T((1,2,3)))
{'x': (1, 2, 3)}
>>> attr.asdict(T(set([1,2,3])))
{'x': {1, 2, 3}}
>>> attr.asdict(T([1,2,3]))
{'x': [1, 2, 3]}


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants