lyft / lyft/pynamodb-attributes

Proposal: EnumMapAttribute

Open
#55 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
62
Forks
12
PR merge metrics
No merged PRs in 30d

Description

Using a MapAttribute as a raw container that has Enum values does not work.

eg
```python
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:

```python
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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading MapAttribute and UnicodeEnumAttribute, focusing on the is_raw, serialize, and deserialize entry points shown in the issue. Define what a reusable EnumMapAttribute should support, then verify that maps with enum values serialize and deserialize consistently; no repository files or tests are named in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
databases
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.