Skip to content

Commit

Permalink
resolved issues raised by mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaasRostock committed Nov 5, 2024
1 parent ee70e90 commit 3d48030
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/component_model/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .model import Model # noqa: F401
from .variable import Variable # noqa: F401
from component_model.model import Model # noqa: F401
from component_model.variable import Variable # noqa: F401
6 changes: 3 additions & 3 deletions src/component_model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
from pythonfmu.enums import Fmi2Causality as Causality # type: ignore
from pythonfmu.enums import Fmi2Variability as Variability # type: ignore
from pythonfmu.fmi2slave import FMI2_MODEL_OPTIONS # type: ignore
from src.component_model.utils.logger import get_module_logger
from src.component_model.variable import Variable

from component_model.caus_var_ini import Initial
from component_model.utils.logger import get_module_logger
from component_model.variable import Variable

logger = get_module_logger(__name__, level=0)
Value: TypeAlias = str | int | float | bool | Enum
Expand Down Expand Up @@ -326,7 +326,7 @@ def ensure_requirements(existing_file: str | Path | None, temp_file: Path) -> Pa
@staticmethod
def build(
script: str = "",
project_files: list[str, Path] | None = None,
project_files: list[str | Path] | None = None,
dest: str | os.PathLike[str] = ".",
documentation_folder: Path | None = None,
):
Expand Down
2 changes: 1 addition & 1 deletion src/component_model/utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class MsgCounterHandler(logging.StreamHandler):
def __init__(self, logger, *args, **kwargs):
self.logger = logger
try:
self._out = sys.stdout.shell
self._out = sys.stdout.shell # type: ignore[union-attr]
except AttributeError:
try:
self._out = sys.stdout
Expand Down
2 changes: 1 addition & 1 deletion src/component_model/utils/osp.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def make_initial_value(var: str, val: bool | int | float | str):
return initial

simulators = ET.Element("Simulators")
if len(models):
if models:
for m, props in models.items():
# Note: instantiated model names might be small, but FMUs are based on class names and are therefore capitalized
simulator = ET.Element(
Expand Down
26 changes: 16 additions & 10 deletions src/component_model/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
from pythonfmu.enums import Fmi2Causality as Causality # type: ignore
from pythonfmu.enums import Fmi2Variability as Variability # type: ignore
from pythonfmu.variables import ScalarVariable # type: ignore
from src.component_model.caus_var_ini import Initial, check_causality_variability_initial, use_start
from src.component_model.utils.logger import get_module_logger

from component_model.caus_var_ini import Initial, check_causality_variability_initial, use_start
from component_model.utils.logger import get_module_logger

logger = get_module_logger(__name__, level=0)
PyType: TypeAlias = str | int | float | bool | Enum
Expand Down Expand Up @@ -183,7 +184,7 @@ def __init__(

self._annotations = annotations
self._check = value_check # unique for all elements in compound variables
self._typ = typ # preliminary. Will be adapted if not explicitly provided (None)
self._typ: type | None = typ # preliminary. Will be adapted if not explicitly provided (None)

self.on_step = on_step # hook to define a function of currentTime and time step dT,
# to be performed during Model.do_step for input variables
Expand Down Expand Up @@ -347,7 +348,7 @@ def getter(self):
if issubclass(self._typ, Enum): # native Enums do not exist in FMI2. Convert to int
value = value.value
elif not isinstance(value, self._typ): # other type conversion
value = self._typ(value)
value = self._typ(value) # type: ignore[call-arg]
if self._check & Check.units: # Convert 'value' display.u -> base unit
if self._display[0] is not None:
value = self.display[0][2](value)
Expand All @@ -360,7 +361,7 @@ def getter(self):
else:
for i in range(self._len): # check whether conversion to _typ is necessary
if not isinstance(value[i], self._typ):
value[i] = self._typ(value[i])
value[i] = self._typ(value[i]) # type: ignore[call-arg]
if self._check & Check.units: # Convert 'value' display.u -> base unit
for i in range(self._len):
if self._display[i] is not None:
Expand All @@ -382,7 +383,9 @@ def _init_range(self, rng: tuple | None) -> tuple:
def ensure_display_limits(val: PyType, idx: int, right: bool):
"""Ensure that value is provided as display unit and that limits are included in range."""
if self._display[idx] is not None: # Range in display units!
val = self._display[idx][2](val)
_val = self._display[idx]
assert isinstance(_val, tuple)
val = _val[2](val)
if isinstance(val, float) and abs(val) != float("inf") and int(val) != val:
if right:
val += 1e-15
Expand Down Expand Up @@ -422,8 +425,8 @@ def ensure_display_limits(val: PyType, idx: int, right: bool):
raise VariableInitError(
f"The supplied range value {str(r)} does not conform to the unit type {self._unit[idx]}"
)
elif du is not None and self._display[idx] is not None and du[0] != self._display[idx][0]:
raise VariableInitError(f"Range unit {du[0]} != start {self._display[idx][0]}!")
elif du is not None and self._display[idx] is not None and du[0] != self._display[idx][0]: # type: ignore[index]
raise VariableInitError(f"Range unit {du[0]} != start {self._display[idx][0]}!") # type: ignore[index]
q = ensure_display_limits(q, idx, len(i_range) > 0)
i_range.append(q)

Expand Down Expand Up @@ -479,7 +482,9 @@ def check_range(self, value: PyType | Compound | None, idx: int | None = None, d

elif isinstance(value, (int, float)) and all(isinstance(x, (int, float)) for x in self._range[idx]):
if not disp and self._display[idx] is not None: # check an internal unit value
value = self._display[idx][2](value)
_val = self._display[idx]
assert isinstance(_val, tuple)
value = _val[2](value)
return self._range[idx] is None or self._range[idx][0] <= value <= self._range[idx][1] # type: ignore
else:
raise VariableUseError(f"check_range(): value={value}, type={self.typ}, range={self.range}") from None
Expand Down Expand Up @@ -641,7 +646,8 @@ def substr(alt1: str, alti: str):
declaredType = {"int": "Integer", "bool": "Boolean", "float": "Real", "str": "String", "Enum": "Enumeration"}[
self.typ.__qualname__
] # translation of python to FMI primitives. Same for all components
do_use_start = use_start(self._causality, self._variability, self._initial)
assert self._initial is not None, "Initial shall be properly set at this point"
do_use_start = use_start(causality=self._causality, variability=self._variability, initial=self._initial)
svars = []
for i in range(self._len):
sv = ET.Element(
Expand Down

0 comments on commit 3d48030

Please sign in to comment.