API/serde: fix construction of Enum values

This commit is contained in:
Johannes Kirschbauer
2024-11-27 09:56:41 +01:00
parent bcc0f9e61d
commit b68cf7a7e5

View File

@@ -230,10 +230,18 @@ def construct_value(
raise ClanError(msg, location=f"{loc}")
return field_value
# Enums
if origin is Enum:
if field_value not in origin.__members__:
msg = f"Expected one of {', '.join(origin.__members__)}, got {field_value}"
raise ClanError(msg, location=f"{loc}")
return origin.__members__[field_value] # type: ignore
if isinstance(t, type) and issubclass(t, Enum):
if field_value not in t.__members__:
msg = f"Expected one of {', '.join(t.__members__)}, got {field_value}"
raise ClanError(msg, location=f"{loc}")
return t.__members__[field_value] # type: ignore
if origin is Annotated:
(base_type,) = get_args(t)