Derive program-statistics entity from model metadata (refs #326)#334
Draft
vahid-ahmadi wants to merge 1 commit intomainfrom
Draft
Derive program-statistics entity from model metadata (refs #326)#334vahid-ahmadi wants to merge 1 commit intomainfrom
vahid-ahmadi wants to merge 1 commit intomainfrom
Conversation
Refs #326 Replace the hard-coded `programs = {...}` dict in `economic_impact_analysis` with a structured `ProgramSpec` list and a `resolve_program_specs(specs, model_version)` helper that: - Validates every variable name against the model up front - Derives `entity` from the variable's own metadata (no more duplicated entity strings that drift silently when a variable moves between entities upstream) - Collects every unknown variable into a single `ValueError` with difflib suggestions, instead of failing one-at-a-time deep inside an `Aggregate.run()` lookup This is a concrete first step toward the durable program-statistics mapping tracked in #326. It removes the entity-drift class of bug; deriving the program list itself from model metadata (e.g. by scanning for variables tagged as program aggregates) is left for a follow-up. Note: this PR overlaps with #327 (Anthony's fix for the immediate StopIteration crash in #325). When either lands first the other will need a small rebase. The variable name corrections from #327 (payroll_tax -> employee_payroll_tax, medicare -> medicare_cost, state_income_tax -> household_state_income_tax) are also applied here so the new validation passes against the current US model. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Refs #326. Draft — see scope notes and overlap with #327 below.
Summary
Replaces the hard-coded
programs = {...}dict ineconomic_impact_analysis(US) with a structuredProgramSpeclist and aresolve_program_specs(specs, model_version)helper that:policyengine-usmoves a variable between entities.difflibsuggestions, instead of failing one-at-a-time deep inside anAggregate.run()next(...)lookup.This is a concrete first step toward the durable program-statistics mapping tracked in #326. Deriving the list itself from model metadata (e.g. scanning for variables tagged as program aggregates) is left for a follow-up.
Files
src/policyengine/tax_benefit_models/us/programs.py(new) —ProgramSpec,ResolvedProgram,US_PROGRAM_SPECS,resolve_program_specs().src/policyengine/tax_benefit_models/us/analysis.py— uses the new helper.tests/test_program_specs.py(new) — 4 unit tests covering entity derivation, multi-error collection, fuzzy suggestions, and de-duplication.changelog.d/program-specs-metadata.changed.md.Overlap with #327
This PR overlaps with @anth-volk's #327 (the immediate fix for the
StopIterationcrash in #325). The variable name corrections from #327 (payroll_tax→employee_payroll_tax,medicare→medicare_cost,state_income_tax→household_state_income_tax) are applied here too so the new validation passes against the current US model.Whichever lands first, the other needs a small rebase. Marked draft for that reason.
Test plan
pytest tests/test_program_specs.pypytest tests/test_aggregate.py tests/test_change_aggregate.pystill passes.examples/us_budgetary_impact.pyand confirmeconomic_impact_analysisproduces the program-by-program table without the originalStopIteration.🤖 Generated with Claude Code