From 997c3e7a18259ee31277b7043fd619196b297e71 Mon Sep 17 00:00:00 2001 From: sneakers-the-rat Date: Mon, 28 Oct 2024 14:37:38 -0700 Subject: [PATCH] fix error handling in XSD types - finally overrides prior returns --- linkml_runtime/utils/metamodelcore.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/linkml_runtime/utils/metamodelcore.py b/linkml_runtime/utils/metamodelcore.py index d490ee2d..fbadf7df 100644 --- a/linkml_runtime/utils/metamodelcore.py +++ b/linkml_runtime/utils/metamodelcore.py @@ -229,6 +229,7 @@ def __new__(cls, value: Union[str, datetime.time, datetime.datetime, Literal]) - raise ValueError(f"{value} is not a valid time") if isinstance(value, Literal): value = value.value + try: if not isinstance(value, datetime.time): value = datetime.time.fromisoformat(value) @@ -236,8 +237,8 @@ def __new__(cls, value: Union[str, datetime.time, datetime.datetime, Literal]) - except (TypeError, ValueError): if is_strict(): raise - finally: - return str(value) + + return str(value) @classmethod def is_valid(cls, value: Union[str, datetime.time, datetime.datetime, Literal]) -> bool: @@ -260,6 +261,7 @@ def __new__(cls, value: Union[str, datetime.date, Literal]) -> str: raise ValueError(f"{value} is not a valid date") if isinstance(value, Literal): value = value.value + try: if not isinstance(value, datetime.date): value = datetime.date.fromisoformat(str(value)) @@ -267,8 +269,8 @@ def __new__(cls, value: Union[str, datetime.date, Literal]) -> str: except (TypeError, ValueError): if is_strict(): raise - finally: - return str(value) + + return str(value) @classmethod def is_valid(cls, value: Union[str, datetime.date, Literal]) -> bool: @@ -293,6 +295,7 @@ def __new__(cls, value: Union[str, datetime.datetime, Literal]) -> str: raise ValueError(f"{value} is not a valid datetime") if isinstance(value, Literal): value = value.value + try: if not isinstance(value, datetime.datetime): value = datetime.datetime.fromisoformat(value) # Note that this handles non 'T' format as well @@ -300,8 +303,8 @@ def __new__(cls, value: Union[str, datetime.datetime, Literal]) -> str: except (TypeError, ValueError): if is_strict(): raise - finally: - return str(value) + + return str(value) @classmethod def is_valid(cls, value: Union[str, datetime.datetime, Literal]) -> bool: