Skip to content

Commit 7f8d7e2

Browse files
committed
[ModelicaSystem] fix mypy warnings - fix reorder of inputs
* define modelName as first (required!) argument * use *kwargs in tests
1 parent c57b8bc commit 7f8d7e2

File tree

5 files changed

+17
-17
lines changed

5 files changed

+17
-17
lines changed

OMPython/ModelicaSystem.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ def parse_simflags(simflags: str) -> dict[str, Optional[str | dict[str, str]]]:
314314
class ModelicaSystem:
315315
def __init__(
316316
self,
317-
fileName: Optional[str | os.PathLike] = None,
318-
modelName: Optional[str] = None,
317+
modelName: str,
318+
fileName: Optional[str | os.PathLike | pathlib.Path] = None,
319319
lmodel: Optional[list[str | tuple[str, str]]] = None,
320320
commandLineOptions: Optional[str] = None,
321321
variableFilter: Optional[str] = None,
@@ -330,10 +330,10 @@ def __init__(
330330
xml files, etc.
331331
332332
Args:
333-
fileName: Path to the model file. Either absolute or relative to
334-
the current working directory.
335333
modelName: The name of the model class. If it is contained within
336334
a package, "PackageName.ModelName" should be used.
335+
fileName: Path to the model file. Either absolute or relative to
336+
the current working directory.
337337
lmodel: List of libraries to be loaded before the model itself is
338338
loaded. Two formats are supported for the list elements:
339339
lmodel=["Modelica"] for just the library name
@@ -421,7 +421,7 @@ def __init__(
421421
self.loadFile(fileName=self.fileName)
422422

423423
# allow directly loading models from MSL without fileName
424-
elif fileName is None and modelName is not None:
424+
else:
425425
self.loadLibrary(lmodel=self.lmodel)
426426

427427
if build:

tests/test_ModelicaSystem.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def __del__(self):
2626
def testModelicaSystemLoop(self):
2727
def worker():
2828
filePath = (self.tmp / "M.mo").as_posix()
29-
m = OMPython.ModelicaSystem(filePath, "M")
29+
m = OMPython.ModelicaSystem(fileName=filePath, modelName="M")
3030
m.simulate()
3131
m.convertMo2Fmu(fmuType="me")
3232
for _ in range(10):
@@ -35,7 +35,7 @@ def worker():
3535
def test_setParameters(self):
3636
omc = OMPython.OMCSessionZMQ()
3737
model_path = omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels/"
38-
mod = OMPython.ModelicaSystem(model_path + "BouncingBall.mo", "BouncingBall")
38+
mod = OMPython.ModelicaSystem(fileName=model_path + "BouncingBall.mo", modelName="BouncingBall")
3939

4040
# method 1
4141
mod.setParameters("e=1.234")
@@ -59,7 +59,7 @@ def test_setParameters(self):
5959
def test_setSimulationOptions(self):
6060
omc = OMPython.OMCSessionZMQ()
6161
model_path = omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels/"
62-
mod = OMPython.ModelicaSystem(model_path + "BouncingBall.mo", "BouncingBall")
62+
mod = OMPython.ModelicaSystem(fileName=model_path + "BouncingBall.mo", modelName="BouncingBall")
6363

6464
# method 1
6565
mod.setSimulationOptions("stopTime=1.234")
@@ -89,7 +89,7 @@ def test_relative_path(self):
8989
model_relative = str(model_file)
9090
assert "/" not in model_relative
9191

92-
mod = OMPython.ModelicaSystem(model_relative, "M")
92+
mod = OMPython.ModelicaSystem(fileName=model_relative, modelName="M")
9393
assert float(mod.getParameters("a")[0]) == -1
9494
finally:
9595
# clean up the temporary file
@@ -99,7 +99,7 @@ def test_customBuildDirectory(self):
9999
filePath = (self.tmp / "M.mo").as_posix()
100100
tmpdir = self.tmp / "tmpdir1"
101101
tmpdir.mkdir()
102-
m = OMPython.ModelicaSystem(filePath, "M", customBuildDirectory=tmpdir)
102+
m = OMPython.ModelicaSystem(fileName=filePath, modelName="M", customBuildDirectory=tmpdir)
103103
assert m.getWorkDirectory().resolve() == tmpdir.resolve()
104104
result_file = tmpdir / "a.mat"
105105
assert not result_file.exists()
@@ -108,7 +108,7 @@ def test_customBuildDirectory(self):
108108

109109
def test_getSolutions(self):
110110
filePath = (self.tmp / "M.mo").as_posix()
111-
mod = OMPython.ModelicaSystem(filePath, "M")
111+
mod = OMPython.ModelicaSystem(fileName=filePath, modelName="M")
112112
x0 = 1
113113
a = -1
114114
tau = -1 / a
@@ -144,7 +144,7 @@ def test_getters(self):
144144
y = der(x);
145145
end M_getters;
146146
""")
147-
mod = OMPython.ModelicaSystem(model_file.as_posix(), "M_getters")
147+
mod = OMPython.ModelicaSystem(fileName=model_file.as_posix(), modelName="M_getters")
148148

149149
q = mod.getQuantities()
150150
assert isinstance(q, list)
@@ -322,7 +322,7 @@ def test_simulate_inputs(self):
322322
y = x;
323323
end M_input;
324324
""")
325-
mod = OMPython.ModelicaSystem(model_file.as_posix(), "M_input")
325+
mod = OMPython.ModelicaSystem(fileName=model_file.as_posix(), modelName="M_input")
326326

327327
mod.setSimulationOptions("stopTime=1.0")
328328

tests/test_ModelicaSystemCmd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self, *args, **kwargs):
2323
der(x) = x*a;
2424
end M;
2525
""")
26-
self.mod = OMPython.ModelicaSystem(self.model.as_posix(), "M")
26+
self.mod = OMPython.ModelicaSystem(fileName=self.model.as_posix(), modelName="M")
2727

2828
def __del__(self):
2929
shutil.rmtree(self.tmp, ignore_errors=True)

tests/test_linearization.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def __del__(self):
3131

3232
def test_example(self):
3333
filePath = (self.tmp / "linearTest.mo").as_posix()
34-
mod = OMPython.ModelicaSystem(filePath, "linearTest")
34+
mod = OMPython.ModelicaSystem(fileName=filePath, modelName="linearTest")
3535
[A, B, C, D] = mod.linearize()
3636
expected_matrixA = [[-3, 2, 0, 0], [-7, 0, -5, 1], [-1, 0, -1, 4], [0, 1, -1, 5]]
3737
assert A == expected_matrixA, f"Matrix does not match the expected value. Got: {A}, Expected: {expected_matrixA}"
@@ -61,7 +61,7 @@ def test_getters(self):
6161
y2 = phi + u1;
6262
end Pendulum;
6363
""")
64-
mod = OMPython.ModelicaSystem(model_file.as_posix(), "Pendulum", ["Modelica"])
64+
mod = OMPython.ModelicaSystem(fileName=model_file.as_posix(), modelName="Pendulum", lmodel=["Modelica"])
6565

6666
d = mod.getLinearizationOptions()
6767
assert isinstance(d, dict)

tests/test_optimization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def test_example(self):
4545
end BangBang2021;
4646
""")
4747

48-
mod = OMPython.ModelicaSystem(model_file.as_posix(), "BangBang2021")
48+
mod = OMPython.ModelicaSystem(fileName=model_file.as_posix(), modelName="BangBang2021")
4949

5050
mod.setOptimizationOptions(["numberOfIntervals=16", "stopTime=1",
5151
"stepSize=0.001", "tolerance=1e-8"])

0 commit comments

Comments
 (0)