Skip to content

Commit 3b369b5

Browse files
authored
DataObjectProperty.decode_from_pdu: support returning the default value when the internal value is invalid (#389)
* DataObjectProperty.decode_from_pdu: support returning the default value if the internal value is invalid * fix coding style * only raise the exception in strict mode * fix linting * update * remove comment
1 parent 9e986a6 commit 3b369b5

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

odxtools/dataobjectproperty.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from .diagcodedtype import DiagCodedType
1111
from .dopbase import DopBase
1212
from .encodestate import EncodeState
13-
from .exceptions import DecodeError, EncodeError, odxraise, odxrequire
13+
from .exceptions import EncodeError, odxraise, odxrequire
1414
from .internalconstr import InternalConstr
1515
from .odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId, OdxLinkRef
1616
from .odxtypes import AtomicOdxType, ParameterValue
@@ -136,11 +136,17 @@ def decode_from_pdu(self, decode_state: DecodeState) -> ParameterValue:
136136

137137
if self.compu_method.is_valid_internal_value(internal):
138138
return self.compu_method.convert_internal_to_physical(internal)
139-
else:
140-
# TODO: How to prevent this?
141-
raise DecodeError(
142-
f"DOP {self.short_name} could not convert the coded value "
143-
f" {repr(internal)} to physical type {self.physical_type.base_data_type}.")
139+
140+
internal_to_phys = self.compu_method.compu_internal_to_phys
141+
default_value = internal_to_phys.compu_default_value if internal_to_phys else None
142+
143+
if default_value is not None:
144+
return str(default_value)
145+
146+
odxraise(f"DOP {self.short_name} could not convert the coded value "
147+
f"{repr(internal)} to physical type {self.physical_type.base_data_type}.")
148+
149+
return None
144150

145151
def is_valid_physical_value(self, physical_value: ParameterValue) -> bool:
146152
return self.compu_method.is_valid_physical_value(cast(AtomicOdxType, physical_value))

0 commit comments

Comments
 (0)