Repository navigation
fix(462): give every threshold verdict one reason text - #696
Conversation
field_comparisons rows now take their reason from ThresholdHelper.match_reason, so a top-level or nested-model field inside the 1e-10 allowance reads "within threshold tolerance" like a list item does, and the score is shown with repr (0.7999999999999999, not 0.8). Stale descriptions of the old exact >= test are updated in architecture.md, Comparators.md and three comments. Closes awslabs#462 Signed-off-by: Ching Wei Kang <164879897+WilliamK112@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mangeshraut712
left a comment
There was a problem hiding this comment.
Verified the unification and the tolerance text by running the helper across the boundary, then the full suite.
Results on this head (b7d965e)
| Check | Result |
|---|---|
test_threshold_reason.py + test_list_reporting.py |
54 / 54 pass |
Same tests against main's threshold_helper.py |
10 fail (the new assertions) — clean fail → pass |
Full suite (tests/, integration excluded) |
7128 passed, 13 skipped, 1 xfailed, 0 failed |
Call sites routed through match_reason |
2 (comparison_helper_base.py:100, field_comparison_collector.py:164); no inline above/below threshold format strings remain in the touched files; unused math imports dropped |
Boundary sweep (threshold 0.8)
The helper's score < threshold test is exactly consistent with is_above_threshold's allowance (score >= threshold or abs(score - threshold) < 1e-10), so "within tolerance" fires precisely when the match came from the allowance:
| Score | is_above_threshold |
Reason |
|---|---|---|
0.7999999999999999 (0.7 + 0.1) |
match | within threshold tolerance (0.7999999999999999 vs 0.8) |
0.79999999995 (inside 1e-10) |
match | within threshold tolerance (0.79999999995 vs 0.8) |
0.8 |
match | above threshold (0.800 >= 0.8) |
0.7999999998 (outside 1e-10) |
no match | below threshold (0.800 < 0.8) ← see below |
1.0 / 0.999999999999 |
match | exact match |
NumPy float64(0.7) + float64(0.1) renders as a plain float in the text (no np.float64(...)). 8/8 consistency cases pass.
One gap the same fix could close on the "below" branch
The PR fixes the hidden-precision problem for the tolerance branch with repr, but the below threshold branch still formats with .3f. That produces a self-contradicting message for any non-match within < 5e-4 of the threshold:
below threshold (0.800 < 0.8) # score 0.7999999998, 0.7999999, 0.79999, 0.7996 ...
below threshold (0.500 < 0.5)
below threshold (0.123 < 0.123)
I counted 30 such renderings sweeping 5 thresholds × 6 sub-threshold offsets. A user reading 0.800 < 0.8 can't tell whether they missed the threshold by 2e-10 or 4e-4, which is the same confusion the tolerance branch now avoids. The above direction doesn't have this problem (rounding a value at-or-above the threshold can't print below it — 0 cases), so the asymmetry is only on the "below" side. Suggested tweak, mirroring the tolerance branch:
shown = repr(float(score)) if abs(score - threshold) < 5e-4 else f"{score:.3f}"
return f"below threshold ({shown} < {threshold})"(or simply use repr(float(score)) whenever .3f would round across the threshold). Not blocking — the PR does what it says — but since the goal is "the same verdict reads the same everywhere", it'd be nice for the "below" text to never state something numerically false.
Nits
above threshold (0.800 >= 0.8)at an exactly-at score is fine, but note it's the only branch where an exact hit reads "above". Ifexact matchis meant for1.0only (as the docstring implies), that's consistent; just flagging the wording.- CHANGELOG entry references #462; good.
sromoam
left a comment
There was a problem hiding this comment.
Thanks @WilliamK112, this is a tidy fix and the description made it easy to check. On b7d965e I reproduced the mismatch on dev and confirmed it's gone here: top-level, Optional and nested-model fields, List[model], Optional[List[...]] and a list inside a list item all read within threshold tolerance (0.7999999999999999 vs 0.8) now. The new consistency tests fail on dev and pass here, and the full suite passes locally.
Must fix
- The
aboveandbelowtexts can still print an inequality that is false (threshold_helper.py:229-230). Agree with mangeshraut712 on the below branch. The above branch has the same problem when the threshold has more than three decimals:match_reason(True, 0.8004, 0.8004)givesabove threshold (0.800 >= 0.8004). The new table test also pins one of these:below threshold (0.800 < 0.8)for0.8 - 1e-9(test_threshold_reason.py:67). Since #462 asks for a reason that shows the real score, please apply the rulefield_comparison_helper.py:157-160already uses: keep.3funless it rounds across the threshold, then userepr. Suggestions inline.non_match_collector.py:179builds the same below text on its own, so please have it callThresholdHelper.match_reason(False, similarity, info.threshold)too. Otherwise a top-levelnon_matchesentry keeps0.800 < 0.8while a list item's reads0.7996 < 0.8. I tried both changes together: the only test that changes is line 67.
Smaller
test_list_reporting.py:507still passes on dev.repr(0.7 - 1e-12)and its.12gform are both0.699999999999, so the literal can't catch the old format (#462 item 2). Adding the list-of-models shape to the new consistency test covers the same structured-child path: it fails on dev with(0.8 vs 0.8)and passes here. Suggestions inline.- Twins of the comments you rewrote still describe the gate as a bare
>=.comparison_helper.py:561-562(tp = 0 # True positives (score >= threshold)) matches thestructured_list_comparator.py:158comment you rewrote, andis_above_thresholddecides it a few lines below. The Returns blocks atcomparison_helper.py:425-426and:551-552andstructured_model.py:1784-1785say the same. #462's done-when covers comments, so they're worth the same pass. - One more row changes text that the CHANGELOG doesn't mention. With a threshold of
1.0, a top-level or nested field scoring0.9999999999999999readexact matchon dev and now readswithin threshold tolerance (0.9999999999999999 vs 1.0), as list items already did. Suggestion inline, which also covers must-fix 1.
One heads-up: we're not merging anything outside the 1.1.0 milestone until 1.1.0 lands, so this will wait until after that even once it's ready. It also conflicts with dev in CHANGELOG.md now. No need to resolve that yet: bring dev in after 1.1.0 lands.
| if is_match: | ||
| if score < threshold: | ||
| return f"within threshold tolerance ({float(score)!r} vs {threshold})" | ||
| if math.isclose(score, 1.0): | ||
| return "exact match" | ||
| return f"above threshold ({score:.3f} >= {threshold})" | ||
| return f"below threshold ({score:.3f} < {threshold})" |
There was a problem hiding this comment.
Measured with this in place: 0.7996 vs 0.8 reads below threshold (0.7996 < 0.8), 0.8004 vs 0.8004 reads above threshold (0.8004 >= 0.8004), and the usual rows (0.900 >= 0.8, 0.500 < 0.8, exact match, the tolerance text) don't change. The docstring above could say the same in one sentence.
| if is_match: | |
| if score < threshold: | |
| return f"within threshold tolerance ({float(score)!r} vs {threshold})" | |
| if math.isclose(score, 1.0): | |
| return "exact match" | |
| return f"above threshold ({score:.3f} >= {threshold})" | |
| return f"below threshold ({score:.3f} < {threshold})" | |
| if is_match: | |
| if score < threshold: | |
| return f"within threshold tolerance ({float(score)!r} vs {threshold})" | |
| if math.isclose(score, 1.0): | |
| return "exact match" | |
| shown = f"{score:.3f}" | |
| # Never round a score across the threshold it was tested against. | |
| if (float(shown) >= threshold) != is_match: | |
| shown = repr(float(score)) | |
| if is_match: | |
| return f"above threshold ({shown} >= {threshold})" | |
| return f"below threshold ({shown} < {threshold})" |
| (True, 0.9, "above threshold (0.900 >= 0.8)"), | ||
| (True, 0.8, "above threshold (0.800 >= 0.8)"), | ||
| (True, NEAR, TOLERANCE_REASON), | ||
| (False, 0.8 - 1e-9, "below threshold (0.800 < 0.8)"), |
There was a problem hiding this comment.
With the change above, this row shows the real score. The added row is the common case: a score that three decimals round up to the threshold.
| (False, 0.8 - 1e-9, "below threshold (0.800 < 0.8)"), | |
| (False, 0.8 - 1e-9, "below threshold (0.7999999990000001 < 0.8)"), | |
| (False, 0.7996, "below threshold (0.7996 < 0.8)"), |
| class Listed(StructuredModel): | ||
| names: list[str] = ComparableField(comparator=NearComparator(), threshold=0.8) |
There was a problem hiding this comment.
A model for the list-of-models case below.
| class Listed(StructuredModel): | |
| names: list[str] = ComparableField(comparator=NearComparator(), threshold=0.8) | |
| class Listed(StructuredModel): | |
| names: list[str] = ComparableField(comparator=NearComparator(), threshold=0.8) | |
| class Items(StructuredModel): | |
| items: list[Flat] |
| Outer(inner=Flat(name="b")), | ||
| id="nested-model field", | ||
| ), | ||
| pytest.param(Listed(names=["a"]), Listed(names=["b"]), id="list item"), |
There was a problem hiding this comment.
This covers the items[0].name child row, the path test_list_reporting.py:507 was meant to guard. It fails on dev with within threshold tolerance (0.8 vs 0.8) and passes here.
| pytest.param(Listed(names=["a"]), Listed(names=["b"]), id="list item"), | |
| pytest.param(Listed(names=["a"]), Listed(names=["b"]), id="list item"), | |
| pytest.param( | |
| Items(items=[Flat(name="a")]), | |
| Items(items=[Flat(name="b")]), | |
| id="list-of-models field", | |
| ), |
| - A `field_comparisons` row that meets its threshold only through the `1e-10` | ||
| allowance now reads `within threshold tolerance (<score> vs <threshold>)` for a | ||
| top-level or nested-model field too, where it read `above threshold`, and the | ||
| score is shown in full (`0.7999999999999999`, not `0.8`). Every row with the | ||
| same verdict gets its reason from `ThresholdHelper.match_reason` | ||
| ([#462](https://github.com/awslabs/stickler/issues/462)). |
There was a problem hiding this comment.
Names the threshold-1.0 case, and the rounding rule from must-fix 1:
| - A `field_comparisons` row that meets its threshold only through the `1e-10` | |
| allowance now reads `within threshold tolerance (<score> vs <threshold>)` for a | |
| top-level or nested-model field too, where it read `above threshold`, and the | |
| score is shown in full (`0.7999999999999999`, not `0.8`). Every row with the | |
| same verdict gets its reason from `ThresholdHelper.match_reason` | |
| ([#462](https://github.com/awslabs/stickler/issues/462)). | |
| - A `field_comparisons` row that meets its threshold only through the `1e-10` | |
| allowance now reads `within threshold tolerance (<score> vs <threshold>)` for a | |
| top-level or nested-model field too, where it read `above threshold` (or | |
| `exact match` at a threshold of `1.0`), and the score is shown in full | |
| (`0.7999999999999999`, not `0.8`). An `above threshold` or `below threshold` | |
| reason also shows the full score where three decimals would round it across | |
| the threshold (`0.7996 < 0.8`, not `0.800 < 0.8`). Every row with the same | |
| verdict gets its reason from `ThresholdHelper.match_reason` | |
| ([#462](https://github.com/awslabs/stickler/issues/462)). |
Keep three decimals unless they would round the score across the threshold it was tested against, then show it with repr (the rule field_comparison_helper already uses). Top-level non_matches reasons now come from ThresholdHelper.match_reason too. Adds the list-of-models case to the consistency test, rewrites the remaining bare ">=" gate comments, and names the threshold-1.0 case in the CHANGELOG. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks @sromoam, and thanks @mangeshraut712 for the boundary sweep. Pushed 47c8ead:
The full suite has the same 9 Windows-only failures as |
Signed-off-by: Ching-Wei Kang <ckang53@wisc.edu>
|
I merged current |
sromoam
left a comment
There was a problem hiding this comment.
Thanks @WilliamK112, this is ready. I checked 3db297f merged with current dev (9680269d). The bullet sits under ## [Unreleased], ### Fixed, and 1.1.0 is untouched. Everything from the last round is in:
match_reasonkeeps.3funless that would round the score across the threshold (0.7996 < 0.8,0.8004 >= 0.8004).- Top-level
non_matchesreasons come from it too. - The list-of-models case is in the consistency test.
- The remaining
>=comments and Returns blocks are rewritten.
The new tests fail on dev (13 failed) and pass here. The full suite passes merged (7492 passed, 3 skipped, 1 xfailed), and ruff and the docs build are clean.
Smaller
- Nothing pins the
non_matcheschange. Withnon_match_collector.pyput back todev's version, the new tests and the 1648 non-match, threshold and report tests all still pass. One assertion would cover it: a top-level field scoring0.7996against0.8should givedetails["reason"] == "below threshold (0.7996 < 0.8)", wheredevgives0.800 < 0.8. - #693 edits the same lines in
field_comparison_collector.py, so whichever lands second will hit one conflict there. Keep #693'snot_comparablebranch and callmatch_reasonin itselse. I tried that resolution, and both PRs' tests pass.
Security Scan ResultsASH Security Scan Report
Scan Metadata
SummaryScanner ResultsThe table below shows findings by scanner, with status based on severity thresholds and dependencies:
Report generated by Automated Security Helper (ASH) at 2026-12-04T22:25:23+00:00 |
Signed-off-by: Ching Wei Kang <164879897+WilliamK112@users.noreply.github.com>
…ries Signed-off-by: Ching Wei Kang <164879897+WilliamK112@users.noreply.github.com>
Issue #, if available: Fixes #462
Description of changes:
Every
field_comparisonsrow now gets its reason from one helper, so the same verdict reads the same everywhere and a score inside the1e-10allowance shows its real value.threshold_helper.py: newThresholdHelper.match_reason(is_match, score, threshold). A score that meets the threshold only through the allowance readswithin threshold tolerance (<score> vs <threshold>), with the score inrepr(0.7999999999999999, not0.8; a NumPy float reads as a plain float). Otherwise it givesexact match,above threshold (...)orbelow threshold (...), with the score to three decimals unless that would round it across the threshold, in which case it is shown withreprtoo (0.7996 < 0.8, not0.800 < 0.8).comparison_helper_base.py(list items) andfield_comparison_collector.py(top-level and nested-model fields) both call it. Before, the collector saidabove threshold (0.800 >= 0.8)for the same score that list items calledwithin threshold tolerance (0.8 vs 0.8). Their unusedmathimports are dropped.non_match_collector.pyuses it for top-levelnon_matchesreasons as well.tests/test_list_reporting.pybuilt its expected string with the code's own.12gformat. It now asserts the literal text.>=test: the recursion-gate snippet inarchitecture.mdnow shows the currentzip(matched_pairs, verdicts)filter and names the methods instead of a line range; thebinary_comparecomment inComparators.md; and the comments instructured_list_comparator.pyandmetrics_helper.py.tests/structured_object_evaluator/test_threshold_reason.py: a0.7 + 0.1score against0.8on a top-level field, a nested-model field and a list item all give the same reason, plus a table test formatch_reason.CHANGELOG.md: a### Fixedbullet under[Unreleased].Not done here: item 4 (one named constant for
1e-10, and moving the check tostickler/utils) is optional in the issue, so I left it for a separate change. #693 (for #509) edits the same lines infield_comparison_collector.pyand says it leaves #462 to a separate PR, so whichever lands second needs a small rebase. I'm happy to do it.Checks, on
devat 33ed6c3:dev's code (two readabove threshold (0.800 >= 0.8), the list item reads(0.8 vs 0.8)) and pass here.uv run pytest -q: 7,118 passed, 14 skipped, 1 xfailed. The 9 failures are the same as ondevon this Windows machine (deep-nesting timeouts and one gallery path test).ruff check src testsis clean. The new test file passesruff format --check; the two edited modules already had format differences ondev, and I didn't reformat them.docs/check_links.pypasses.Written with Claude Code.
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
🤖 Generated with Claude Code