From c412f6f3e5d1f93832ccbfea6371fc2ff445e8a1 Mon Sep 17 00:00:00 2001 From: syntron Date: Wed, 28 May 2025 20:36:43 +0200 Subject: [PATCH 1/2] [ModelicaSystem] fix mypy warnings - fix reorder of inputs * define modelName as first (required!) argument * use *kwargs in tests --- OMPython/ModelicaSystem.py | 10 +++++----- tests/test_ModelicaSystem.py | 8 ++++---- tests/test_ModelicaSystemCmd.py | 2 +- tests/test_linearization.py | 2 +- tests/test_optimization.py | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/OMPython/ModelicaSystem.py b/OMPython/ModelicaSystem.py index e362520a..731723a0 100644 --- a/OMPython/ModelicaSystem.py +++ b/OMPython/ModelicaSystem.py @@ -314,8 +314,8 @@ def parse_simflags(simflags: str) -> dict[str, Optional[str | dict[str, str]]]: class ModelicaSystem: def __init__( self, - fileName: Optional[str | os.PathLike] = None, - modelName: Optional[str] = None, + modelName: str, + fileName: Optional[str | os.PathLike | pathlib.Path] = None, lmodel: Optional[list[str | tuple[str, str]]] = None, commandLineOptions: Optional[str] = None, variableFilter: Optional[str] = None, @@ -330,10 +330,10 @@ def __init__( xml files, etc. Args: - fileName: Path to the model file. Either absolute or relative to - the current working directory. modelName: The name of the model class. If it is contained within a package, "PackageName.ModelName" should be used. + fileName: Path to the model file. Either absolute or relative to + the current working directory. lmodel: List of libraries to be loaded before the model itself is loaded. Two formats are supported for the list elements: lmodel=["Modelica"] for just the library name @@ -421,7 +421,7 @@ def __init__( self.loadFile(fileName=self.fileName) # allow directly loading models from MSL without fileName - elif fileName is None and modelName is not None: + else: self.loadLibrary(lmodel=self.lmodel) if build: diff --git a/tests/test_ModelicaSystem.py b/tests/test_ModelicaSystem.py index 202e066d..71da85c8 100644 --- a/tests/test_ModelicaSystem.py +++ b/tests/test_ModelicaSystem.py @@ -57,7 +57,7 @@ def test_setParameters(): def test_setSimulationOptions(): omc = OMPython.OMCSessionZMQ() model_path = omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels/" - mod = OMPython.ModelicaSystem(model_path + "BouncingBall.mo", "BouncingBall") + mod = OMPython.ModelicaSystem(fileName=model_path + "BouncingBall.mo", modelName="BouncingBall") # method 1 mod.setSimulationOptions("stopTime=1.234") @@ -88,7 +88,7 @@ def test_relative_path(model_firstorder): model_relative = str(model_file) assert "/" not in model_relative - mod = OMPython.ModelicaSystem(model_relative, "M") + mod = OMPython.ModelicaSystem(fileName=model_relative, modelName="M") assert float(mod.getParameters("a")[0]) == -1 finally: model_file.unlink() # clean up the temporary file @@ -145,7 +145,7 @@ def test_getters(tmp_path): y = der(x); end M_getters; """) - mod = OMPython.ModelicaSystem(model_file.as_posix(), "M_getters") + mod = OMPython.ModelicaSystem(fileName=model_file.as_posix(), modelName="M_getters") q = mod.getQuantities() assert isinstance(q, list) @@ -324,7 +324,7 @@ def test_simulate_inputs(tmp_path): y = x; end M_input; """) - mod = OMPython.ModelicaSystem(model_file.as_posix(), "M_input") + mod = OMPython.ModelicaSystem(fileName=model_file.as_posix(), modelName="M_input") mod.setSimulationOptions("stopTime=1.0") diff --git a/tests/test_ModelicaSystemCmd.py b/tests/test_ModelicaSystemCmd.py index f82510df..32f111b2 100644 --- a/tests/test_ModelicaSystemCmd.py +++ b/tests/test_ModelicaSystemCmd.py @@ -16,7 +16,7 @@ def model_firstorder(tmp_path): def test_simflags(model_firstorder): - mod = OMPython.ModelicaSystem(model_firstorder.as_posix(), "M") + mod = OMPython.ModelicaSystem(fileName=model_firstorder.as_posix(), modelName="M") mscmd = OMPython.ModelicaSystemCmd(runpath=mod.tempdir, modelname=mod.modelName) mscmd.args_set({ "noEventEmit": None, diff --git a/tests/test_linearization.py b/tests/test_linearization.py index e9f0f6d7..2c79190c 100644 --- a/tests/test_linearization.py +++ b/tests/test_linearization.py @@ -55,7 +55,7 @@ def test_getters(tmp_path): y2 = phi + u1; end Pendulum; """) - mod = OMPython.ModelicaSystem(model_file.as_posix(), "Pendulum", ["Modelica"]) + mod = OMPython.ModelicaSystem(fileName=model_file.as_posix(), modelName="Pendulum", lmodel=["Modelica"]) d = mod.getLinearizationOptions() assert isinstance(d, dict) diff --git a/tests/test_optimization.py b/tests/test_optimization.py index 672de4a6..aa74df79 100644 --- a/tests/test_optimization.py +++ b/tests/test_optimization.py @@ -33,7 +33,7 @@ def test_optimization_example(tmp_path): end BangBang2021; """) - mod = OMPython.ModelicaSystem(model_file.as_posix(), "BangBang2021") + mod = OMPython.ModelicaSystem(fileName=model_file.as_posix(), modelName="BangBang2021") mod.setOptimizationOptions(["numberOfIntervals=16", "stopTime=1", "stepSize=0.001", "tolerance=1e-8"]) From 9a8050385d9ae8cf05551bc564525639ac5e03ac Mon Sep 17 00:00:00 2001 From: syntron Date: Tue, 10 Jun 2025 17:44:29 +0200 Subject: [PATCH 2/2] [ModelicaSystem] fix mypy warnings - update it to a backward compatible solution --- OMPython/ModelicaSystem.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/OMPython/ModelicaSystem.py b/OMPython/ModelicaSystem.py index 731723a0..ee444c0f 100644 --- a/OMPython/ModelicaSystem.py +++ b/OMPython/ModelicaSystem.py @@ -314,8 +314,8 @@ def parse_simflags(simflags: str) -> dict[str, Optional[str | dict[str, str]]]: class ModelicaSystem: def __init__( self, - modelName: str, fileName: Optional[str | os.PathLike | pathlib.Path] = None, + modelName: Optional[str] = None, lmodel: Optional[list[str | tuple[str, str]]] = None, commandLineOptions: Optional[str] = None, variableFilter: Optional[str] = None, @@ -330,10 +330,10 @@ def __init__( xml files, etc. Args: - modelName: The name of the model class. If it is contained within - a package, "PackageName.ModelName" should be used. fileName: Path to the model file. Either absolute or relative to the current working directory. + modelName: The name of the model class. If it is contained within + a package, "PackageName.ModelName" should be used. lmodel: List of libraries to be loaded before the model itself is loaded. Two formats are supported for the list elements: lmodel=["Modelica"] for just the library name @@ -360,9 +360,13 @@ def __init__( mod = ModelicaSystem("ModelicaModel.mo", "modelName", ["Modelica"]) mod = ModelicaSystem("ModelicaModel.mo", "modelName", [("Modelica","3.2.3"), "PowerSystems"]) """ + if fileName is None and modelName is None and not lmodel: # all None raise ModelicaSystemError("Cannot create ModelicaSystem object without any arguments") + if modelName is None: + raise ModelicaSystemError("A modelname must be provided (argument modelName)!") + self.quantitiesList = [] self.paramlist = {} self.inputlist = {} @@ -421,7 +425,7 @@ def __init__( self.loadFile(fileName=self.fileName) # allow directly loading models from MSL without fileName - else: + elif fileName is None and modelName is not None: self.loadLibrary(lmodel=self.lmodel) if build: