Fix Enum field by-name lookup to only return actual members#2902
Merged
sloria merged 4 commits intomarshmallow-code:devfrom Mar 25, 2026
Merged
Fix Enum field by-name lookup to only return actual members#2902sloria merged 4 commits intomarshmallow-code:devfrom
sloria merged 4 commits intomarshmallow-code:devfrom
Conversation
Use dict-style access (self.enum[val]) instead of getattr(self.enum, val) for by-name deserialization. getattr returns any attribute of the Enum class, not just members. For example, passing "mro" or "__class__" would return built-in methods/attributes instead of raising a validation error. The enum item access operator [] only looks up actual enum members, so non-member attribute names now correctly raise a validation error.
sloria
reviewed
Feb 20, 2026
Member
sloria
left a comment
There was a problem hiding this comment.
Thanks! Mind adding a test and adding yourself to AUTHORS.rst?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
Enumfield's by-name deserialization usesgetattr(self.enum, val)to look up members. However,getattrreturns any attribute of the Enum class, not just actual enum members. This means inputs like"mro","__class__", or"__members__"silently return non-Enum objects instead of raising a validation error.Problem
When a user submits one of these attribute names as input, the
Enumfield returns the raw attribute/method object instead of an Enum member. Downstream code expecting an Enum member would break or behave unpredictably.Fix
Replace
getattr(self.enum, val)withself.enum[val]. The[]operator on Enum classes only looks up actual members (viaEnumMeta.__getitem__), so non-member names correctly raiseKeyError, which we catch and convert to a validation error.