-
Notifications
You must be signed in to change notification settings - Fork 154
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
Allow enum dict keys #215
base: master
Are you sure you want to change the base?
Allow enum dict keys #215
Conversation
res = type_(value) | ||
# we got the enum value | ||
for enum_member in type_: | ||
# We rely on the user that the enum values are unique |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't believe this is necessarily true -- enum names are not necessarily one-to-one with values. There is a decorator @unique
that enforces it, but the standard library does allow for it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Appreciate the contribution again! But I think this is functionality should be supplied via config by the user. Specifically, you can specify the decoder
for the field. I purposely limited the supported types, with the intention of only supporting the most common use-cases and the user being able to extend the library otherwise.
While I see the motivation / use-case for Enum keys in dicts, I don't think the library code itself should try to do that decoding -- there's a tiny bit of code overhead with each feature that adds up to the library's maintainability. I'm struggling to read through / follow the library code much more compared to before, and I'm feeling quite strongly to not add any more features / additions, and focus on closing issues / performance / refactoring / simplifying.
I do appreciate your work on this library a lot, and let me know if you think differently.
I would like to have this feature as well. Super handy to have especially when coming from Jackson in java. |
This pull requests allows enums as dict keys.
Since marshmallow does not support anything but string enums by default (see #213 ),
the marshmallow code for enum dict keys also only works with strings.
I've implemented it so that it works for other stuff (e.g.
float, int
) when usingfrom_json
,to_json
,if that is not desired let me know.
This means you can now do:
@lidatong