A triage form posts "priority": "__members__" and the request is accepted. The Enum field, which looks members up by name, returns whatever attribute of the enum class has that name: the whole member mapping for __member…
A triage form posts "priority": "__members__" and the request is accepted. The Enum field, which looks members up by name, returns whatever attribute of the enum class has that name: the whole member mapping for __members__, a method for mro, the class name for __name__. Code after validation then crashes on a value that is not a member.
import enum
from marshmallow import fields
class Color(enum.Enum):
RED = 1
GREEN = 2
BLUE = 3
field = fields.Enum(Color)
field.deserialize("__members__")
field.deserialize("mro")
Both raise ValidationError: Must be one of: RED, GREEN, BLUE.
The first returns the mapping of all members, the second returns the bound method Color.mro.
LEGACY = "a" declared after ACTIVE = "a", the name LEGACY loads as Status.ACTIVE.by_value=True) is not affected.Adapted from marshmallow-code/marshmallow (MIT).
Visible test command
pytest testsThe repository’s own tests. Run all tests includes them, and so does every submission.
Repository
enum-field-accepts-non-member-names
Changes under src/ are evaluated.