Repository navigation
Description
compare() and compare_with() are two separate implementations of "score this field", and neither calls the other:
compare_with()goes throughComparisonDispatcher.dispatch_field_comparison, which builds the full result: counts, thresholds and reports.compare()goes throughStructuredModel.compareandComparisonHelper.compare_field_raw, a hand-written second copy. It exists for speed: it is the cost function for every cell of a Hungarian cost matrix, so it skips building a result.
Every scoring rule therefore has to be written twice, and the copies drift whenever a fix lands in only one. Each time, the two readers disagree about the same pair, and because compare() feeds list matching, a list can pair items on one rule and classify them on the other. That's the contradiction #233 rules out.
Fixes that had to land in both copies so far:
- [BUG]: overall_score and the confusion matrix read different score sources, so identical objects can score 1.0 and FD at once #233: absent values;
- the plain-model class gate (
can_compare_object_pair); - An identical pair of nan float values scores 0.0 instead of 1.0 #367: NaN as absent;
- StructuredModel fields typed date, datetime, time, Decimal, UUID or Enum score 0.0 against an identical value #383 / fix(383): let date, Decimal, UUID, Enum, tuple and set fields reach their comparator #384: scalar types and declared comparators.
Each of those code paths carries a comment saying it must agree with the dispatcher.
Disagreements still open:
- Converge
compare()andcompare_with()on absent-on-both (1.1.0) #357: absent-on-both fields are excluded fromcompare()'s mean and credited at1.0bycompare_with(). A nested model's score infield_scorescomes fromcompare(), so both rules show up in one result. - Tuple and Set fields score 0.0 against an identical value in compare_with(), and compare() disagrees #391: on a
TupleorSetfield with an inferred comparator,compare_with()gives0.0andcompare()calls the comparator.
Proposal. Have one implementation. compare() should return the dispatcher's raw score for the pair, without thresholds or reports, rather than recomputing it. The speed advantage should come from not building reports (skipping the confusion matrix, non-matches and field comparisons), not from a second copy of the rules. Once that's done, a new rule has exactly one place to go, and a test that runs every field shape through both readers and asserts they agree would keep it that way.
What to measure first:
- Cost:
compare()runs once per cell (60x60 objects of 20 fields is 72,000 field calls), so the shared path needs a benchmark against today'scompare()before and after. - Score changes: the unified behaviour has to pick one rule where the two readers differ today. Converge
compare()andcompare_with()on absent-on-both (1.1.0) #357 is the known case, so this changes somecompare()scores and some list pairings, and needs a CHANGELOG entry marked as breaking.
This would subsume #357 and close the reader-disagreement half of #391.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsBacklog
Activity
fix(383): always call a declared comparator, and drop Tuple/Set
release(v1.0.2): scoring, JSON Schema import and report fixes, strict…
sromoam commentedon Nov 24, 2026
Folded in from #357, which is now closed. It is the known case where the unified scorer has to pick one rule.
compare()excludes fields absent on both sides from its mean, andcompare_with()credits them at 1.0. A nested model's score infield_scorescomes fromcompare(), so both rules show up in one result. This is still true ondevat 57f9f3b: in Convergecompare()andcompare_with()on absent-on-both (1.1.0) #357'sDoc/Addrexample,addris 0.0 in the parent'sfield_scoresand 0.6667 on its own.comparison_engine's percolation loop failed 23 tests, intest_simple_list_in_structured_list.pyandtests/auto/test_risk_surface.py. Several of them are the guards that assertcompare_with()credits absent-on-both. Either direction changes reported scores on sparse data, so it is breaking.compare_with()exclude, matchingcompare(), precision, recall and F1, and leaveaccuracyas the metric that counts correct absence. A perfect extraction on a sparse document still scores 1.0; what drops is a wrong extraction on a sparse document. The alternative, makingcompare()credit, is cheaper in tests but makes the Hungarian cost matrix less discriminating.