Skip to content

Commit

Permalink
Deprecation warning for scalar sets with filter/validate callbacks ex…
Browse files Browse the repository at this point in the history
…pecting expanded values
  • Loading branch information
jsiirola committed Aug 13, 2024
1 parent df7f2b2 commit 2d47311
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
22 changes: 1 addition & 21 deletions pyomo/core/base/set.py
Original file line number Diff line number Diff line change
Expand Up @@ -1488,7 +1488,7 @@ def _cb_validate_filter(self, mode, val_iter):
f"{self.__class__.__name__} {self.name}: '{mode}=' "
"callback signature matched (block, *value). "
"Please update the callback to match the signature "
"(block, value, *index).",
f"(block, value{', *index' if comp.is_indexed() else ''}).",
version='6.7.4.dev0',
)
orig_fcn = fcn._fcn
Expand Down Expand Up @@ -2264,26 +2264,6 @@ def __init__(self, *args, **kwds):
if self._init_dimen.constant():
self._dimen = self._init_dimen(self.parent_block(), None)

if self._validate.__class__ is ParameterizedIndexedCallInitializer:
# TBD [JDS: 8/2024]: should we deprecate the "expanded
# tuple" version of the validate callback for scalar sets?
# It is widely used and we can (reasonably reliably) map to
# the expected behavior...
orig_fcn = self._validate._fcn
self._validate = ParameterizedScalarCallInitializer(
lambda m, v: orig_fcn(m, *v), True
)

if self._filter.__class__ is ParameterizedIndexedCallInitializer:
# TBD [JDS: 8/2024]: should we deprecate the "expanded
# tuple" version of the filter callback for scalar sets?
# It is widely used and we can (reasonably reliably) map to
# the expected behavior...
orig_fcn = self._filter._fcn
self._filter = ParameterizedScalarCallInitializer(
lambda m, v: orig_fcn(m, *v), True
)

@deprecated(
"check_values() is deprecated: Sets only contain valid members", version='5.7'
)
Expand Down
47 changes: 40 additions & 7 deletions pyomo/core/tests/unit/test_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -4336,30 +4336,63 @@ def _lt_3(model, i):

m = ConcreteModel()

def _validate(model, i, j):
def _validate(model, val):
i, j = val
self.assertIs(model, m)
if i + j < 2:
return True
if i - j > 2:
return False
raise RuntimeError("Bogus value")

m.I = Set(validate=_validate)
m.I1 = Set(validate=_validate)
output = StringIO()
with LoggingIntercept(output, 'pyomo.core'):
self.assertTrue(m.I.add((0, 1)))
self.assertTrue(m.I1.add((0, 1)))
self.assertEqual(output.getvalue(), "")
with self.assertRaisesRegex(
ValueError,
r"The value=\(4, 1\) violates the validation rule of " r"Set I",
r"The value=\(4, 1\) violates the validation rule of " r"Set I1",
):
m.I.add((4, 1))
m.I1.add((4, 1))
self.assertEqual(output.getvalue(), "")
with self.assertRaisesRegex(RuntimeError, "Bogus value"):
m.I.add((2, 2))
m.I1.add((2, 2))
self.assertEqual(
output.getvalue(),
"Exception raised while validating element '(2, 2)' for Set I1\n",
)

def _validate(model, i, j):
self.assertIs(model, m)
if i + j < 2:
return True
if i - j > 2:
return False
raise RuntimeError("Bogus value")

m.I2 = Set(validate=_validate)
with LoggingIntercept(module='pyomo.core') as output:
self.assertTrue(m.I2.add((0, 1)))
self.assertRegex(
output.getvalue().replace('\n', ' '),
r"DEPRECATED: OrderedScalarSet I2: 'validate=' callback "
r"signature matched \(block, \*value\). Please update the "
r"callback to match the signature \(block, value\)",
)
with LoggingIntercept(module='pyomo.core') as output:
with self.assertRaisesRegex(
ValueError,
r"The value=\(4, 1\) violates the validation rule of " r"Set I2",
):
m.I2.add((4, 1))
self.assertEqual(output.getvalue(), "")
with LoggingIntercept(module='pyomo.core') as output:
with self.assertRaisesRegex(RuntimeError, "Bogus value"):
m.I2.add((2, 2))
self.assertEqual(
output.getvalue(),
"Exception raised while validating element '(2, 2)' for Set I\n",
"Exception raised while validating element '(2, 2)' for Set I2\n",
)

m.J1 = Set([(0, 0), (2, 2)], validate=_validate)
Expand Down

0 comments on commit 2d47311

Please sign in to comment.