Comparison: attrs vs marshmallow

attrs

  • Oriented towards easing defining classes with attributes having validators, defaults, input conversion, etc.
  • Doesn't do much towards relational mapping.
  • Unclear about how it works with nested/complex data types.
  • Handles serialization to `dict` and `tuple`.
  • No special facility for deserializing, aside from the built-in keyword expansion of constructor parameters.
  • Uses class decorator to make it work, which happens at import-time.
  • Builds code from strings, which is eval'd. While using eval is a little icky, it does make it faster than using meta-stuff and probably faster than using descriptors. The generated code can also be viewed with a debugger, which is probably much easier to deal with than metaclasses in a debugger.
  • Generates methods for comparison & hashing, potentially limiting to a subset of attributes.

marshmallow

  • Oriented towards defining schema for incoming structured primitives, such as a dict from JSON.
  • Defines schema in class separate from actual model class; deserializing to an object has to be done with extra.
  • Can be used with an ORM; examples include using with Pewee ORM: http://marshmallow.readthedocs.io/en/latest/examples.html#todo-api-flask-peewee. In this case, marshmallow handles validation & de-/serialization and ORM models just define basic types, defaults, foreign keys, DB constraints & indexes.
  • Only applies to attributes, not classes containing those attributes, so there are no comparison & hashing methods.