diff --git a/odxtools/compumethods/compumethod.py b/odxtools/compumethods/compumethod.py index 9df65560..9abbe060 100644 --- a/odxtools/compumethods/compumethod.py +++ b/odxtools/compumethods/compumethod.py @@ -1,9 +1,9 @@ # SPDX-License-Identifier: MIT import abc from dataclasses import dataclass -from typing import Literal, Union +from typing import List, Literal, Optional -from ..odxtypes import DataType +from ..odxtypes import AtomicOdxType, DataType CompuMethodCategory = Literal[ "IDENTICAL", @@ -24,17 +24,17 @@ class CompuMethod(abc.ABC): def category(self) -> CompuMethodCategory: pass - def convert_physical_to_internal(self, physical_value): + def convert_physical_to_internal(self, physical_value: AtomicOdxType) -> AtomicOdxType: raise NotImplementedError() - def convert_internal_to_physical(self, internal_value) -> Union[int, float, str]: + def convert_internal_to_physical(self, internal_value: AtomicOdxType) -> AtomicOdxType: raise NotImplementedError() - def is_valid_physical_value(self, physical_value): + def is_valid_physical_value(self, physical_value: AtomicOdxType) -> bool: raise NotImplementedError() - def is_valid_internal_value(self, internal_value): + def is_valid_internal_value(self, internal_value: AtomicOdxType) -> bool: raise NotImplementedError() - def get_valid_physical_values(self): + def get_valid_physical_values(self) -> Optional[List[DataType]]: return None diff --git a/odxtools/compumethods/tabintpcompumethod.py b/odxtools/compumethods/tabintpcompumethod.py index 0c6c2299..6be2b80b 100644 --- a/odxtools/compumethods/tabintpcompumethod.py +++ b/odxtools/compumethods/tabintpcompumethod.py @@ -3,7 +3,7 @@ from typing import List, Tuple, Union from ..exceptions import DecodeError, EncodeError, odxassert, odxraise -from ..odxtypes import DataType +from ..odxtypes import AtomicOdxType, DataType from .compumethod import CompuMethod, CompuMethodCategory from .limit import IntervalType, Limit @@ -115,7 +115,11 @@ def _piecewise_linear_interpolate(self, x: Union[int, float], return None - def convert_physical_to_internal(self, physical_value: Union[int, float]) -> Union[int, float]: + def convert_physical_to_internal(self, physical_value: AtomicOdxType) -> AtomicOdxType: + if not isinstance(physical_value, (int, float)): + raise EncodeError("The type of values of tab-intp compumethods must " + "either int or float") + reference_points = list(zip(self.physical_points, self.internal_points)) result = self._piecewise_linear_interpolate(physical_value, reference_points) @@ -127,7 +131,11 @@ def convert_physical_to_internal(self, physical_value: Union[int, float]) -> Uni odxraise() return res - def convert_internal_to_physical(self, internal_value: Union[int, float]) -> Union[int, float]: + def convert_internal_to_physical(self, internal_value: AtomicOdxType) -> AtomicOdxType: + if not isinstance(internal_value, (int, float)): + raise EncodeError("The internal type of values of tab-intp compumethods must " + "either int or float") + reference_points = list(zip(self.internal_points, self.physical_points)) result = self._piecewise_linear_interpolate(internal_value, reference_points) @@ -139,10 +147,16 @@ def convert_internal_to_physical(self, internal_value: Union[int, float]) -> Uni odxraise() return res - def is_valid_physical_value(self, physical_value: Union[int, float]) -> bool: + def is_valid_physical_value(self, physical_value: AtomicOdxType) -> bool: + if not isinstance(physical_value, (int, float)): + return False + return min(self.physical_points) <= physical_value and physical_value <= max( self.physical_points) - def is_valid_internal_value(self, internal_value: Union[int, float]) -> bool: + def is_valid_internal_value(self, internal_value: AtomicOdxType) -> bool: + if not isinstance(internal_value, (int, float)): + return False + return min(self.internal_points) <= internal_value and internal_value <= max( self.internal_points) diff --git a/odxtools/parameterinfo.py b/odxtools/parameterinfo.py index b3fe8575..210e400d 100644 --- a/odxtools/parameterinfo.py +++ b/odxtools/parameterinfo.py @@ -86,8 +86,8 @@ def parameter_info(param_list: Iterable[Union[Parameter, EndOfPduField]]) -> str ul = cm.physical_upper_limit result += (f" range: " f"{'[' if ll.interval_type == IntervalType.CLOSED else '('}" - f"{ll.value}, " - f"{ul.value}" + f"{ll.value!r}, " + f"{ul.value!r}" f"{']' if ul.interval_type == IntervalType.CLOSED else ')'}\n") unit = dop.unit