-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: Consolidate validation of enum values and raise ValueError on in…
…valid values (#263)
- Loading branch information
1 parent
1b89ff7
commit b4fade9
Showing
5 changed files
with
56 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from enum import IntEnum | ||
|
||
|
||
class ParamEnum(IntEnum): | ||
"""Wraps IntEnum to provide validation of a requested item. | ||
Intended for enums used for function parameters. | ||
Use enum.get_value(item) for this behavior instead of builtin enum[item]. | ||
""" | ||
|
||
@classmethod | ||
def get_value(cls, item): | ||
"""Validate incoming item and raise a ValueError with valid options if not present.""" | ||
try: | ||
return cls[item].value | ||
except KeyError: | ||
valid_options = {e.name for e in cls} | ||
raise ValueError( | ||
"'{}' is not a valid option, must be one of '{}'".format( | ||
item, "', '".join(valid_options) | ||
) | ||
) |
This file contains 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
This file contains 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