-
-
Notifications
You must be signed in to change notification settings - Fork 385
Interoperability with dataclasses #565
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
Comments
Well this is obviously a multi-tiered problem. I'm personally happy about having some kind of interop with DCs, since they are quite similar and everyone would win. I think |
Would everything "just work" if we had a fallback thing for |
@glyph you mean like a special case for |
The
dataclasses
ecosystem is growing rapidly. Currently, it's not easy for attrs classes to benefit from the developments in thedataclasses
ecosystem. It would be nice to fix this somehow.There are at least two use cases. One is where I write a class with
attrs
that holds aclass defined with
dataclasses
. I want to be able to use all theattrs
functionality.For example, the normally recursive function
attr.asdict()
doesn'trecurse into dataclasses.
The other situation is where I want to use libraries developed for use with
dataclasses
on my
attrs
classes. There are libraries which provide very useful functionality fordataclasses
, but not forattrs
classes. For example, marshmallow_dataclass has some good ideas about serialization, and I might like to use it on myattrs
classes.Users of libraries that use
attrs
anddataclasses
would benefit from easy interoperabilitybetween the two systems. I've been wondering how to make this work.
Supporting both systems explicitly in every library
One solution would be to edit each of these functions to support both systems, by adding
if
statementsThis might need to be done in each library that intends to support both systems.
Supporting both systems implicitly in every library through an interface
It might be possible to support both systems through a common interface. For example, if
attrs classes got
__dataclasses_params__
and__dataclasses_fields__
in addition to__attrs_attrs__
, using thedataclasses
API as the common interface. This mightrestrict usage in some ways because
dataclasses
has less functionality thanattrs
, but it could work relatively seamlessly. Alternatively, a shared compatibility interface could be defined in a third library, which could supportdataclasses
,attrs
, and any other libraries that come to provide similar declarative-class-definition functionality.Converting between attrs and dataclasses
Perhaps it would be useful to have a function for converting between
dataclasses
andattrs
so classes defined under one library can use the ecosystem written to support theother. There would be nuances involved with making classes round-trippable since each
library supports different functionality.
I'm interested in everybody's thoughts on how to handle this situation. :-)
The text was updated successfully, but these errors were encountered: