Skip to content

Commit fbfcf78

Browse files
JorgelmhCopilot
andauthored
Add dynamic optional typing import (#236)
* Add dynamic optional typing import when Signature updates the class parameters of the model * Update pythonfmu/builder.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Upgrade ubuntu image due to rollout * Remove regex and just add import as Python won't throw errors in case of multiple imports --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent c98cc56 commit fbfcf78

3 files changed

Lines changed: 14 additions & 10 deletions

File tree

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
runs-on: ${{ matrix.os }}
1515
strategy:
1616
matrix:
17-
os: [ubuntu-20.04, windows-2019]
17+
os: [ubuntu-22.04, windows-2019]
1818

1919
steps:
2020
- uses: actions/checkout@v3
@@ -85,7 +85,7 @@ jobs:
8585
strategy:
8686
fail-fast: false
8787
matrix:
88-
os: [ubuntu-20.04, windows-2019]
88+
os: [ubuntu-22.04, windows-2019]
8989
python-version: ['3.9', '3.11', '3.13']
9090
timeout-minutes: 15
9191

.github/workflows/publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
runs-on: ${{ matrix.os }}
1111
strategy:
1212
matrix:
13-
os: [ubuntu-20.04, windows-2019]
13+
os: [ubuntu-22.04, windows-2019]
1414

1515
steps:
1616
- uses: actions/checkout@v2

pythonfmu/builder.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import importlib
66
import itertools
77
import logging
8+
import re
89
import shutil
910
import sys
1011
import tempfile
@@ -102,16 +103,16 @@ def get_model_class(src: Path) -> Fmi2Slave:
102103
def update_model_parameters(src: Path, model: Fmi2Slave, newargs: dict) -> str:
103104
"""
104105
Update the model parameters in the __init__ function of a given module.
105-
This function modifies the default values of the parameters in the __init__
106-
function of the specified model with the new values provided in the newargs
106+
This function modifies the default values of the parameters in the __init__
107+
function of the specified model with the new values provided in the newargs
107108
dictionary. It returns the updated module code as a string.
108109
109110
Args:
110111
src (Path): The path to the source file containing the module.
111-
model (Fmi2Slave): The model object whose __init__ function parameters
112+
model (Fmi2Slave): The model object whose __init__ function parameters
112113
need to be updated.
113-
newargs (dict): A dictionary containing the new parameter values. The
114-
keys should be the parameter names and the values should
114+
newargs (dict): A dictionary containing the new parameter values. The
115+
keys should be the parameter names and the values should
115116
be the new default values.
116117
Returns:
117118
str: The updated module code as a string.
@@ -143,7 +144,7 @@ def update_model_parameters(src: Path, model: Fmi2Slave, newargs: dict) -> str:
143144
else:
144145
par = pars[p]
145146
newpars.append(par)
146-
signew = inspect.Signature(parameters=newpars)
147+
signew = inspect.Signature(parameters=newpars)
147148

148149
# Replace the signature of the __init__ function
149150
init_line = inspect.getsourcelines(init)[1]
@@ -153,7 +154,10 @@ def update_model_parameters(src: Path, model: Fmi2Slave, newargs: dict) -> str:
153154

154155
from_init = from_init.replace(from_init[start - 1 : end], str(signew), 1)
155156
module_code = "".join(line for line in module_lines[0][: init_line - 1]) + from_init
156-
157+
158+
# Ensure that Optional is imported from typing.
159+
module_code = "from typing import Optional\n" + module_code
160+
157161
return module_code
158162

159163
def get_model_description(filepath: Path, module_name: str, class_name: str) -> Tuple[str, Element]:

0 commit comments

Comments
 (0)