Add support for Enum#33
Conversation
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #33 +/- ##
==========================================
+ Coverage 92.43% 92.69% +0.25%
==========================================
Files 34 35 +1
Lines 1468 1547 +79
Branches 219 231 +12
==========================================
+ Hits 1357 1434 +77
- Misses 49 50 +1
- Partials 62 63 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Thanks for this contribution. Let me think more about the right thing to do with regard to this:
My motivation for the original design is that this is the canonical use of enums: from enum import Enum, auto
class Foo(Enum):
a = auto()
b = auto()But serialize this and, I don't know if you get what you expect, but you certainly don't get something useful. Your design also works on things like: class Foo(Enum):
a = {}
b = {"a": 1}But kind of accidentally. It does not work on things like: class Status(Enum):
success = Success("Job completed successfully")
failure = Failure("Job failed")
crashed = Failure("Server did not start")It would not work on the planet example. I always thought of the "value" of an enum as the Python payload. I find it strange that other tools treat the value as the "serialization representation". I guess this makes sense if you have a lot of enums whose serialized representations are not appropriate Python enum names (e.g. I had also envisioned this only working with |
20cc84b to
3abbb42
Compare
|
I've been thinking more about this and came around to serializing by name by default rather than by value. It has better interoperability with other languages and serialization frameworks. Value-based serialization is still supported as an opt-in. Regarding dispatching on all enums vs. requiring a decorator, I prefer dispatch. For the cases where you want different behavior (like by-value), |
|
In typical use of Serialite, classes define themselves how they are serialized by default. Obviously, this cannot be done for builtin Python classes, so Serialite provides the dispatcher to define default serialization. But also, you can override the default serializer for anything by providing it to By putting
(I guess another reason would be to allow parameterization like If we went the mixin route, I would probably create |
Fixes #5
I checked two other serialization libraries, msgspec and Pydantic, and they both serialize Enums to their values. I also think this is more appropriate for
IntEnum, so decided to diverge from the original issue description. Let me know what you think.