For example, a CI gate that requires a score of at least 80 can accept NaN when it only rejects scores below 80. The rejection condition evaluates to False, so the script takes the passing path.
Reproduce the bypass without a pipeline
This dependency-free example isolates the faulty decision:
THRESHOLD = 80.0
def buggy_accepts(score: float) -> bool:
if score < THRESHOLD:
return False
return True
score = float("nan")
print(score < THRESHOLD)
print(buggy_accepts(score))
Expected output:
False
True
Python's float() permits NaN and both infinities because they are valid floating-point values. Successful conversion alone does not make them usable scores.
NaN is unordered: both nan < 80 and nan >= 80 evaluate to False. The function accepts it because it treats “not below the threshold” as sufficient evidence to pass.
Changing the implementation to score >= THRESHOLD rejects NaN, but still accepts positive infinity. If the metric's input contract requires a finite score, express that requirement directly:
import math
def accepts(score: float) -> bool:
return math.isfinite(score) and score >= THRESHOLD
This rejects NaN, positive infinity, and negative infinity while preserving acceptance at the threshold.
Keep parsing, validity, and policy separate
A useful gate answers three distinct questions:
| Boundary | Question | Example |
|---|---|---|
| Parsing | Can the report field become a float? | "missing" fails conversion. |
| Validity | Is the resulting value finite? | "nan" converts but fails validation. |
| Policy | Does the valid score meet the threshold? | "79" is finite but fails the threshold. |
These boundaries distinguish a malformed field, a non-finite value, and a normal below-threshold rejection.
For this example, the contract accepts report fields that Python's float() can parse, provided the result is finite and at least 80. It does not impose an upper bound. If your metric has a documented range, such as 0 through 100, enforce that range separately.
Finiteness does not impose an upper bound.
Save this boundary table as a regression check
The six-case boundary table covers both sides of the threshold, equality, and all three non-finite categories:
| Report field | Expected decision | Reason |
|---|---|---|
"79" |
Reject | Below threshold |
"80" |
Accept | Exactly at threshold |
"81" |
Accept | Above threshold |
"nan" |
Reject | Not finite |
"inf" |
Reject | Not finite |
"-inf" |
Reject | Not finite |
Save the following as test_metric_gate.py. It uses only Python's standard library and keeps conversion separate from the numeric decision.
import math
import unittest
THRESHOLD = 80.0
def parse_metric(raw: str) -> float:
return float(raw)
def meets_threshold(score: float) -> bool:
return math.isfinite(score) and score >= THRESHOLD
def metric_passes(raw: str) -> bool:
try:
score = parse_metric(raw)
except ValueError:
return False
return meets_threshold(score)
class MetricGateTests(unittest.TestCase):
def test_decision_table(self):
cases = [
("79", False),
("80", True),
("81", True),
("nan", False),
("inf", False),
("-inf", False),
]
for raw, expected in cases:
with self.subTest(raw=raw):
self.assertEqual(metric_passes(raw), expected)
def test_parsing_preserves_nan_for_validation(self):
self.assertTrue(math.isnan(parse_metric("nan")))
def test_malformed_fields_fail_closed(self):
for raw in ("", "missing"):
with self.subTest(raw=raw):
with self.assertRaises(ValueError):
parse_metric(raw)
self.assertFalse(metric_passes(raw))
if __name__ == "__main__":
unittest.main()
When adapting this suite, remove the reference function definitions and import your production predicate and parsing functions instead. Adjust the calls to match their interfaces. Testing a copied implementation does not protect your existing gate from regression.
Run it with:
python test_metric_gate.py
The test runner exits unsuccessfully if an assertion fails. The expected result for this implementation is a passing suite.
For a quick check that the tests detect the original bug, temporarily replace the body of meets_threshold with:
return not (score < THRESHOLD)
The NaN and positive-infinity cases should fail. Restore the finite-value check afterward. This exercise tests the regression check itself rather than merely inspecting code that looks correct.
Test the gate's wiring separately
The suite verifies the decision function. It does not check whether rejection reaches the process exit status or whether the pipeline reads the intended report field.
A correct predicate can still be wired into a passing job incorrectly.
At the command boundary, False must produce a nonzero exit status. For a parsed report field named raw_score, the essential connection is:
raise SystemExit(0 if metric_passes(raw_score) else 1)
To exercise that connection, save this reference command as metric_gate.py beside test_metric_gate.py. It takes the raw score as a command-line argument and reuses the reference implementation above:
import sys
from test_metric_gate import metric_passes
if __name__ == "__main__":
raw_score = sys.argv[1]
raise SystemExit(0 if metric_passes(raw_score) else 1)
Save this subprocess regression test as test_metric_gate_command.py in the same directory:
from pathlib import Path
import subprocess
import sys
import unittest
GATE = Path(__file__).resolve().with_name("metric_gate.py")
class MetricGateCommandTests(unittest.TestCase):
def test_exit_status(self):
cases = [
("80", True),
("79", False),
("nan", False),
]
for raw, should_pass in cases:
with self.subTest(raw=raw):
result = subprocess.run(
[sys.executable, str(GATE), raw],
capture_output=True,
text=True,
check=False,
timeout=5,
)
if should_pass:
self.assertEqual(result.returncode, 0, result.stderr)
else:
self.assertNotEqual(result.returncode, 0, result.stderr)
if __name__ == "__main__":
unittest.main()
Run both suites:
python -m unittest test_metric_gate test_metric_gate_command
The subprocess test checks zero exit status for 80 and nonzero status for 79 and NaN. It tests the reference command, not your pipeline's report reader. When adapting it, invoke your production command with isolated fixture reports containing those values, rather than live evaluation data. Replace the reference import with your production implementation as well.
I would start with these small executable checks before reproducing a full pipeline failure. They isolate comparison semantics and command behavior without network access or external dependencies. The fixture-report check then covers the report-reading path; the pipeline still needs to propagate the command's failure into a failed job.
These checks validate enforcement, not the metric's scientific meaning, report provenance, or usefulness as a release criterion.
Today, run the saved cases against your production predicate and command boundary. Keep the fixtures as regression tests, with that limit in mind.