From e2817442091a084db00e4eeead374cf5bd9448ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Juaristi?= <127882282+juaristi22@users.noreply.github.com> Date: Tue, 1 Dec 2026 18:58:51 +0000 Subject: [PATCH 1/3] Read Schedule A line 14 at Historic Table 2's US rows soi-historic-table-2 (TY2022) and soi-historic-table-2-2023 (TY2023) now read N19700/A19700, "Total charitable contributions" (Schedule A line 14) in the IRS guides of both years, at the US row of all 11 AGI classes: 22 facts per package, 605 -> 627. The measures take the ids, concepts and labels that Table 2.1 and the state broad packages already use for the same line (charitable_returns, charitable_amount), so the 51 state rows of each edition have a US total from the same file. The TY2023 twin gets the same two measures, as test_packages_mirror_their_2022_twins requires. The new measures come after the 55 existing ones (ordinals 55 and 56), so the existing facts keep their keys. The packages already parse every cell of the US rows, so the source cell counts do not move. New tests/test_chronicle_soi_ht2_us_charitable.py checks, for both years, each new fact against its cell read with the csv module, the AGI classes against the all-returns fact, the 51 states plus OA and PR against the new US fact, and the ratio to Table 2.1's line 14 for the same tax year (0.9931/0.9937 for TY2022, 0.9842/0.9950 for TY2023). The count pins of the package, guide and single-source bundle tests move by 22. Microcosm's US target nesting (PolicyEngine/microcosm#1760) needs the TY2022 US total to give the Historic Table 2 state rows of this line their shares. Co-Authored-By: Claude Opus 5.5 --- .../irs-soi-ht2-us-charitable.added.md | 6 + docs/us-legacy-etl-cleanup.md | 2 +- .../historic_table_2/source_package.yaml | 23 +++ .../historic_table_2_2023/source_package.yaml | 23 +++ tests/test_chronicle_bundle.py | 4 +- tests/test_chronicle_soi_doc_guide_columns.py | 6 +- tests/test_chronicle_soi_ht2_2023.py | 8 +- tests/test_chronicle_soi_ht2_us_charitable.py | 190 ++++++++++++++++++ tests/test_chronicle_source_package.py | 22 +- 9 files changed, 273 insertions(+), 11 deletions(-) create mode 100644 changelog.d/irs-soi-ht2-us-charitable.added.md create mode 100644 tests/test_chronicle_soi_ht2_us_charitable.py diff --git a/changelog.d/irs-soi-ht2-us-charitable.added.md b/changelog.d/irs-soi-ht2-us-charitable.added.md new file mode 100644 index 00000000..165072c9 --- /dev/null +++ b/changelog.d/irs-soi-ht2-us-charitable.added.md @@ -0,0 +1,6 @@ +Add Schedule A line 14, the contributions deduction (`N19700`/`A19700`), to +the US rows of IRS SOI Historic Table 2: `soi-historic-table-2` (TY2022) and +`soi-historic-table-2-2023` (TY2023) each read `charitable_returns` and +`charitable_amount` at all 11 AGI classes (22 facts each). They use the ids, +concepts and labels of Table 2.1 and of the state packages, so the state rows +of each edition now have a US total from the same file. diff --git a/docs/us-legacy-etl-cleanup.md b/docs/us-legacy-etl-cleanup.md index 5949305c..b2f0abe3 100644 --- a/docs/us-legacy-etl-cleanup.md +++ b/docs/us-legacy-etl-cleanup.md @@ -120,7 +120,7 @@ counted 32,067,810. The retired tests pinned the invented levels: for example | `db/etl_soi_credits.py` CTC and ACTC by number of children (national) | — | Gap: no packaged IRS table publishes CTC or ACTC by number of qualifying children. | | `db/etl_soi_deductions.py` `salt_claims`, `salt_amount` (state) | `soi-historic-table-2-state-broad-2022` `taxes_paid_deduction_*` (`N18300`/`A18300`, Schedule A line 7), `limited_state_local_taxes_*` (line 5e), `real_estate_taxes_*` (line 5b) | The legacy "SALT" named no IRS line, so no one measure is a like-for-like replacement. None for 2022. | | `db/etl_soi_deductions.py` `mortgage_interest_claims`, `mortgage_interest_amount` (state) | state: `soi-historic-table-2-state-broad-2022` `mortgage_interest_paid_*` (`N19300`/`A19300`, Schedule A line 8a, Historic Table 2's state rows; the district file's state-total rows for the same column give no fact since chronicle#449); national: `soi-table-2-1` `mortgage_interest_paid_*` | None for 2022. The two state series are separate IRS tabulations of one line and differ by state. | -| `db/etl_soi_deductions.py` `charitable_claims`, `charitable_amount` (state) | state: `soi-historic-table-2-state-broad-2022` `charitable_*` (`N19700`/`A19700`, Schedule A line 14, Historic Table 2's state rows; the district file's state-total rows for the same column give no fact since chronicle#449); national: `soi-table-2-1` `charitable_*` | None for 2022. The two state series are separate IRS tabulations of one line and differ by state (WY `A19700`: $572M in the district file, $1,204M in Historic Table 2). | +| `db/etl_soi_deductions.py` `charitable_claims`, `charitable_amount` (state) | state: `soi-historic-table-2-state-broad-2022` `charitable_*` (`N19700`/`A19700`, Schedule A line 14, Historic Table 2's state rows; the district file's state-total rows for the same column give no fact since chronicle#449); national: `soi-historic-table-2` and `soi-historic-table-2-2023` `charitable_*` (the same file's US row), `soi-table-2-1` `charitable_*` | None for 2022. The two state series are separate IRS tabulations of one line and differ by state (WY `A19700`: $572M in the district file, $1,204M in Historic Table 2). | | `db/etl_soi_deductions.py` `medical_claims`, `medical_amount` (state) | `soi-historic-table-2-state-broad-2022` `medical_dental_expense_*` | None for 2022. | | `db/etl_soi_deductions.py` `qbi_claims`, `qbi_amount` (state) | `soi-historic-table-2-state-broad-2022` `qbi_claims`, `qbi_amount`; `soi-congressional-district-2022` `qualified_business_income_deduction_*` (`N04475`/`A04475`) for districts only: its state-total rows give no fact since chronicle#449 | None for 2022. chronicle#345 repinned the broad package's `qbi_*` from `N03270`/`A03270` to `N04475`/`A04475`. | | `db/etl_soi_income_sources.py` taxable and tax-exempt interest, ordinary and qualified dividends, Schedule C, partnership and S corporation, rental and royalty (national and state) | national: `soi-historic-table-2` (US row); state: `soi-historic-table-2-state-broad-2022` `taxable_interest_*`, `tax_exempt_interest_*`, `ordinary_dividends_*`, `qualified_dividends_*`, `schedule_c_income_*`, `partnership_scorp_income_*`, `rental_royalty_income_*` | None for 2022. | diff --git a/packages/irs_soi/historic_table_2/source_package.yaml b/packages/irs_soi/historic_table_2/source_package.yaml index 59fb1459..fee84193 100644 --- a/packages/irs_soi/historic_table_2/source_package.yaml +++ b/packages/irs_soi/historic_table_2/source_package.yaml @@ -902,3 +902,26 @@ record_sets: expected_column_header_row: 1 expected_column_header: A11070 value_scale: 1000 + - measure_id: charitable_returns + label: Returns with contributions deduction + ordinal: 55 + column: CP + source_column_id: N19700 + concept: irs_soi.returns_with_contributions_deduction + unit: count + aggregation: sum + expected_cell_type: number + expected_column_header_row: 1 + expected_column_header: N19700 + - measure_id: charitable_amount + label: Contributions deduction amount + ordinal: 56 + column: CQ + source_column_id: A19700 + concept: irs_soi.contributions_deduction + unit: usd + aggregation: sum + expected_cell_type: number + expected_column_header_row: 1 + expected_column_header: A19700 + value_scale: 1000 diff --git a/packages/irs_soi/historic_table_2_2023/source_package.yaml b/packages/irs_soi/historic_table_2_2023/source_package.yaml index f36697ef..3178300d 100644 --- a/packages/irs_soi/historic_table_2_2023/source_package.yaml +++ b/packages/irs_soi/historic_table_2_2023/source_package.yaml @@ -953,3 +953,26 @@ record_sets: expected_column_header_row: 1 expected_column_header: A11070 value_scale: 1000 + - measure_id: charitable_returns + label: Returns with contributions deduction + ordinal: 55 + column: CP + source_column_id: N19700 + concept: irs_soi.returns_with_contributions_deduction + unit: count + aggregation: sum + expected_cell_type: number + expected_column_header_row: 1 + expected_column_header: N19700 + - measure_id: charitable_amount + label: Contributions deduction amount + ordinal: 56 + column: CQ + source_column_id: A19700 + concept: irs_soi.contributions_deduction + unit: usd + aggregation: sum + expected_cell_type: number + expected_column_header_row: 1 + expected_column_header: A19700 + value_scale: 1000 diff --git a/tests/test_chronicle_bundle.py b/tests/test_chronicle_bundle.py index 56100dfd..f5cb8dce 100644 --- a/tests/test_chronicle_bundle.py +++ b/tests/test_chronicle_bundle.py @@ -3736,9 +3736,9 @@ def test_build_bundle_cli_supports_historic_table_2_source(tmp_path, capsys): assert exit_code == 0 assert payload["valid"] assert payload["counts"]["source_package_count"] == 1 - assert payload["counts"]["fact_count"] == 605 + assert payload["counts"]["fact_count"] == 627 assert payload["coverage"]["counts"]["by_source_table"] == { - "irs_soi:Historic Table 2": 605 + "irs_soi:Historic Table 2": 627 } assert ( output_dir / "sources" / "soi-historic-table-2" / "source_rows.jsonl" diff --git a/tests/test_chronicle_soi_doc_guide_columns.py b/tests/test_chronicle_soi_doc_guide_columns.py index 255b6a3f..31555721 100644 --- a/tests/test_chronicle_soi_doc_guide_columns.py +++ b/tests/test_chronicle_soi_doc_guide_columns.py @@ -1129,6 +1129,10 @@ def test_congressional_district_us_row_is_a_subset_of_historic_table_2( "real_estate_taxes_claims", "real_estate_taxes_amount", "itemized_deductions_returns", + # Schedule A line 14, which Historic Table 2's US row reads like its + # state rows. + "charitable_returns", + "charitable_amount", } <= set(pairs) missing = { measure_id @@ -1136,7 +1140,7 @@ def test_congressional_district_us_row_is_a_subset_of_historic_table_2( if HT2_TO_DISTRICT_IDS.get(measure_id, measure_id) not in district } assert missing == NOT_READ_BY_DISTRICT[year] - assert len(pairs) == 55 - len(missing) + assert len(pairs) == 57 - len(missing) # None of the compared US cells is a fact of the district package. assert not { HT2_TO_DISTRICT_IDS.get(measure_id, measure_id) for measure_id in pairs diff --git a/tests/test_chronicle_soi_ht2_2023.py b/tests/test_chronicle_soi_ht2_2023.py index 531c1523..ed929c85 100644 --- a/tests/test_chronicle_soi_ht2_2023.py +++ b/tests/test_chronicle_soi_ht2_2023.py @@ -35,7 +35,7 @@ # TY2023 package -> (TY2022 twin, fact count) PACKAGES = { - "soi-historic-table-2-2023": ("soi-historic-table-2", 605), + "soi-historic-table-2-2023": ("soi-historic-table-2", 627), "soi-historic-table-2-state-broad-2023": ( "soi-historic-table-2-state-broad-2022", 3111, @@ -232,6 +232,10 @@ def test_packages_emit_their_fact_counts_and_published_totals(built): # self-employment health insurance deduction (N03270/A03270). assert by_id[f"{prefix}.qbi_claims"].value == 26_391_030 assert by_id[f"{prefix}.qbi_amount"].value == 213_733_168_000 + # Schedule A line 14 (N19700/A19700), the column pair the state package + # reads under the same ids. + assert by_id[f"{prefix}.charitable_returns"].value == 11_562_200 + assert by_id[f"{prefix}.charitable_amount"].value == 210_916_402_000 @pytest.mark.parametrize("package_id", sorted(PACKAGES)) @@ -361,7 +365,7 @@ def test_national_agi_bands_add_up_to_the_all_returns_row(built): entry["total"] = fact else: entry["bands"] += fact.value - assert len(by_measure) == 55 + assert len(by_measure) == 57 for measure_id, entry in by_measure.items(): total = entry["total"] assert _gap_ok(entry["bands"], total.value, 11, total.measure.unit), measure_id diff --git a/tests/test_chronicle_soi_ht2_us_charitable.py b/tests/test_chronicle_soi_ht2_us_charitable.py new file mode 100644 index 00000000..978877fd --- /dev/null +++ b/tests/test_chronicle_soi_ht2_us_charitable.py @@ -0,0 +1,190 @@ +"""Historic Table 2's US rows for Schedule A line 14, tax years 2022 and 2023. + +``soi-historic-table-2`` (``22in55cmcsv.csv``) and ``soi-historic-table-2-2023`` +(``23in55cmcsv.csv``) read ``N19700``/``A19700`` at the US row of each AGI +class. The IRS documentation guides of both years define the pair as "Total +charitable contributions", Schedule A line 14. The state packages read the same +pair for each state under the same ids, concepts and labels (chronicle#350), so +the new US facts are the totals of the file that holds those state rows. + +These tests read the publisher CSV with the csv module alone, so the values +they check do not depend on Chronicle's own row parser. +""" + +from __future__ import annotations + +import csv +from functools import cache +from pathlib import Path +from typing import Any + +import pytest + +from chronicle.source_package import load_source_package + +REPO_ROOT = Path(__file__).resolve().parents[1] +HT2_DIR = REPO_ROOT / "db" / "data" / "irs_soi" / "historic_table_2" +# tax year -> (national package, state package, publisher file) +EDITIONS = { + 2022: ( + "soi-historic-table-2", + "soi-historic-table-2-state-broad-2022", + "22in55cmcsv.csv", + ), + 2023: ( + "soi-historic-table-2-2023", + "soi-historic-table-2-state-broad-2023", + "23in55cmcsv.csv", + ), +} +# measure_id -> (IRS variable, concept, label), as Table 2.1 declares them. +LINE_14 = { + "charitable_returns": ( + "N19700", + "irs_soi.returns_with_contributions_deduction", + "Returns with contributions deduction", + ), + "charitable_amount": ( + "A19700", + "irs_soi.contributions_deduction", + "Contributions deduction amount", + ), +} +# The package's AGI class value ids by the file's AGI_STUB. +INCOME_RANGE_BY_STUB = { + 0: "all", 1: "under_1", 2: "1_to_10k", 3: "10k_to_25k", 4: "25k_to_50k", + 5: "50k_to_75k", 6: "75k_to_100k", 7: "100k_to_200k", 8: "200k_to_500k", + 9: "500k_to_1m", 10: "1m_plus", +} # fmt: skip +# Historic Table 2 publishes these rows besides the 50 states and DC. +NON_STATE_ROWS = frozenset({"US", "OA", "PR"}) +# The publisher rounds return counts to tens, so a sum of n count cells can +# miss its published total by up to 5n. +ROUNDING_PER_COUNT_CELL = 5 + + +@cache +def _csv_rows(filename: str) -> dict[tuple[str, int], dict[str, str]]: + with (HT2_DIR / filename).open(newline="", encoding="utf-8-sig") as handle: + return { + (row["STATE"], int(row["AGI_STUB"])): row for row in csv.DictReader(handle) + } + + +def _cell(row: dict[str, str], variable: str) -> int: + scale = 1000 if variable.startswith("A") else 1 + return int(row[variable].replace(",", "")) * scale + + +@cache +def _facts(package_id: str, build_year: int = 2023) -> dict[str, Any]: + package = load_source_package(package_id) + return {fact.source_record_id: fact for fact in package.build_facts(build_year)} + + +def _us_fact(year: int, income_range: str, measure_id: str) -> Any: + national = EDITIONS[year][0] + prefix = f"irs_soi.ty{year}.historic_table_2.us" + return _facts(national)[f"{prefix}.{income_range}.{measure_id}"] + + +@pytest.mark.parametrize("year", sorted(EDITIONS)) +def test_every_new_fact_is_its_published_us_cell(year): + """Exhaustive: two measures at the US row of all 11 AGI classes.""" + rows = _csv_rows(EDITIONS[year][2]) + national = EDITIONS[year][0] + line_14 = [ + fact for fact in _facts(national).values() if fact.layout.measure_id in LINE_14 + ] + + assert len(line_14) == 22 + for stub, income_range in INCOME_RANGE_BY_STUB.items(): + for measure_id, (variable, concept, label) in LINE_14.items(): + fact = _us_fact(year, income_range, measure_id) + where = f"{year}:{income_range}:{measure_id}" + assert fact.value == _cell(rows[("US", stub)], variable), where + assert fact.layout.source_column_id == variable, where + assert fact.measure.concept == concept, where + assert fact.layout.measure_label == label, where + assert fact.measure.unit == ( + "usd" if variable.startswith("A") else "count" + ), where + assert fact.geography.id == "0100000US", where + assert fact.period.type == "tax_year", where + assert fact.period.value == year, where + + +@pytest.mark.parametrize("year", sorted(EDITIONS)) +def test_agi_classes_add_up_to_the_all_returns_fact(year): + """Amounts add exactly; ten rounded counts miss by at most 5 x 10.""" + for measure_id, (variable, _, _) in LINE_14.items(): + total = _us_fact(year, "all", measure_id).value + classes = sum( + _us_fact(year, income_range, measure_id).value + for income_range in INCOME_RANGE_BY_STUB.values() + if income_range != "all" + ) + if variable.startswith("A"): + assert classes == total, (year, measure_id) + else: + assert abs(classes - total) <= ROUNDING_PER_COUNT_CELL * 10, ( + year, + measure_id, + ) + + +@pytest.mark.parametrize("year", sorted(EDITIONS)) +def test_the_state_facts_add_up_to_the_new_us_fact(year): + """Accounting identity of the published file, checked through the facts. + + The 51 state facts plus the file's other-areas (OA) and Puerto Rico (PR) + cells equal the new US fact: amounts exactly, rounded counts within 5 x 53. + """ + _, state_package, filename = EDITIONS[year] + rows = _csv_rows(filename) + state_prefix = f"irs_soi.ty{year}.historic_table_2.state_broad." + for measure_id, (variable, _, _) in LINE_14.items(): + states = [ + fact + for record_id, fact in _facts(state_package).items() + if record_id.startswith(state_prefix) + and record_id.endswith(f".all.{measure_id}") + ] + assert len(states) == 51, (year, measure_id) + total = sum(fact.value for fact in states) + total += sum(_cell(rows[(row, 0)], variable) for row in ("OA", "PR")) + us = _us_fact(year, "all", measure_id).value + if variable.startswith("A"): + assert total == us, (year, measure_id) + else: + assert abs(total - us) <= ROUNDING_PER_COUNT_CELL * 53, (year, measure_id) + + +@pytest.mark.parametrize( + ("year", "expected"), + [ + (2022, {"charitable_returns": 0.9931, "charitable_amount": 0.9937}), + (2023, {"charitable_returns": 0.9842, "charitable_amount": 0.9950}), + ], +) +def test_the_us_facts_track_table_2_1_line_14(year, expected): + """Differential check against the national publication's same line. + + Table 2.1 (Publication 1304) tabulates itemized returns from the SOI + sample; Historic Table 2 tabulates the state files. Their line 14 totals + for one tax year differ by under 2%. A column pair one place over would + not: on the US row, ``N19570``/``A19570`` and ``N20950``/``A20950`` are 7% + to 25% of these totals in both years. + """ + table_2_1 = _facts("soi-table-2-1", year) + prefix = f"irs_soi.ty{year}.table_2_1.itemized_all_returns.all." + ratios = { + measure_id: round( + _us_fact(year, "all", measure_id).value + / table_2_1[prefix + measure_id].value, + 4, + ) + for measure_id in LINE_14 + } + + assert ratios == expected diff --git a/tests/test_chronicle_source_package.py b/tests/test_chronicle_source_package.py index 25e74a51..69334cf2 100644 --- a/tests/test_chronicle_source_package.py +++ b/tests/test_chronicle_source_package.py @@ -1842,8 +1842,8 @@ def test_national_soi_source_package_aliases_validate_fixture_counts(): "soi-historic-table-2-2023": { "record_set_count": 1, "row_count": 11, - "measure_count": 55, - "source_record_count": 605, + "measure_count": 57, + "source_record_count": 627, "source_region_count": 1, }, "soi-historic-table-2-state-broad-2023": { @@ -4392,8 +4392,8 @@ def test_soi_historic_table_2_source_package_alias_validates_fixture_counts(): assert report.counts == { "record_set_count": 1, "row_count": 11, - "measure_count": 55, - "source_record_count": 605, + "measure_count": 57, + "source_record_count": 627, "source_region_count": 1, } @@ -4684,7 +4684,7 @@ def test_soi_historic_table_2_package_builds_2022_national_facts(): assert validate_source_cells(cells).valid assert validate_facts(facts).valid assert len(cells) == 1_956 - assert len(facts) == 605 + assert len(facts) == 627 assert all(fact.source_row_keys for fact in facts) tax_filers = values_by_record[ @@ -4715,6 +4715,12 @@ def test_soi_historic_table_2_package_builds_2022_national_facts(): ] ctc = values_by_record["irs_soi.ty2022.historic_table_2.us.all.ctc_amount"] actc = values_by_record["irs_soi.ty2022.historic_table_2.us.all.actc_amount"] + charitable_returns = values_by_record[ + "irs_soi.ty2022.historic_table_2.us.all.charitable_returns" + ] + charitable = values_by_record[ + "irs_soi.ty2022.historic_table_2.us.all.charitable_amount" + ] agi_bracket_eitc_claims = values_by_record[ "irs_soi.ty2022.historic_table_2.us.1_to_10k.eitc_claims" ] @@ -4734,6 +4740,12 @@ def test_soi_historic_table_2_package_builds_2022_national_facts(): assert rental.value == 85_642_801_000 assert ctc.value == 82_862_736_000 assert actc.value == 33_857_987_000 + # Schedule A line 14 (N19700/A19700), under the state package's ids. + assert charitable_returns.value == 12_095_980 + assert charitable_returns.layout.source_column_id == "N19700" + assert charitable.value == 220_984_505_000 + assert charitable.layout.source_column_id == "A19700" + assert charitable.measure.concept == "irs_soi.contributions_deduction" assert agi_bracket_eitc_claims.value == 5_013_220 assert { constraint.operator for constraint in agi_bracket_eitc_claims.constraints From 39830e914b1a716de522e07d32e7330dd445156b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Juaristi?= <127882282+juaristi22@users.noreply.github.com> Date: Tue, 1 Dec 2026 19:23:44 +0000 Subject: [PATCH 2/3] Move the bundle and district-rule pins for line 14 at the US rows Full-bundle pins (tests/test_chronicle_bundle.py), measured with the 34 IRS SOI packages built at bundle year 2023 on 13294b93 and on this branch: fact count, irs_soi, country:0100000US and tax_unit +44, tax_year:2022 and tax_year:2023 +22 each. No new geography, period or entity key, semantic duplicate key (95 IRS keys before and after) or warning. chronicle#449's district totals rule: the district files' United States totals of line 14 now share a semantic key with the new Historic Table 2 facts, so the first rule removes them and their Publication 1304 pair is no longer needed. PUBLICATION_1304_LINES drops the two charitable pairs (17 -> 15, eight Schedule A items in Table 2.1), and the district packages' comments say so. Counted with the test's rules: same key 3,571 and 3,468 (were 3,569 and 3,466), same column 1,030 and 979, declared pairs 15; the removed totals stay 4,616 and 4,462, so no district fact changes. Judge notes answered: the count bounds of the new test allow for the rounded total (5 x 11 and 5 x 54), the test builds each package at its tax year and drops an unused constant, the changelog fragment describes the US row as the file's all-returns total and gives the new chronicle#449 counts, and the legacy ETL doc says the two national series of line 14 differ. The first commit's message says the guide pin moves by 22; it moves by 2 (55 -> 57 measures). Co-Authored-By: Claude Opus 5.5 --- .../irs-soi-ht2-us-charitable.added.md | 17 +++++++++-- docs/us-legacy-etl-cleanup.md | 2 +- .../source_package.yaml | 4 +-- .../source_package.yaml | 4 +-- tests/test_chronicle_bundle.py | 21 +++++++++---- ...test_chronicle_soi_district_agi_classes.py | 30 +++++++++++-------- tests/test_chronicle_soi_ht2_us_charitable.py | 27 +++++++++-------- 7 files changed, 67 insertions(+), 38 deletions(-) diff --git a/changelog.d/irs-soi-ht2-us-charitable.added.md b/changelog.d/irs-soi-ht2-us-charitable.added.md index 165072c9..c8ed8888 100644 --- a/changelog.d/irs-soi-ht2-us-charitable.added.md +++ b/changelog.d/irs-soi-ht2-us-charitable.added.md @@ -1,6 +1,17 @@ Add Schedule A line 14, the contributions deduction (`N19700`/`A19700`), to the US rows of IRS SOI Historic Table 2: `soi-historic-table-2` (TY2022) and `soi-historic-table-2-2023` (TY2023) each read `charitable_returns` and -`charitable_amount` at all 11 AGI classes (22 facts each). They use the ids, -concepts and labels of Table 2.1 and of the state packages, so the state rows -of each edition now have a US total from the same file. +`charitable_amount` at all 11 AGI classes, 22 facts each (each package now has +57 measures and 627 facts). The measures use the ids, concepts and labels that +Table 2.1 and the state broad packages use for the same line. The US row is the +file's total for all returns: it includes the file's other-areas and Puerto +Rico rows as well as the 50 states and DC. + +The congressional district files' United States totals of line 14 now share a +semantic key with these facts, so chronicle#449's third rule pairs 15 United +States totals of each year with Publication 1304, not 17 (eight Schedule A +items in Table 2.1, not ten). 3,571 TY2022 and 3,468 TY2023 removed totals have +a same-key twin, not 3,569 and 3,466. The new twins are in packages pinned to +their tax year, so twelve, not fourteen, of the removed United States totals of +each year have their only twin in a Publication 1304 package that builds one +tax year at a time. No district fact changes. diff --git a/docs/us-legacy-etl-cleanup.md b/docs/us-legacy-etl-cleanup.md index b2f0abe3..95fb8a39 100644 --- a/docs/us-legacy-etl-cleanup.md +++ b/docs/us-legacy-etl-cleanup.md @@ -120,7 +120,7 @@ counted 32,067,810. The retired tests pinned the invented levels: for example | `db/etl_soi_credits.py` CTC and ACTC by number of children (national) | — | Gap: no packaged IRS table publishes CTC or ACTC by number of qualifying children. | | `db/etl_soi_deductions.py` `salt_claims`, `salt_amount` (state) | `soi-historic-table-2-state-broad-2022` `taxes_paid_deduction_*` (`N18300`/`A18300`, Schedule A line 7), `limited_state_local_taxes_*` (line 5e), `real_estate_taxes_*` (line 5b) | The legacy "SALT" named no IRS line, so no one measure is a like-for-like replacement. None for 2022. | | `db/etl_soi_deductions.py` `mortgage_interest_claims`, `mortgage_interest_amount` (state) | state: `soi-historic-table-2-state-broad-2022` `mortgage_interest_paid_*` (`N19300`/`A19300`, Schedule A line 8a, Historic Table 2's state rows; the district file's state-total rows for the same column give no fact since chronicle#449); national: `soi-table-2-1` `mortgage_interest_paid_*` | None for 2022. The two state series are separate IRS tabulations of one line and differ by state. | -| `db/etl_soi_deductions.py` `charitable_claims`, `charitable_amount` (state) | state: `soi-historic-table-2-state-broad-2022` `charitable_*` (`N19700`/`A19700`, Schedule A line 14, Historic Table 2's state rows; the district file's state-total rows for the same column give no fact since chronicle#449); national: `soi-historic-table-2` and `soi-historic-table-2-2023` `charitable_*` (the same file's US row), `soi-table-2-1` `charitable_*` | None for 2022. The two state series are separate IRS tabulations of one line and differ by state (WY `A19700`: $572M in the district file, $1,204M in Historic Table 2). | +| `db/etl_soi_deductions.py` `charitable_claims`, `charitable_amount` (state) | state: `soi-historic-table-2-state-broad-2022` `charitable_*` (`N19700`/`A19700`, Schedule A line 14, Historic Table 2's state rows; the district file's state-total rows for the same column give no fact since chronicle#449); national: `soi-historic-table-2` and `soi-historic-table-2-2023` `charitable_*` (the same file's US row), `soi-table-2-1` `charitable_*` | None for 2022. The two state series are separate IRS tabulations of one line and differ by state (WY `A19700`: $572M in the district file, $1,204M in Historic Table 2). The two national series are separate tabulations too: Historic Table 2's US row is 0.5% to 1.6% below Table 2.1 for TY2022 and TY2023. | | `db/etl_soi_deductions.py` `medical_claims`, `medical_amount` (state) | `soi-historic-table-2-state-broad-2022` `medical_dental_expense_*` | None for 2022. | | `db/etl_soi_deductions.py` `qbi_claims`, `qbi_amount` (state) | `soi-historic-table-2-state-broad-2022` `qbi_claims`, `qbi_amount`; `soi-congressional-district-2022` `qualified_business_income_deduction_*` (`N04475`/`A04475`) for districts only: its state-total rows give no fact since chronicle#449 | None for 2022. chronicle#345 repinned the broad package's `qbi_*` from `N03270`/`A03270` to `N04475`/`A04475`. | | `db/etl_soi_income_sources.py` taxable and tax-exempt interest, ordinary and qualified dividends, Schedule C, partnership and S corporation, rental and royalty (national and state) | national: `soi-historic-table-2` (US row); state: `soi-historic-table-2-state-broad-2022` `taxable_interest_*`, `tax_exempt_interest_*`, `ordinary_dividends_*`, `qualified_dividends_*`, `schedule_c_income_*`, `partnership_scorp_income_*`, `rental_royalty_income_*` | None for 2022. | diff --git a/packages/irs_soi/congressional_district_2022/source_package.yaml b/packages/irs_soi/congressional_district_2022/source_package.yaml index 0bff20fc..f1e1423e 100644 --- a/packages/irs_soi/congressional_district_2022/source_package.yaml +++ b/packages/irs_soi/congressional_district_2022/source_package.yaml @@ -14425,8 +14425,8 @@ artifact: # state data); # 2. the same IRS column in a Historic Table 2 package at the same level, under # another concept id or domain; -# 3. for 17 United States totals, the same tax-form line in Publication 1304 -# Table 2.1 (ten Schedule A items), Table 2.5 (the EITC for no, one and two +# 3. for 15 United States totals, the same tax-form line in Publication 1304 +# Table 2.1 (eight Schedule A items), Table 2.5 (the EITC for no, one and two # qualifying children) or Table 1.4 (the self-employed retirement plans # deduction), which use another domain or concept id. The pairs are listed # in tests/test_chronicle_soi_district_agi_classes.py. diff --git a/packages/irs_soi/congressional_district_2023/source_package.yaml b/packages/irs_soi/congressional_district_2023/source_package.yaml index ccea6066..d980d4c6 100644 --- a/packages/irs_soi/congressional_district_2023/source_package.yaml +++ b/packages/irs_soi/congressional_district_2023/source_package.yaml @@ -14455,8 +14455,8 @@ artifact: # state data); # 2. the same IRS column in a Historic Table 2 package at the same level, under # another concept id or domain; -# 3. for 17 United States totals, the same tax-form line in Publication 1304 -# Table 2.1 (ten Schedule A items), Table 2.5 (the EITC for no, one and two +# 3. for 15 United States totals, the same tax-form line in Publication 1304 +# Table 2.1 (eight Schedule A items), Table 2.5 (the EITC for no, one and two # qualifying children) or Table 1.4 (the self-employed retirement plans # deduction), which use another domain or concept id. The pairs are listed # in tests/test_chronicle_soi_district_agi_classes.py. diff --git a/tests/test_chronicle_bundle.py b/tests/test_chronicle_bundle.py index f5cb8dce..f9cf4a70 100644 --- a/tests/test_chronicle_bundle.py +++ b/tests/test_chronicle_bundle.py @@ -658,7 +658,11 @@ def test_build_bundle_writes_merged_consumer_contract(tmp_path): # 4,462 TY2023 totals go), and the one row of each single-district # jurisdiction becomes a district fact (1,000 and 868 new). TY2022 # goes from 59,982 facts to 56,366 and TY2023 from 59,626 to 56,032. - "fact_count": 2416365, + # Historic Table 2's US rows add Schedule A line 14 (N19700/A19700) at + # the 11 AGI classes of TY2022 and TY2023: 44 tax-unit facts. Measured + # with the IRS SOI packages at bundle year 2023: no new geography, + # period or entity key, duplicate key or warning. + "fact_count": 2416409, # The same change adds 82,064 geography keys: 46,363 Scottish 2022 # output areas (UV404), 35,673 LSOA-level keys from the DESNZ LSOA # workbooks (Scotland's data zones already exist), the 26 Northern @@ -912,7 +916,7 @@ def test_build_bundle_writes_merged_consumer_contract(tmp_path): "jct-tax-expenditures-2024", "jct-tax-expenditures-2025", ] - assert coverage["fact_count"] == 2416365 + assert coverage["fact_count"] == 2416409 assert coverage["counts"]["by_source"] == { "bea": 446, "bfp_economic_outlook": 5, @@ -939,7 +943,7 @@ def test_build_bundle_writes_merged_consumer_contract(tmp_path): "hmrc": 40025, "ici": 12, "ird": 4362, - "irs_soi": 146622, + "irs_soi": 146666, "isc": 4, "jrc_euromod_be": 90, "kff": 52, @@ -2440,6 +2444,10 @@ def test_build_bundle_writes_merged_consumer_contract(tmp_path): # go and 1,000 district facts are new. TY2023: 4,462 go and 868 are new. expected_period_counts["tax_year:2022"] += 1000 - 4616 expected_period_counts["tax_year:2023"] += 868 - 4462 + # Historic Table 2's US rows: Schedule A line 14, two measures at 11 AGI + # classes, for each of TY2022 and TY2023. + expected_period_counts["tax_year:2022"] += 22 + expected_period_counts["tax_year:2023"] += 22 # BEA NIPA Table 7.8 line 17 (B4923C): the bundle year's own column. expected_period_counts["calendar_year:2023"] += 1 # chronicle#343: ONS constituency sales and medians for the years ending @@ -3145,7 +3153,9 @@ def test_build_bundle_writes_merged_consumer_contract(tmp_path): # chronicle#449 takes 153 off: the district file's United States totals # that another package gives (77 of TY2022 and 76 of TY2023). 30 stay for # each year. - assert coverage["counts"]["by_geography"]["country:0100000US"] == 10840 + # Historic Table 2's US rows add 44: Schedule A line 14, two measures at + # 11 AGI classes, for TY2022 and TY2023. + assert coverage["counts"]["by_geography"]["country:0100000US"] == 10884 # 255, less Vintage 2024's state total (chronicle#397), + 16 Census Vintage # 2025 age groups (15 groups and the total), + 6 from SSA's Supplement # Tables 7.B3 (four California cells) and 7.B7 (two), + 8 Historic Table 2 @@ -3327,7 +3337,8 @@ def test_build_bundle_writes_merged_consumer_contract(tmp_path): # Trustees 1) and household 55 (SNAP 54, LIHEAP 1). # chronicle#444 adds 73,805 tax units. # chronicle#449's rule for the district file's totals takes 7,210 off. - "tax_unit": 147927, + # Historic Table 2's US rows add 44 (Schedule A line 14). + "tax_unit": 147971, } assert not coverage["duplicates"]["aggregate_fact_keys"] assert len(coverage["duplicates"]["semantic_fact_keys"]) == 2355 diff --git a/tests/test_chronicle_soi_district_agi_classes.py b/tests/test_chronicle_soi_district_agi_classes.py index 30075646..ee578255 100644 --- a/tests/test_chronicle_soi_district_agi_classes.py +++ b/tests/test_chronicle_soi_district_agi_classes.py @@ -27,7 +27,7 @@ district (value id ``_00``). The file's United States row and its state total rows give a fact only where no other IRS package gives the same quantity: the same semantic key, the same IRS column in Historic Table 2, or one of the -17 tax-form lines of ``PUBLICATION_1304_LINES`` below. +15 tax-form lines of ``PUBLICATION_1304_LINES`` below. Those totals sit in the ``*_state_totals`` and ``*_us_total`` record sets, under the source record ids they had before. Checks about the file itself (its classes add up, its districts add up to its state rows) read the CSV, where @@ -916,8 +916,10 @@ def test_a_state_total_row_is_never_a_district_fact(district): TABLE_2_5 = "table_2_5.eitc_by_agi_children" TABLE_2_5_PACKAGE = "soi-table-2-5-eitc-agi-children-{year}" PUBLICATION_1304_LINES = { - # Table 2.1, returns with itemized deductions: Schedule A lines 8a, 8b, 8c, - # 9 and 14, under the district file's measure ids. + # Table 2.1, returns with itemized deductions: Schedule A lines 8a, 8b, 8c + # and 9, under the district file's measure ids. Line 14 (``charitable_*``) + # is not here: Historic Table 2's US row reads its columns (N19700, A19700) + # under the same semantic key, so the first rule removes that total. "mortgage_interest_paid_returns": ( "soi-table-2-1", f"{TABLE_2_1}.mortgage_interest_paid_returns", @@ -950,8 +952,6 @@ def test_a_state_total_row_is_never_a_district_fact(district): "soi-table-2-1", f"{TABLE_2_1}.investment_interest_paid_amount", ), - "charitable_returns": ("soi-table-2-1", f"{TABLE_2_1}.charitable_returns"), - "charitable_amount": ("soi-table-2-1", f"{TABLE_2_1}.charitable_amount"), # Table 2.5: the EITC for no, one and two qualifying children. "eitc_no_children_claims": ( TABLE_2_5_PACKAGE, @@ -1106,7 +1106,7 @@ def test_publication_1304_gives_each_paired_us_line(district): domain or its concept id is another: that is why no semantic key pairs the two. Its value is close to the district file's US cell, read from the CSV, and above it: the district file protects small cells and leaves out returns - that match no district by ZIP code. Over the 17 pairs and both years the + that match no district by ZIP code. Over the 15 pairs and both years the cell is 0.8307 to 0.9895 of the Publication 1304 value, so the bound is 0.80 to 1. A line of the wrong size fails it; ``tests/test_chronicle_soi_doc_guide_columns.py`` tells line 8a from the @@ -1120,13 +1120,13 @@ def test_publication_1304_gives_each_paired_us_line(district): a_district = all_returns["rows"][0]["value_id"] pairs = _publication_1304_records(district) - assert len(pairs) == 17 + assert len(pairs) == 15 assert {package_id for package_id, _ in pairs.values()} == { "soi-table-1-4", "soi-table-2-1", f"soi-table-2-5-eitc-agi-children-{year}", } - assert len(set(pairs.values())) == 17 + assert len(set(pairs.values())) == 15 ratios = {} for measure_id, (package_id, record_id) in pairs.items(): (paired,) = [ @@ -1174,7 +1174,7 @@ def test_publication_1304_gives_each_paired_us_line(district): def test_the_table_1_4_pair_is_schedule_1_line_16_on_both_sides(district): """The Table 1.4 pair is tied to each publication's own words. - The ten Table 2.1 pairs share a measure id and a concept id, and the six + The eight Table 2.1 pairs share a measure id and a concept id, and the six Table 2.5 pairs share a qualifying-children constraint. The Table 1.4 pair shares none of these, and the size bound alone does not tell its line from the next one of Table 1.4: the district file's cell is also 0.92 to 0.93 @@ -1378,7 +1378,7 @@ def test_no_fact_repeats_a_quantity_another_irs_package_gives(district): ids (A00900, A01700, A06500, A25870, A26270 and their N columns) and Historic Table 2's state EITC package has another domain, so the keys differ where the column and its guide text are the same. Third, none of - the 17 United States totals of ``PUBLICATION_1304_LINES`` is a fact. The + the 15 United States totals of ``PUBLICATION_1304_LINES`` is a fact. The first two are derived here, and only sets of keys and of columns are compared. """ @@ -1436,7 +1436,7 @@ def test_a_total_stays_only_where_no_other_package_gives_it(district): gives the total when (1) a ``soi-*`` package of the tax year has that key, (2) a Historic Table 2 package of the tax year reads the same IRS column at the same level for the same class of returns, or (3) it is one of the - 17 United States totals of ``PUBLICATION_1304_LINES``. The total is a fact + 15 United States totals of ``PUBLICATION_1304_LINES``. The total is a fact exactly when none of the three gives it, and a total that stays keeps the source record id it had before. Rules 1 and 2 are derived here, with no list of measures; rule 3's pairs are the declared list, and they are the @@ -1495,9 +1495,9 @@ def test_a_total_stays_only_where_no_other_package_gives_it(district): assert len(kept) == district.state_total_facts + district.us_total_facts # 4,539 state and 77 US totals in TY2022, 4,386 and 76 in TY2023. assert len(dropped) == {2022: 4_616, 2023: 4_462}[district.year] - # Each of the 17 pairs is needed: neither a key nor a column removes it. + # Each of the 15 pairs is needed: neither a key nor a column removes it. assert by_line_only == publication_1304_lines - assert len(by_line_only) == 17 + assert len(by_line_only) == 15 # The totals sit in their own record sets, apart from the district rows. for record_id, fact in facts.items(): record_set_id = fact.layout.record_set_id @@ -1520,6 +1520,10 @@ def test_a_total_stays_only_where_no_other_package_gives_it(district): # retirement plans deduction in Table 1.4 under another concept id. "all_returns.us.mortgage_interest_paid_amount": False, "all_returns.us.self_employed_retirement_plans_amount": False, + # Historic Table 2's US row gives Schedule A line 14 under the same + # semantic key. + "all_returns.us.charitable_amount": False, + "all_returns.us.charitable_returns": False, # No package gives the returns with that deduction or the total tax # liability for the nation, or the standard deduction of a state. "all_returns.us.self_employed_retirement_plans_returns": True, diff --git a/tests/test_chronicle_soi_ht2_us_charitable.py b/tests/test_chronicle_soi_ht2_us_charitable.py index 978877fd..e8b53f89 100644 --- a/tests/test_chronicle_soi_ht2_us_charitable.py +++ b/tests/test_chronicle_soi_ht2_us_charitable.py @@ -56,10 +56,8 @@ 5: "50k_to_75k", 6: "75k_to_100k", 7: "100k_to_200k", 8: "200k_to_500k", 9: "500k_to_1m", 10: "1m_plus", } # fmt: skip -# Historic Table 2 publishes these rows besides the 50 states and DC. -NON_STATE_ROWS = frozenset({"US", "OA", "PR"}) -# The publisher rounds return counts to tens, so a sum of n count cells can -# miss its published total by up to 5n. +# The publisher rounds each return count to tens, the total too, so n cells +# can miss their published total by up to 5(n + 1). ROUNDING_PER_COUNT_CELL = 5 @@ -77,7 +75,8 @@ def _cell(row: dict[str, str], variable: str) -> int: @cache -def _facts(package_id: str, build_year: int = 2023) -> dict[str, Any]: +def _facts(package_id: str, build_year: int) -> dict[str, Any]: + """The package's facts, built at the tax year they publish.""" package = load_source_package(package_id) return {fact.source_record_id: fact for fact in package.build_facts(build_year)} @@ -85,7 +84,7 @@ def _facts(package_id: str, build_year: int = 2023) -> dict[str, Any]: def _us_fact(year: int, income_range: str, measure_id: str) -> Any: national = EDITIONS[year][0] prefix = f"irs_soi.ty{year}.historic_table_2.us" - return _facts(national)[f"{prefix}.{income_range}.{measure_id}"] + return _facts(national, year)[f"{prefix}.{income_range}.{measure_id}"] @pytest.mark.parametrize("year", sorted(EDITIONS)) @@ -94,7 +93,9 @@ def test_every_new_fact_is_its_published_us_cell(year): rows = _csv_rows(EDITIONS[year][2]) national = EDITIONS[year][0] line_14 = [ - fact for fact in _facts(national).values() if fact.layout.measure_id in LINE_14 + fact + for fact in _facts(national, year).values() + if fact.layout.measure_id in LINE_14 ] assert len(line_14) == 22 @@ -116,7 +117,8 @@ def test_every_new_fact_is_its_published_us_cell(year): @pytest.mark.parametrize("year", sorted(EDITIONS)) def test_agi_classes_add_up_to_the_all_returns_fact(year): - """Amounts add exactly; ten rounded counts miss by at most 5 x 10.""" + """Amounts add exactly; ten rounded counts and their rounded total miss + by at most 5 x 11.""" for measure_id, (variable, _, _) in LINE_14.items(): total = _us_fact(year, "all", measure_id).value classes = sum( @@ -127,7 +129,7 @@ def test_agi_classes_add_up_to_the_all_returns_fact(year): if variable.startswith("A"): assert classes == total, (year, measure_id) else: - assert abs(classes - total) <= ROUNDING_PER_COUNT_CELL * 10, ( + assert abs(classes - total) <= ROUNDING_PER_COUNT_CELL * 11, ( year, measure_id, ) @@ -138,7 +140,8 @@ def test_the_state_facts_add_up_to_the_new_us_fact(year): """Accounting identity of the published file, checked through the facts. The 51 state facts plus the file's other-areas (OA) and Puerto Rico (PR) - cells equal the new US fact: amounts exactly, rounded counts within 5 x 53. + cells equal the new US fact: amounts exactly, rounded counts within 5 x 54 + (53 rounded cells and the rounded US cell). """ _, state_package, filename = EDITIONS[year] rows = _csv_rows(filename) @@ -146,7 +149,7 @@ def test_the_state_facts_add_up_to_the_new_us_fact(year): for measure_id, (variable, _, _) in LINE_14.items(): states = [ fact - for record_id, fact in _facts(state_package).items() + for record_id, fact in _facts(state_package, year).items() if record_id.startswith(state_prefix) and record_id.endswith(f".all.{measure_id}") ] @@ -157,7 +160,7 @@ def test_the_state_facts_add_up_to_the_new_us_fact(year): if variable.startswith("A"): assert total == us, (year, measure_id) else: - assert abs(total - us) <= ROUNDING_PER_COUNT_CELL * 53, (year, measure_id) + assert abs(total - us) <= ROUNDING_PER_COUNT_CELL * 54, (year, measure_id) @pytest.mark.parametrize( From 4153ee7971bfadbd8b0f624d524d4361b251467d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Juaristi?= <127882282+juaristi22@users.noreply.github.com> Date: Thu, 3 Dec 2026 13:56:32 +0000 Subject: [PATCH 3/3] Pin the bundle's consumer row count at 2,416,409 The full-bundle test counts the rows of consumer_facts.jsonl separately from the summary and coverage fact_count pins, and that pin still held the base count. CI's bundle job on 39830e91 built 2,416,409 rows, the 44 new Historic Table 2 facts, and failed only there (tests/test_chronicle_bundle.py line 865; the summary counts above it passed). The row count now takes the same value as the other two pins. Co-Authored-By: Claude Opus 5.5 --- tests/test_chronicle_bundle.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_chronicle_bundle.py b/tests/test_chronicle_bundle.py index f9cf4a70..d06d025e 100644 --- a/tests/test_chronicle_bundle.py +++ b/tests/test_chronicle_bundle.py @@ -862,7 +862,8 @@ def test_build_bundle_writes_merged_consumer_contract(tmp_path): if row_count == 1: assert row["aggregate_fact_key"].startswith("ledger.aggregate_fact.v2:") assert row["semantic_fact_key"].startswith("ledger.semantic_fact.v2:") - assert row_count == 2416365 + # The same count as the summary's and the coverage's fact_count pins. + assert row_count == 2416409 assert provenance_classes <= { "administrative", "census",