Skip to content
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

Type check all exception arguments in component __call__ implementations #3538

Merged
merged 27 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d0ff0f1
Param raises a ValueError when 'exception' arg is not a bool
emma58 Mar 24, 2025
a6b1cbb
black
emma58 Mar 24, 2025
aa38102
more black
emma58 Mar 24, 2025
3216fea
Moving the implementation of the type checker for the exception arg t…
emma58 Mar 25, 2025
c47b8dd
Type-checking exception arg in Expression
emma58 Mar 25, 2025
1efee8d
Type checking exception arg for Objective
emma58 Mar 25, 2025
bb3a1c8
Type checking exception arg for units
emma58 Mar 25, 2025
3225949
Type checking exception arg for Var
emma58 Mar 25, 2025
4ec511e
black
emma58 Mar 25, 2025
4f54c06
Type checking exception arg in BooleanVar
emma58 Mar 25, 2025
b211822
Type checking exception arg in Constraint
emma58 Mar 25, 2025
3ee7810
Type checking exception arg for LogicalConstraint
emma58 Mar 25, 2025
472ccfb
Type checking exception arg for matrix constraint
emma58 Mar 25, 2025
707fe6a
Moving the exception arg type check implementation to expr_common for…
emma58 Mar 25, 2025
4999f5f
Type checking exception arg in boolean value
emma58 Mar 25, 2025
4dad55e
Type checking exception arg in numvalue
emma58 Mar 25, 2025
9c2a87c
Template expr exception flag will be checked by ExpressionBase __call…
emma58 Mar 25, 2025
f572ba3
black
emma58 Mar 25, 2025
5698c14
Type checking exception arg in kernel/expression
emma58 Mar 25, 2025
c10a3d7
Type checking exception in kernel matrix constraint
emma58 Mar 25, 2025
ae6ea8c
Type checking exception arg in conic
emma58 Mar 25, 2025
264c714
Type checking exception arg in kernel parameter
emma58 Mar 25, 2025
defb293
type checking exception arg for kernel variable
emma58 Mar 25, 2025
3588dc1
type checking exception arg for kernel constraint
emma58 Mar 25, 2025
12c81fa
black
emma58 Mar 25, 2025
9fa799a
Type checking exception arg in beta/matrix
emma58 Mar 25, 2025
70cc92f
Merge branch 'main' into params-complain-more
jsiirola Mar 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions pyomo/core/base/param.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,21 @@ def set_value(self, value, idx=NOTSET):
self._value = old_value
raise

def __call__(self, exception=True):
def __call__(self, exception=NOTSET):
"""
Return the value of this object.
"""
if exception is NOTSET:
raise_exception = True
elif type(exception) is not bool:
raise ValueError(
"Param '%s' was called with a non-boolean argument for "
"'exception': %s" % (self.name, exception)
)
else:
raise_exception = exception
if self._value is Param.NoValue:
if exception:
if raise_exception:
raise ValueError(
"Error evaluating Param value (%s):\n\tThe Param value is "
"currently set to an invalid value. This is\n\ttypically "
Expand Down Expand Up @@ -929,10 +938,20 @@ def __init__(self, *args, **kwds):
# up both the Component and Data base classes.
#

def __call__(self, exception=True):
def __call__(self, exception=NOTSET):
"""
Return the value of this parameter.
"""
if exception is NOTSET:
raise_exception = True
elif type(exception) is not bool:
raise ValueError(
"Param '%s' was called with a non-boolean argument for "
"'exception': %s" % (self.name, exception)
)
else:
raise_exception = exception

if self._constructed:
if not self._data:
if self._mutable:
Expand All @@ -943,8 +962,8 @@ def __call__(self, exception=True):
# Immutable Param defaults never get added to the
# _data dict
return self[None]
return super(ScalarParam, self).__call__(exception=exception)
if exception:
return super(ScalarParam, self).__call__(exception=raise_exception)
if raise_exception:
raise ValueError(
"Evaluating the numeric value of parameter '%s' before\n\t"
"the Param has been constructed (there is currently no "
Expand Down Expand Up @@ -976,9 +995,19 @@ class SimpleParam(metaclass=RenamedClass):


class IndexedParam(Param):
def __call__(self, exception=True):
def __call__(self, exception=NOTSET):
"""Compute the value of the parameter"""
if exception:
if exception is NOTSET:
raise_exception = True
elif type(exception) is not bool:
raise ValueError(
"IndexedParam '%s' was called with a non-boolean argument for "
"'exception': %s" % (self.name, exception)
)
else:
raise_exception = exception

if raise_exception:
raise TypeError(
'Cannot compute the value of an indexed Param (%s)' % (self.name,)
)
Expand Down
25 changes: 25 additions & 0 deletions pyomo/core/tests/unit/test_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,31 @@ def test_nonfinite_pprint(self):
" bb : 4\n",
)

def test_invalid_exception_argument(self):
m = ConcreteModel()
m.p = Param(initialize=7, mutable=True)
m.indexed = Param([1, 2], initialize={1: 3, 2: 4}, mutable=True)
with self.assertRaisesRegex(
ValueError,
r"Param 'p' was called with a non-boolean argument for 'exception': "
r"p \+ 2",
):
m.p(m.p + 2)

with self.assertRaisesRegex(
ValueError,
r"Param 'indexed\[1\]' was called with a non-boolean argument for "
r"'exception': 3.2",
):
m.indexed[1](3.2)

with self.assertRaisesRegex(
ValueError,
r"IndexedParam 'indexed' was called with a non-boolean argument for "
r"'exception': hi",
):
m.indexed('hi')


def createNonIndexedParamMethod(func, init_xy, new_xy, tol=1e-10):
def testMethod(self):
Expand Down
Loading