I would like to structure and unstructure an attrs object, which includes dict fields that use simple frozen attrs for the dict keys. This works very well for objects created at runtime, but the frozen attribute fails to make un/structuring with cattrs easy.
This is a simple example of the problem:
import attr, cattr
# Simple attr that contains only a single primitive data type.
@attr.s(frozen=True)
class AbstractID:
_id: Optional[int] = attr.ib()
def __str__(self) -> str:
if self._id is not None:
return f"A{self._id}"
else:
return "—"
@attr.s(auto_attribs=True)
class Database:
storage: dict[AbstractID, str] = {}
# Attempt to unstructure using cattrs
db = Database()
db.storage[AbstractID(1)] = "some data"
cattr.unstructure(db)
>>> TypeError: unhashable type: 'dict'
Is there some way to serialize the data, without using int or str as the dict keys, outside the import/export process? I saw that cattrs offers hooks to customize the serialization process, but I can't figure out how to reduce the AbstractID to an int when unstructuring, or how to structure it back into an AbstractID.
Can this be done?