You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Enums: Support for default member / __missing__ #531
python enums supports __missing__, which can be used to resolve unknown members on an enum to a preset default member rather than erroring out.
support for this method - or some alternative way of specifying a "default" member when deserializing an enum field - is msgspec would be quite valuable.
use case
In our case, we use msgspec to deserialize deeply nested JSON from an API that heavily uses a "flag" concept. these flags are lists of strings that we map to enums, for instance a user object could have its flags set to ["ACTIVE", "ADMIN"]. New flags are added regularly, and there are multiple occurences of different flag sets at different nesting levels of our models.
As it stands, deserialization will fail with a validation error if any of the flags list contains a newly added flag. We would like to be able to deserialize, and use a default "UNKNOWN" value. The only option we currently have (that i'm aware of) is:
attempt to deserialize the message using the model
catch validation exceptions
check whether the exception is caused by an unknown member in any of the (many) fields where we expect values to be added
deserialize the message again, without using the model
There is of course the possibility of treating these fields as simple strings, but that means losing information about the data model.
Activity
jcrist commentedon Aug 24, 2023
Thanks for opening this, this is definitely a feature worth supporting (it's even been brought up before). Should be fixed by #532.
iodbh commentedon Aug 31, 2023
I wasn't expecting such a quick fix ! This is is saving us a huge headache, thank you for your work on this library - it is greatly appreciated :)