pynamodb-attributes
pynamodb-attributes copied to clipboard
Proposal: EnumMapAttribute
Using a MapAttribute as a raw container that has Enum values does not work.
eg
class MyModel(Model):
# this does not work as expected - errors on serialization
my_enum_mapping = MapAttribute[str, MyEnum](null=False, default=dict)
My current workaround is to subclass and then override the methods:
class MyModel(MapAttribute[str, MyEnum]):
@classmethod
def is_raw(cls) -> bool:
return True
def serialize(
self, values: "MyModel", *, null_check: bool = True
) -> dict[str, MyEnum]:
container = {}
for attr_name in values:
v = values[attr_name]
attr_class = UnicodeEnumAttribute(MyEnum)
attr_type = attr_class.attr_type
attr_value = attr_class.serialize(v)
container[attr_name] = {attr_type: attr_value}
return container
def deserialize(
self, values: "MyModel"
) -> dict[str, MyEnum]:
return {
k: UnicodeEnumAttribute(MyEnum).deserialize(attr_value)
for k, v in values.items()
for attr_type, attr_value in v.items()
}
Would not be much work to turn this code into something generic and reusable - happy to do the work and contribute upstream if it is desired