DataObjectProperty.decode_from_pdu: support returning the default value when the internal value is invalid#389
Conversation
…ue if the internal value is invalid
|
note: if you want to get rid of these pesky complaints about the coding style, just install yapf and isort via pip and run the |
odxtools/dataobjectproperty.py
Outdated
| return str(default_value) | ||
|
|
||
| # TODO: How to prevent this? | ||
| raise DecodeError(f"DOP {self.short_name} could not convert the coded value " |
There was a problem hiding this comment.
@andlaus what do you think about using str(internal_value) when not in strict mode as final fallback ?
There was a problem hiding this comment.
I don't really know: my hunch is that None is better because if the callling code encounters an unexpected type like str instead of int it almost certainly goes belly-up anyway and it is probably mentally a bit easier to associate None with "did not work". On the other hand, the internal value of the parameter would be lost in the warnings...
|
|
||
| internal_to_phys = self.compu_method.compu_internal_to_phys | ||
| default_value = internal_to_phys.compu_default_value if internal_to_phys else None | ||
|
|
||
| if default_value is not None: | ||
| return str(default_value) |
There was a problem hiding this comment.
do you have a reference to the spec for this? It still seems fishy to me to fall back to a default value when receiving an undecodable value. (encoding a PDU where the parameter in question is left unspecified is a different story IMO.)
There was a problem hiding this comment.
Section 7.3.6.6.7
The optional COMPU-DEFAULT-VALUE can be used to define the physical value if the internal value does not lie in any given interval.
There was a problem hiding this comment.
ok, thanks. That's a build-in footgun IMO, but...
|
@andlaus is it possible to get a new release today? Thank you! |
This merge request adds support for returning the default value when the internal value is invalid. If the internal value is not defined, the method will check for a defined default value and return it; otherwise, it will raise an exception.
odxtools version: 9.2.0