Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.

Allow dumping of non-enum value that is equal to enum's value or name #47

Open
Boris-Barboris opened this issue Feb 25, 2021 · 2 comments

Comments

@Boris-Barboris
Copy link

We try to use the same serializer for loading and dumping, and some of our internal objects are simple dicts with string fields, and we are forced to manually convert strings to enums or load our internal entities by schema in order to dump them. We would like to be able to

class ImageOsTypeEnum(str, Enum):
    linux = 'linux'
    windows = 'windows'

class OpenstackKeypairListSerializer(Schema):
    os_type = EnumField(ImageOsTypeEnum, required=False)

OpenstackKeypairListSerializer().dump({'os_type': 'linux'})

without converting 'linux' to enum beforehand.
We could use OneOf and Str, but Enums are much more readable and work great with type hints.

@drewm1980
Copy link

Wouldn't that API change violate type safety? i.e. silently letting 'linux' be used in place ImageOsTypeEnum.linux even though it violates the spec since it has the wrong type? If you want to have static contraints on what strings a value can take (at the level of static) there is also the new Literal type...

@Boris-Barboris
Copy link
Author

Personally, I don't use enums for type safety, I use them for valid value set restriction, so I don't see a problem here. Python's vision of the fact that 'linux' is not the same as ImageOsTypeEnum.linux is a language implementation detail for me.
You can always hide this feature under some kind of optional field flag.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants