Extract shared program-statistics validation and build helpers#458
Merged
Conversation
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>
1965420 to
aefc62d
Compare
MaxGhenis
added a commit
that referenced
this pull request
Jul 7, 2026
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #263
What was duplicated
Near-identical program-statistics logic lived in both country analysis files:
_validate_program_statistics_config—us/analysis.py:104anduk/analysis.py:112(identical but for the country word andUS_PROGRAMS/UK_PROGRAMS)_format_missing_program_variablesand the config-error-message builder (_program_statistics_config_error_message/_uk_program_statistics_config_error_message) — identical but for theUS/UKlabeleconomic_impact_analysis(loop + dataframe +OutputCollection) — identical but for the programs dictWhat moved where
Extracted into
src/policyengine/outputs/program_statistics.py(re-exported fromoutputs/__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.pyanduk/analysis.pynow delegate to these.US_PROGRAMS/UK_PROGRAMSstay 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 themonkeypatchsurface; theProgramStatisticspatch intest_cliff_impact_analysis.pyandtest_uk_analysis.pynow 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.pyandtests/test_uk_program_statistics.pypass unchanged. Addedtests/test_program_statistics.pycovering the shared validator's country-labelled error message.Net line delta
8 files, +269 / -190 (net +79).
🤖 Generated with Claude Code