Skip to content

Commit 093d5c3

Browse files
authored
Don't silently cast non-numeric types when evaluating numeric bounds. (#520)
1 parent 54b3545 commit 093d5c3

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,15 @@ 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(
543+
TypeError,
544+
match="Value must be a float or int to evaluate numeric bounds but gave <class 'str'>",
545+
):
546+
evaluate_measurement_bounds(measurement, "1", {"min": 0.0, "max": 10.0})
547+
539548
def test_evaluate_measurement_bounds_string_matching(self):
540549
"""Test string value matching expected string."""
541550
measurement = TestMeasurementUpdate()

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,14 @@ 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 = measurement.passed and measurement.numeric_bounds.min <= value # type: ignore
76+
if measurement.numeric_bounds.max is not None:
77+
measurement.passed = measurement.passed and measurement.numeric_bounds.max >= value # type: ignore
78+
except TypeError:
79+
raise TypeError(
80+
f"Value must be a float or int to evaluate numeric bounds but gave {type(value)}"
81+
) from None
82+
8283
return bool(measurement.passed)

0 commit comments

Comments
 (0)