Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 5 additions & 2 deletions packages/gds-framework/gds/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ def check_value(self, value: Any) -> bool:
if not self.typedef.check_value(value):
return False
if self.bounds is not None:
low, high = self.bounds
if not (low <= value <= high):
try:
low, high = self.bounds
if not (low <= value <= high):
return False
except Exception:
return False
return True

Expand Down
7 changes: 6 additions & 1 deletion packages/gds-framework/gds/types/typedef.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ def check_value(self, value: Any) -> bool:
"""Check if a value satisfies this type definition."""
if not isinstance(value, self.python_type):
return False
return self.constraint is None or self.constraint(value)
if self.constraint is None:
return True
try:
return bool(self.constraint(value))
except Exception:
return False


# ── Built-in types ──────────────────────────────────────────
Expand Down
28 changes: 28 additions & 0 deletions packages/gds-framework/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,34 @@ def test_frozen(self):
with pytest.raises(ValidationError):
t.name = "Other" # type: ignore[misc]

def test_constraint_exception_returns_false(self):
"""Constraint that raises should return False, not propagate."""
t = TypeDef(
name="Bad",
python_type=float,
constraint=lambda x: 1 / 0, # ZeroDivisionError
)
assert t.check_value(1.0) is False

def test_constraint_type_error_returns_false(self):
"""Constraint that raises TypeError should return False."""
t = TypeDef(
name="Bad",
python_type=float,
constraint=lambda x: x > "not a number", # TypeError
)
assert t.check_value(1.0) is False

def test_constraint_returns_truthy_non_bool(self):
"""Constraint returning truthy non-bool value should work."""
t = TypeDef(
name="Truthy",
python_type=str,
constraint=lambda x: x, # non-empty string is truthy
)
assert t.check_value("hello") is True
assert t.check_value("") is False


# ── Built-in types ───────────────────────────────────────────

Expand Down