Skip to content

Extract shared program-statistics validation and build helpers#458

Merged
MaxGhenis merged 1 commit into
mainfrom
fix/263-shared-program-statistics
Jul 7, 2026
Merged

Extract shared program-statistics validation and build helpers#458
MaxGhenis merged 1 commit into
mainfrom
fix/263-shared-program-statistics

Conversation

@MaxGhenis

Copy link
Copy Markdown
Contributor

Fixes #263

What was duplicated

Near-identical program-statistics logic lived in both country analysis files:

  • _validate_program_statistics_configus/analysis.py:104 and uk/analysis.py:112 (identical but for the country word and US_PROGRAMS/UK_PROGRAMS)
  • _format_missing_program_variables and the config-error-message builder (_program_statistics_config_error_message / _uk_program_statistics_config_error_message) — identical but for the US/UK label
  • the program-statistics build loop inside each economic_impact_analysis (loop + dataframe + OutputCollection) — identical but for the programs dict

What moved where

Extracted into src/policyengine/outputs/program_statistics.py (re-exported from outputs/__init__.py):

  • validate_program_statistics_config(programs, baseline_simulation, reform_simulation, country_label)
  • build_program_statistics(programs, baseline_simulation, reform_simulation) -> OutputCollection[ProgramStatistics]

us/analysis.py and uk/analysis.py now delegate to these. US_PROGRAMS/UK_PROGRAMS stay in their country files, and each keeps a thin _validate_program_statistics_config(baseline, reform) wrapper that binds its programs dict and country label. The wrapper preserves the existing call site and the monkeypatch surface; the ProgramStatistics patch in test_cliff_impact_analysis.py and test_uk_analysis.py now targets the shared module, where the build loop moved.

Per the #263 re-scope (2026-07-08) and the PR #260 closing review, this intentionally does not introduce a strategy protocol, a unified cross-country result container, or any new public result classes — the country result containers deliberately diverge (UK wealth-decile outputs).

Behavior lock

Pure refactor, zero behavior change. tests/test_us_program_statistics.py and tests/test_uk_program_statistics.py pass unchanged. Added tests/test_program_statistics.py covering the shared validator's country-labelled error message.

Net line delta

8 files, +269 / -190 (net +79).

🤖 Generated with Claude Code

Deduplicate the near-identical program-statistics validation and build
logic shared by the US and UK economic_impact_analysis by moving it into
policyengine.outputs.program_statistics.

- Add validate_program_statistics_config(programs, baseline, reform,
  country_label) and build_program_statistics(programs, baseline, reform)
  to outputs/program_statistics.py, re-exported from outputs/__init__.py.
- Rewire us/analysis.py and uk/analysis.py to delegate to the shared
  helpers; US_PROGRAMS/UK_PROGRAMS stay in their country files and the
  thin _validate_program_statistics_config wrappers preserve the existing
  call/patch surface.
- Redirect the ProgramStatistics monkeypatch in test_cliff_impact_analysis.py
  and test_uk_analysis.py to the shared module (the build loop moved there).
- Add tests/test_program_statistics.py covering the shared validator's
  country-labelled error message.

Pure refactor with no behavior change; the existing US/UK program-statistics
tests pass unchanged.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@MaxGhenis MaxGhenis force-pushed the fix/263-shared-program-statistics branch from 1965420 to aefc62d Compare July 7, 2026 11:20
@MaxGhenis MaxGhenis marked this pull request as ready for review July 7, 2026 12:25
@MaxGhenis MaxGhenis merged commit e090c57 into main Jul 7, 2026
12 checks passed
MaxGhenis added a commit that referenced this pull request Jul 7, 2026
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consolidate economic impact analysis into shared compute functions

1 participant