Skip to content

Commit 0edb506

Browse files
jkhsjdhjss-heppner
authored andcommitted
model.datatypes: add Decimal parsing
During construction of a `Decimal`, an `InvalidOperation` exception may occur. Since the user only expects a `ValueError` from this function, because all other parsers only raise `ValueError`s, the `InvalidOperation` error-type is handled and re-raised as a `ValueError`. Fix #99
1 parent c356f89 commit 0edb506

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

basyx/aas/model/datatypes.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,16 @@ def from_xsd(value: str, type_: Type[AnyXSDType]) -> AnyXSDType: # workaround.
548548
return _parse_xsd_bool(value)
549549
elif issubclass(type_, (int, float, str)):
550550
return type_(value)
551+
elif type_ is decimal.Decimal:
552+
try:
553+
return decimal.Decimal(value)
554+
except decimal.InvalidOperation as e:
555+
# We cannot use the original exception text here, because the text differs depending on
556+
# whether the _decimal or _pydecimal module is used. Furthermore, the _decimal doesn't provide
557+
# a real error message suited for end users, but provides a list of conditions that trigger the exception.
558+
# See https://github.com/python/cpython/issues/76420
559+
# Raising our own error message allows us to verify it in the tests.
560+
raise ValueError(f"Cannot convert '{value}' to Decimal!") from e
551561
elif type_ is Duration:
552562
return _parse_xsd_duration(value)
553563
elif type_ is YearMonthDuration:

0 commit comments

Comments
 (0)