Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle enums when key passed instead of value #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gblackadder
Copy link
Collaborator

For pyMetadataEditor, the enums in templates can be ignored. But we don't have that option here since the enums are in the base types. So instead, make the enum robust to receiving the key as well as the value at instantiation.

For example I was seeing errors like:

series_description.related_indicators.1.type

Input should be 'concept', 'disaggregation' or 'derivation' [type=enum, input_value='Derivation', input_type=str]
For further information visit https://errors.pydantic.dev/2.10/v/enum/
I have written a custom Enum class that allows the input to be either the key or the value of the Enum.

Example:

class IndicatorType(EnumWithValueOrKey):
    Concept = "concept"
    Disaggregation = "disaggregation"
    Derivation = "derivation"
    
# Example usage:
# This will work
IndicatorType("concept")  # gives IndicatorType.Concept
IndicatorType("Concept")  # gives IndicatorType.Concept

# This will raise a ValidationError
IndicatorType("Invalid")

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

Successfully merging this pull request may close these issues.

1 participant