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
5 changes: 4 additions & 1 deletion laya/confidence.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ def jev_confidence(answer: Dict[str, Any]) -> Optional[float]:
p = [float(probabilities[key]) for key in sorted(probabilities, key=int)]
else:
p = [float(value) for value in probabilities.values()]
except (TypeError, ValueError):
except (TypeError, ValueError, OverflowError):
# `OverflowError` is the same "does not read as a number": a JSON integer literal too wide
# for a double (a 400-digit probability) overflows `float()`, and an infinite score level
# overflows `int()`. `calibrate.py:780` already names it for the same reason.
return None
if (any(isinstance(v, bool) for v in probabilities.values())
or not all(math.isfinite(v) and v >= 0.0 for v in p)):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_confidence.py
Original file line number Diff line number Diff line change
Expand Up @@ -2476,6 +2476,14 @@ def _score(*p):
("all-negative-probabilities", _choice(-0.9, -0.1)),
("negative-total-near-zero", _choice(-0.61, 0.6)),
("negative-score-level", _score(1.5, -0.5, 0.0)),
# `float()` and `int()` do not only refuse a value that is not a number, they also refuse one
# that is too wide to become one: JSON's grammar has no double limit, so a 400-digit integer
# probability arrives as a Python `int` and `float()` raises `OverflowError` out of a function
# whose docstring promises no number rather than a made-up one.
("overflowing-integer-probability", _choice(10 ** 400, 1)),
("json-integer-too-wide-for-a-double",
_json.loads('{"type":"choice","probabilities":{"a":' + "9" * 400 + ',"b":0.1}}')),
("overflowing-score-level", {"type": "score", "probabilities": {float("inf"): 0.5, 2: 0.5}}),
):
check("jev_confidence/none-for-%s" % _name, jev_confidence(_answer), None)

Expand Down
Loading