You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import cattr
import attr
from cattrs.strategies import configure_tagged_union, include_subclasses
@attr.s(frozen=True, auto_attribs=True)
class A:
pass
@attr.s(frozen=True, auto_attribs=True)
class B1(A):
b: str
@attr.s(frozen=True, auto_attribs=True)
class B2(A):
b: str
a: A
CONVERTER = cattr.Converter()
union_strategy = partial(configure_tagged_union, tag_name="type_name")
include_subclasses(A, CONVERTER, union_strategy=union_strategy)
if __name__ == "__main__":
instance = B1(b="1")
r = CONVERTER.unstructure(instance)
CONVERTER.structure(r, A)
instance2 = B2(b="2", a=B1(b="1"))
r2 = CONVERTER.unstructure(instance2)
CONVERTER.structure(r2, A)
In the example above, we can (un)structure a B1 instance (e.g. instance) correctly. However, for a B2 instance (e.g. instance2), I can see catter unstructure it correctly, r2 looks correctly. However, I got B2(b='2', a=A()) back when I structure the r2 as A, which does not equal to the original instance2.
The text was updated successfully, but these errors were encountered:
Description
I want to use cattrs to (un)strcuture classes.
What I Did
In the example above, we can (un)structure a
B1
instance (e.g.instance
) correctly. However, for aB2
instance (e.g.instance2
), I can see catter unstructure it correctly,r2
looks correctly. However, I gotB2(b='2', a=A())
back when I structure ther2
asA
, which does not equal to the originalinstance2
.The text was updated successfully, but these errors were encountered: