Skip to content

Enums: Support for default member / __missing__ #531

Closed

Description

@iodbh

Description

feature

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:

  1. attempt to deserialize the message using the model
  2. catch validation exceptions
  3. check whether the exception is caused by an unknown member in any of the (many) fields where we expect values to be added
  4. 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.

example

Ideally, the following would be supported:

class Flag(Enum):
    ACTIVE = "active"
    ADMIN = "admin"
    UNKNOWN_FLAG = "unknown_flag"

    @classmethod
    def __missing__(cls, key):
        return cls.UNKNOWN_FLAG

class User(msgspec.Struct):
    username: str
    flags: List[Flag]

any alternative API allowing this sort of behavior would be a huge improvement.

Activity

  1. jcrist commented on Aug 24, 2023

    @jcrist
    Member

    Thanks for opening this, this is definitely a feature worth supporting (it's even been brought up before). Should be fixed by #532.

  2. iodbh commented on Aug 31, 2023

    @iodbh
    Author

    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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions