Skip to content

ModelicaSystem - remove xml_file as class variable #321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: master
Choose a base branch
from

Conversation

syntron
Copy link
Contributor

@syntron syntron commented Jul 11, 2025

cleanup handling of the xml file & remove another class variable

preparation for OMCPath (PR #317)

needs also PR #321

syntron added 22 commits July 11, 2025 18:39
…ents

* fix some type hint issues in setInput()
* prepare for definition via dictionary replacing 'a=b' and '[a=b, c=d]' style
* rename from setMethodHelper()
* use _prepare_inputdata()
* cleanup code to align with new input as dict[str, str]

* setInput() is a special case
* replace eval() with ast.literal_eval() as a saver version
* use _prepare_input_data()
* simplify code
This method is used to set input values. It can be called with a sequence of input name and assigning
corresponding values as arguments as show in the example below. Compared to other set*() methods this is a
special case as value could be a list of tuples - these are converted to a string in _prepare_input_data()
and restored here via ast.literal_eval().
@syntron syntron force-pushed the ModelicaSystem_xml branch 2 times, most recently from f431876 to c80d31d Compare July 11, 2025 20:25
@syntron
Copy link
Contributor Author

syntron commented Jul 11, 2025

@ondras12345 do you have an idea why the last commit is needed to make mypy happy?

on my local checks there was no problem; I do not understand the background of the error in line 1528: n is str but should be int - why is n a str? There was no change in this code ...

@ondras12345
Copy link
Contributor

ondras12345 commented Jul 12, 2025

mypy does not like it when you reuse variable names with different types.

for n in nameVal:

Here, n is a str. Its scope is not limited to that for loop, so it still exists when you get here:
(n, m, p, x0, u0, A, B, C, D, stateVars, inputVars, outputVars) = result
self._linearized_inputs = inputVars
self._linearized_outputs = outputVars
self._linearized_states = stateVars
return LinearizationResult(n, m, p, A, B, C, D, x0, u0, stateVars,
inputVars, outputVars)

mypy does not know the type of result, so it assumes it is compatible with the previous (inferred) type of n. And when you try to pass that to LinearizationResult, it gives you the

OMPython/ModelicaSystem.py:1527: error: Argument 1 to "LinearizationResult" has incompatible type "str"; expected "int"  [arg-type]

This is my best guess.

It looks like there is now an option in mypy to allow variable redefinition, but I'm not sure if it would help in this case. If you want to try it, add this to pyproject.toml:

[tool.mypy]
allow_redefinition = true

@syntron
Copy link
Contributor Author

syntron commented Jul 12, 2025

@ondras12345 Thanks for your analyses - I now also see the problem in the usage of n. I do prefere to solve it and not use any special options / allow ambiguity.

However, did you notice, that current master (3e9c55b) does pass mypy without error and commit f958964 from this PR fails? There are no changes in this part of the code!

I plan to update this PR to include PR #321 - it removes the first use of n as variable for the for loop.

if self._has_inputs:
nameVal = self.getInputs()
for n in nameVal:
tupleList = nameVal.get(n)

will be changed to

        if self._inputs:  # if model has input quantities
            for key in self._inputs:
                val = self._inputs[key]

@syntron syntron force-pushed the ModelicaSystem_xml branch from c80d31d to 3ba84ab Compare July 12, 2025 10:06
@syntron
Copy link
Contributor Author

syntron commented Jul 12, 2025

Even with PR #321 this needed some additional checks as the variable value is used with different types (str / int / float):

        with open(file=overrideLinearFile, mode="w", encoding="utf-8") as fh:
            for key, value in self._override_variables.items():
                fh.write(f"{key}={value}\n")
            for key, value in self._linearization_options.items():
                fh.write(f"{key}={value}\n")

and

        if self._inputs:
            for key in self._inputs:
                data = self._inputs[key]
                if data is not None:
                    for value in data:
                        if value[0] < float(self._simulate_options["startTime"]):
                            raise ModelicaSystemError('Input time value is less than simulation startTime')

See commit 3ba84ab

@ondras12345
Copy link
Contributor

However, did you notice, that current master (3e9c55b) does pass mypy without error and commit f958964 from this PR fails? There are no changes in this part of the code!

No idea why this is happening. We specify an exact version of mypy, so it shouldn't be that the new CI run pulled a newer version:

- repo: https://github.com/pre-commit/mirrors-mypy.git
rev: "v1.15.0"

And we use
- name: Run pre-commit linters
run: 'pre-commit run --all-files'

so it should check all files in each run.

@ondras12345
Copy link
Contributor

See commit 3ba84ab

I don't think it's worth doing renames like this just to please mypy. If allow_redefinition = true would silence the warning, I think we should do that instead. Although I haven't tested it in this specific case, I do use this option in some of my personal projects without trouble.

@syntron
Copy link
Contributor Author

syntron commented Jul 12, 2025

See commit 3ba84ab

I don't think it's worth doing renames like this just to please mypy. If allow_redefinition = true would silence the warning, I think we should do that instead. Although I haven't tested it in this specific case, I do use this option in some of my personal projects without trouble.

The rename is not the only point of the commit / PR #321 - it does change other parts of the code at the same place and the rename is just an additional modification at the same point

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants