Skip to content

Commit 694c26f

Browse files
committed
Don't silently cast non-numeric types when evaluating numeric bounds.
1 parent 54b3545 commit 694c26f

2 files changed

Lines changed: 18 additions & 9 deletions

File tree

python/lib/sift_client/_tests/util/test_test_results_utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,12 @@ def test_evaluate_measurement_bounds_with_numeric_bounds_object(self):
536536
assert measurement.numeric_bounds.min == 0.0
537537
assert measurement.numeric_bounds.max == 10.0
538538

539+
def test_numeric_bounds_bad_value_type(self):
540+
"""Test that an error is raised if the value is not a float or int when evaluating numeric bounds."""
541+
measurement = TestMeasurementUpdate()
542+
with pytest.raises(TypeError, match="Value must be a float or int to evaluate numeric bounds but gave <class 'str'>"):
543+
evaluate_measurement_bounds(measurement, "1", {"min": 0.0, "max": 10.0})
544+
539545
def test_evaluate_measurement_bounds_string_matching(self):
540546
"""Test string value matching expected string."""
541547
measurement = TestMeasurementUpdate()

python/lib/sift_client/util/test_results/bounds.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,16 @@ def evaluate_measurement_bounds(
7070
elif isinstance(bounds, NumericBounds):
7171
measurement.numeric_bounds = bounds
7272
measurement.passed = True
73-
float_value = float(value)
74-
if measurement.numeric_bounds.min is not None:
75-
measurement.passed = (
76-
measurement.passed and measurement.numeric_bounds.min <= float_value
77-
)
78-
if measurement.numeric_bounds.max is not None:
79-
measurement.passed = (
80-
measurement.passed and measurement.numeric_bounds.max >= float_value
81-
)
73+
try:
74+
if measurement.numeric_bounds.min is not None:
75+
measurement.passed = (
76+
measurement.passed and measurement.numeric_bounds.min <= value
77+
)
78+
if measurement.numeric_bounds.max is not None:
79+
measurement.passed = (
80+
measurement.passed and measurement.numeric_bounds.max >= value
81+
)
82+
except TypeError:
83+
raise TypeError(f"Value must be a float or int to evaluate numeric bounds but gave {type(value)}") from None
84+
8285
return bool(measurement.passed)

0 commit comments

Comments
 (0)