Attrs: Marshmallow integration for serialization/deserialization

Created on 17 Aug 2017  路  12Comments  路  Source: python-attrs/attrs

I have put together a start at integrating attrs with marshmallow to achieve nested serialization and deserialization (including mixed-type lists) of attrs objects. I have been using attrs for bit but this is my first work with marshmallow.

https://repl.it/KEpi/latest

Any feedback is welcome but I am primarily curious if I am working on something useful or just poorly reinventing an existing wheel. Thanks for any thoughts, including if there's a more proper venue for discussion vs. reporting issues.

Most helpful comment

I feel like we should start collecting attrs extensions somewhere. I don鈥檛 think the docs are the right place because we don鈥檛 release often enough, so I guess the GitHub wiki it is?

All 12 comments

I don鈥檛 have the time to look at it any closer but you may want to look at #215 which will probably make your job a lot easier.

I might consider looking at https://cattrs.readthedocs.io/en/latest/, which uses Python's typing module to annotate attrs fields and serialize / deserialize them. It doesn't to mix-typed lists per se, but you can utilize a converter to help guide that transformation.

Deserialization isn't so intuitive with attrs---unless I'm missing something. What's the best way to handle fields that are collections of attr objects? For example:

from typing import List

@attr.s
class A(object):
    foo = attr.ib()

@attr.s
class B(object):
    a_list: List[A] = attr.ib()

I can create a well-formed instance of B:

>>> o = B(a_list=[A('bar'), A('biz')])
>>> print(o)
B(a_list=[A(foo='bar'), A(foo='biz')])

But if I convert it to a dict then try to "deserialize" it, the nested list contains dictionaries, not instances of A like you'd hope:

>>> B(attr.asdict(o))
B(a_list={'a_list': [{'foo': 'bar'}, {'foo': 'biz'}]})

Perhaps my expectations are too high, but I really like attrs so far and serialization/deserialization is such a common pattern in this age of APIs and microservices. It'd be great if there's a way to support it with attrs in a way that feels natural.

After more digging it looks like this is related to #12

It is a common pattern but it is not a simple pattern so we wait how external solutions like cattrs pan out. Recently we鈥檝e added first-class type support to make it more feasible.

So it totally is on our agenda but it鈥檚 completely possible that attrs proper will never support it itself, but we鈥檒l arrive at a point where it has all the affordances that third parties can build great serialisation frameworks on top of it.

Along with cattrs you could check out "related" which I built on top of attrs for a more Django ORM flavored "Object-to-Doc/Dict Mapper":

https://github.com/genomoncology/related

Thanks for attrs!

I feel like we should start collecting attrs extensions somewhere. I don鈥檛 think the docs are the right place because we don鈥檛 release often enough, so I guess the GitHub wiki it is?

Lemme know if you want me to spend a few minutes and set that up

Quick start: https://github.com/python-attrs/attrs/wiki/Extensions-to-attrs
Has cattrs and related.
Any others to add?

I鈥檝e got an email point out that https://smarie.github.io/python-valid8/#attrs support attrs.

Did the Marshmallow integration land on PyPI @altendky?

@hynek, technically yes. There's github.com/altendky/graham and pypi.python.org/pypi/graham but I call it alpha. I use it in my own program but it's been on hold for awhile. No (useful) docs and plenty of opportunity for significant interface change.

I leave it up to you whether you want to add it or not. In any case I鈥檓 closing this issue now since it鈥檚 not actionable for us.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kissgyorgy picture kissgyorgy  路  6Comments

madig picture madig  路  3Comments

hynek picture hynek  路  9Comments

Nicholas-Mitchell picture Nicholas-Mitchell  路  15Comments

ojii picture ojii  路  16Comments