Skip to content

Commit 449b7c1

Browse files
Transurgeonclaude
andauthored
Fix NaN validation to reject NaN for Parameters while allowing for Variables (#127)
Parameters should raise ValueError when assigned NaN values, but Variables need to allow NaN for NLP structural Jacobian/Hessian computation. The previous code allowed NaN for all Leaf types, breaking upstream test test_nan_in_parameter_raises. The fix checks self.variables() to distinguish Variables (returns [self]) from Parameters (returns []). Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 5382605 commit 449b7c1

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

cvxpy/expressions/leaf.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,9 +601,14 @@ def _validate_value(self, val, sparse_path=False):
601601
attr_str = 'in bounds'
602602
else:
603603
attr_str = ([k for (k, v) in self.attributes.items() if v] + ['real'])[0]
604-
if np.isnan(val).any():
604+
if np.isnan(val).any() and self.variables():
605605
# necessary for NLP package extension and computing the structural jacobian
606+
# Only allow NaN for Variables, not Parameters
606607
return val
608+
elif np.isnan(val).any():
609+
raise ValueError(
610+
"%s value must be real." % self.__class__.__name__
611+
)
607612
else:
608613
raise ValueError(
609614
"%s value must be %s." % (self.__class__.__name__, attr_str)

0 commit comments

Comments
 (0)