Fix silent data corruption for invalid enum values#411
Merged
Conversation
2 tasks
Change the behavior from logging a warning and returning index 0 to raising a ValueError with a clear message. This prevents silent data corruption when incorrect enum values are passed to simulations. The previous behavior (introduced in the searchsorted refactor) would: 1. Log a warning about invalid values 2. Return 0 (first enum value) for the invalid entries 3. Continue execution with corrupted data This was an improvement over the original np.select behavior (which silently returned 0 without any warning), but still allowed simulations to run with incorrect data. Now invalid enum values will raise: ValueError: Invalid value(s) ['MARRIED'] for enum FilingStatus. Valid values are: ['SINGLE', 'JOINT', 'SEPARATE', ...] Fixes #410 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
6fec490 to
c01e0f9
Compare
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.
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