Skip to content

Commit

Permalink
fix error handling in XSD types - finally overrides prior returns
Browse files Browse the repository at this point in the history
  • Loading branch information
sneakers-the-rat committed Oct 28, 2024
1 parent c71fb0c commit 997c3e7
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions linkml_runtime/utils/metamodelcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,16 @@ 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)
return datetime.time.fromisoformat(str(value)).isoformat()
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:
Expand All @@ -260,15 +261,16 @@ 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))
return value.isoformat()
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:
Expand All @@ -293,15 +295,16 @@ 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
return value.isoformat()
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:
Expand Down

0 comments on commit 997c3e7

Please sign in to comment.