Validate US household state_code_str instead of silently defaulting to CA#50
Merged
Merged
Conversation
…o CA PolicyEngine US takes the state as a household-level `state_code_str` and silently defaults to California when it is absent or given under a wrong key. A caller passing `state_code` (the person-level-looking name) got a plausible but wrong CA answer -- $14,270 of state income tax on a $200k single filer that should be $0 in TX. The state itself was always wired through correctly; the failure mode was the silent fallback. Now reject a wrong key or an invalid code with an actionable message, normalise case, and document the CA default in the tool docstring. Tests: CA-vs-TX regression (TX must be 0, CA must differ), wrong-key and invalid-code rejection, case normalisation. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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.
Investigation
Reported symptom:
calculate_householdreturned identical US results withstate_code,state_code_str, or neither — including a non-zero state income tax.Reproduced against the hosted server (single filer, $200k employment income, 2026):
{"state_code_str": "CA"}{"state_code_str": "TX"}{"state_code": "CA"}(wrong key)The state field is NOT ignored — CA and TX differ correctly (net income 132,036 vs 148,927). Our layer forwards
householdtope.us.calculate_householdat the correct entity level, and there is no caching.Root cause
PolicyEngine US defaults to CA when no state is supplied. The original test passed the state under the wrong key / at the top level, where it was silently dropped — so all three calls fell back to the CA default and looked identical, with a plausible non-zero state tax masking the problem.
Not a wiring bug, but a real silent-failure bug: a caller who mistypes the key gets a confidently wrong answer for 49 states rather than an error.
Fix
integration/src/policyengine_macro/core.py— new_validate_us_household, called from_pe_runbefore the PolicyEngine import:state_codeinstead ofstate_code_str→ValueErrornaming the correct keyValueErrormcp_server.pydocstring now states the CA default explicitlyTests
integration/tests/test_core.py:test_pe_household_us_state_code_changes_state_income_tax(slow) — CA vs TX must differ, TX must be exactly 0Fast suite: 104 passed.
test_summary_parsesfails on this branch and onmainalike — pre-existing and unrelated.🤖 Generated with Claude Code