Skip to content

Commit

Permalink
CompuMethod: add/fix type annotations
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Lauser <andreas.lauser@mbition.io>
Signed-off-by: Christian Hackenbeck <christian.hackenbeck@mbition.io>
Signed-off-by: Alexander Walz <alexander.walz@mbition.io>
  • Loading branch information
andlaus committed Sep 27, 2023
1 parent 20c8bf0 commit 3bc3f60
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
14 changes: 7 additions & 7 deletions odxtools/compumethods/compumethod.py
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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
24 changes: 19 additions & 5 deletions odxtools/compumethods/tabintpcompumethod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)
4 changes: 2 additions & 2 deletions odxtools/parameterinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3bc3f60

Please sign in to comment.