gh-150220: List valid member values in Enum's default lookup ValueError#150223
Closed
drain99 wants to merge 1 commit into
Closed
gh-150220: List valid member values in Enum's default lookup ValueError#150223drain99 wants to merge 1 commit into
drain99 wants to merge 1 commit into
Conversation
…lueError
Append the list of valid member values to the default ValueError raised
by Enum.__new__ when an unknown value is looked up, but only when the
class has not overridden _missing_. Flag/IntFlag and any user enum
defining a custom _missing_ are unaffected and continue to produce the
bare default message.
>>> class Currency(Enum):
... USD = 'USD'; EUR = 'EUR'; JPY = 'JPY'
>>> Currency('USO')
ValueError: 'USO' is not a valid Currency. Valid values: 'USD', 'EUR', 'JPY'
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
Member
|
See issue for the closing rationale. |
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.
The user has to inspect
cls.__members__to discover the legal set.Pydantic and similar validators already include the list (
"Input should be 'USD', 'EUR' or 'JPY'").…but only when
_missing_has not been overridden. Subclasses thatoverride
_missing_(case-insensitive matching, aliases,Flag/IntFlag's bitwise composition, ...) own their own errormessages.
Enum/IntEnum/StrEnum(default_missing_)Flag/IntFlag(override for bitwise)_missing_returningNone_missing_raisingValueErrorExisting
test_enum.pysuite passes unchanged.Additive.
ValueErrortype and prefix unchanged; only aValid values: ...suffix.Code asserting on the exact bare message would need a minor change.