Fix silent data corruption for invalid enum values #411
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.
Summary
Invalid enum values passed to
Enum.encode()were silently converted to index 0 (the first enum value) instead of raising an error. This caused silent data corruption in simulations.The Problem
In
Enum.encode(),numpy.select()is used to map string values to enum indices:When no condition matches (invalid value),
numpy.selectreturns its default value of0, which silently converts invalid values to the first enum item.For example:
'MARRIED'passed toFilingStatus(which hasSINGLE,JOINT,SEPARATE) would silently becomeSINGLE(index 0)''would also silently become the first enum valueReal-World Impact
This bug was discovered in PolicyEngine/policyengine-us#6901 where Missouri test files had:
filing_status: MARRIED(invalid - should beJOINT)filing_status:(empty/blank)These tests passed silently because the invalid values were converted to
SINGLE.The Fix
Add validation after
numpy.selectto detect unmatched values and raise a descriptive error:Test Plan
tests/core/test_enum_encoding.pywith tests for:Fixes #410
🤖 Generated with Claude Code