From acd5340ab0df387567f8d736df499be60e5410b4 Mon Sep 17 00:00:00 2001 From: Max Ghenis Date: Thu, 5 Nov 2026 14:48:49 -0500 Subject: [PATCH 1/2] Refuse malformed printed amounts and invented conditions in the AB2026 ingest We answer L2 and L3 of the cloud review of #247 at a28223f2. The ingest's printed-amount pattern now refuses a comma after a lone zero, a leading zero and a lead group of four digits. The preservation test now refuses a claim condition that neither the staged row nor the ingest supplies. No staged row prints those forms, and no claim moves. Co-Authored-By: Claude Opus 5.5 --- scorecard_db/ingest_uk_ab2026.py | 4 +- tests/test_uk_ab2026_ingest.py | 74 ++++++++++++++++++++++++++++++-- 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/scorecard_db/ingest_uk_ab2026.py b/scorecard_db/ingest_uk_ab2026.py index 03c062212..6c13db652 100644 --- a/scorecard_db/ingest_uk_ab2026.py +++ b/scorecard_db/ingest_uk_ab2026.py @@ -682,7 +682,9 @@ def _drop(acct: dict, fam: dict, reason_key: str) -> None: fam["drops"][reason_key] = fam["drops"].get(reason_key, 0) + 1 -_PRINTED_AMOUNT = re.compile(r"£(\d+|\d{1,3}(?:,\d{3})+)(?:\.(\d+))? (million|billion)") +_PRINTED_AMOUNT = re.compile( + r"£(0|[1-9]\d*|[1-9]\d{0,2}(?:,\d{3})+)(?:\.(\d+))? (million|billion)" +) _MULTIPLIERS = {"million": 1e6, "billion": 1e9} diff --git a/tests/test_uk_ab2026_ingest.py b/tests/test_uk_ab2026_ingest.py index a077f5335..415c90916 100644 --- a/tests/test_uk_ab2026_ingest.py +++ b/tests/test_uk_ab2026_ingest.py @@ -714,12 +714,32 @@ def _staged_unit(row): STORED_UNITS = {("uk_ifs_ab2026", "slide 4", "£80"): "gbp_per_household"} +# The conditions `_score`, `_verdict_conditions` and `with_baseline_condition` +# write beside the staged ones. +INGEST_CONDITIONS = { + "action_link", + "baseline_policy", + "benchmark_class", + "counterfactual", + "country", + "estimate_kind", + "measure_key", + "pe_expressibility", + "pe_missing", + "reform_hint", + "scoring_method", + "staged_metric", + "value_verbatim", +} + + def _keeps_conditions(row, score): """Require each staged condition on the claim as staged. For a condition the ingest canonicalises, require the registered name and the staged text beside any name that differs. For a program we haven't registered, require the staged - text alone.""" - for key, value in (row.get("conditions") or {}).items(): + text alone. Refuse any condition neither the row nor the ingest supplies.""" + staged = row.get("conditions") or {} + for key, value in staged.items(): stored = score.conditions.get(key) verbatim = score.conditions.get(f"{key}_verbatim") if key == "program" and value not in known(row["source"], key): @@ -731,7 +751,12 @@ def _keeps_conditions(row, score): kept = stored == value if not kept: return False - return True + verbatim = { + f"{key}_verbatim" + for key in staged + if key == "program" or key in I.AB25._CANONICALISED + } + return set(score.conditions) <= set(staged) | verbatim | INGEST_CONDITIONS _CHILDCARE = "free childcare entitlement (claimed and received some)" @@ -776,9 +801,21 @@ def _keeps_conditions(row, score): {"source": "obr", "conditions": {"geography": "NI"}}, {"geography": "Northern Ireland"}, ), + ( + {"source": "centax", "conditions": {"basis": "forecast"}}, + {"basis": "forecast", "housing_costs": "AHC"}, + ), + ( + {"source": "centax", "conditions": {"basis": "forecast"}}, + {"basis": "forecast", "basis_verbatim": "forecast"}, + ), + ( + {"source": "centax", "conditions": {"basis": "forecast"}}, + {"basis": "forecast", "geography_verbatim": "England"}, + ), ], ) -def test_preservation_rejects_a_lost_condition(row, stored): +def test_preservation_rejects_a_lost_or_invented_condition(row, stored): assert not _keeps_conditions(row, SimpleNamespace(conditions=stored)) @@ -808,6 +845,10 @@ def test_preservation_rejects_a_lost_condition(row, stored): {"source": "obr", "conditions": {"geography": "NI"}}, {"geography": "Northern Ireland", "geography_verbatim": "NI"}, ), + ( + {"source": "centax", "conditions": {"basis": "forecast"}}, + {"basis": "forecast", "country": "UK", "value_verbatim": "£1"}, + ), ], ) def test_preservation_accepts_what_the_ingest_keeps(row, stored): @@ -1039,6 +1080,9 @@ def test_a_ukraine_row_keeps_its_table_cell_for_the_year_exception(monkeypatch): "£26,00 million", "£2600, million", "£1,2345 million", + "£0,123 million", + "£01 million", + "£1234,567 million", ], ) def test_a_reviewed_twin_printed_in_another_form_raises(raw): @@ -1048,6 +1092,28 @@ def test_a_reviewed_twin_printed_in_another_form_raises(raw): I._check_row_calls("uk_hmt_redbook", rows) +@given(st.integers(0, 10**12), st.sampled_from(["million", "billion"])) +def test_a_printed_amount_reads_with_or_without_thousands_commas(number, unit): + for digits in (str(number), f"{number:,}"): + printed = I._PRINTED_AMOUNT.fullmatch(f"£{digits} {unit}") + assert printed and int(printed[1].replace(",", "")) == number + + +@given(st.integers(1, 10**12), st.integers(0, 13)) +def test_a_printed_amount_takes_commas_only_at_thousands(number, at): + digits = str(number) + comma = f"{digits[:at]},{digits[at:]}" + assert (I._PRINTED_AMOUNT.fullmatch(f"£{comma} million") is not None) == ( + comma == f"{number:,}" + ) + + +@given(st.integers(1, 10**12)) +def test_a_printed_amount_takes_no_leading_zero(number): + assert I._PRINTED_AMOUNT.fullmatch(f"£0{number} million") is None + assert I._PRINTED_AMOUNT.fullmatch(f"£0{number:,} million") is None + + def test_a_reviewed_twin_whose_value_leaves_its_print_raises(): """We cannot widen the ingest's tolerance by storing a value that differs from the printed amount.""" From 9123e3244289c8a47978ef9eb3400563aeb2307e Mon Sep 17 00:00:00 2001 From: Max Ghenis Date: Thu, 5 Nov 2026 16:20:45 -0500 Subject: [PATCH 2/2] Ingest guards: test the static scoring method the ingest adds We staged no row under exchequer_impact_static, so the ingest wrote its static scoring_method on no claim, and a mutant that dropped it from the test's list of added conditions survived. We now set that metric on an HM Treasury costings row and require the ingest to add a static scoring method that the preservation helper accepts. Co-Authored-By: Claude Opus 5.5 --- tests/test_uk_ab2026_ingest.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test_uk_ab2026_ingest.py b/tests/test_uk_ab2026_ingest.py index 415c90916..a57241167 100644 --- a/tests/test_uk_ab2026_ingest.py +++ b/tests/test_uk_ab2026_ingest.py @@ -877,6 +877,22 @@ def canonical(source, axis, value): assert score.conditions.get("program_verbatim") == verbatim +def test_a_static_exchequer_row_gains_its_scoring_method(): + """We staged no row under `exchequer_impact_static`, so we set it on an + HM Treasury costings row with no scoring method.""" + row = copy.deepcopy( + next( + r + for r in RAW["uk_hmt_costings"] + if "scoring_method" not in (r.get("conditions") or {}) + ) + ) + row["proposed_metric"] = "exchequer_impact_static" + score = I._score(row, "uk_hmt_costings", row["source"], Metric.REVENUE_CHANGE) + assert score.conditions["scoring_method"] == "static" + assert _keeps_conditions(row, score) + + def test_every_claim_preserves_its_staged_row(staged): """Read each family's claims file directly, find the row behind each claim by its printed place and printed value, and require the claim to