Skip to content

Commit 2767263

Browse files
authored
Apply suggestions from code review
1 parent 6efaa0e commit 2767263

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

pyomo/contrib/parmest/tests/test_utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,13 @@ def test_update_model_from_suffix_not_numeric(self):
154154
m.unknown_parameters[m.x] = 0.0 # tag a Var
155155
m.unknown_parameters[m.y] = bad_value # tag a Var with a bad value
156156
# Attempt to update with a list of mixed types
157-
# This should raise an error because the suffix expects VarData or ParamData
157+
# This should raise an error because this utility only allows numeric values
158+
# in the model to be updated.
158159

159160
with self.assertRaisesRegex(
160161
ValueError, f"could not convert string to float: '{bad_value}'"
161162
):
162-
# Attempt to update with a list of different length
163+
# Attempt to update with a non-numeric value
163164
update_model_from_suffix(m.unknown_parameters, [42, bad_value])
164165

165166
def test_update_model_from_suffix_wrong_component_type(self):

pyomo/contrib/parmest/utils/model_utils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,25 +221,25 @@ def update_model_from_suffix(suffix_obj: pyo.Suffix, values):
221221
222222
Notes
223223
-----
224-
Measurement error is a special case: instead of updating the value of the
224+
The measurement_error suffix is a special case: instead of updating the value of the
225225
keys (variables/parameters), it updates the value stored in the suffix itself.
226226
"""
227227
# Check that the length of values matches the suffix length
228-
items = list(suffix_obj.items())
229-
if len(items) != len(values):
228+
comps = list(suffix_obj.keys())
229+
if len(comps) != len(values):
230230
raise ValueError("values length does not match suffix length")
231231

232232
# Add a check for measurement error suffix
233233
is_me_err = "measurement_error" in suffix_obj.name
234-
# Iterate through the items in the suffix and update their values
234+
# Iterate through the keys in the suffix and update their values
235235
# First loop: check all values are the right type
236-
for comp, _ in items:
236+
for comp in comps:
237237
if not isinstance(comp, (VarData, ParamData)):
238238
raise TypeError(
239239
f"Unsupported component type {type(comp)}; expected VarData or ParamData."
240240
)
241241
# Second loop: adjust the values
242-
for (comp, _), new_val in zip(items, values):
242+
for comp, new_val in zip(comps, values):
243243
if is_me_err:
244244
suffix_obj[comp] = float(new_val)
245245
else:

0 commit comments

Comments
 (0)