API/serde: fix construction of Enum values

This commit is contained in:
Johannes Kirschbauer
2024-11-27 09:56:41 +01:00
parent f8b98dfe54
commit 4e61df9601

View File

@@ -230,10 +230,18 @@ def construct_value(
raise ClanError(msg, location=f"{loc}") raise ClanError(msg, location=f"{loc}")
return field_value return field_value
# Enums
if origin is Enum: if origin is Enum:
if field_value not in origin.__members__: if field_value not in origin.__members__:
msg = f"Expected one of {', '.join(origin.__members__)}, got {field_value}" msg = f"Expected one of {', '.join(origin.__members__)}, got {field_value}"
raise ClanError(msg, location=f"{loc}") 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: if origin is Annotated:
(base_type,) = get_args(t) (base_type,) = get_args(t)