Handle a None encoding in load_data when assertions are disabled - #979
Merged
jeongyoonlee merged 1 commit intoJul 31, 2026
Merged
Conversation
Under python -O the assertion in OneHotEncoder.transform is compiled out, so it returns None instead of raising and the except branch never runs. Calling .toarray() on that failed with AttributeError for the same constant-categorical input the assertion path already covers. Check the value as well as catching the assertion. Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
10 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.
Proposed changes
Follow-up to #978, taking @jeongyoonlee's note from that review.
OneHotEncoder.transformsignals "no column produced a non-empty encoding" with anassert, andload_datacatches it. Underpython -O(orPYTHONOPTIMIZE=1) that assertion is compiled out, sotransformreturnsNoneinstead of raising, theexceptnever runs, and.toarray()lands onNone:That is the same constant-categorical input the assertion path already handles, so it works normally and fails only when assertions are off.
The fix keeps
fit_transformin thetryand moves.toarray()out behind aNonecheck, so both signals converge on the same branch. No re-deriving of the encoder's own rule.Types of changes
Checklist
Further comments
The test monkeypatches
OneHotEncoder.fit_transformto returnNone, which reproduces the optimized-mode behavior deterministically without spawning a second interpreter. It fails on current master with theAttributeErrorabove and passes with this change.I also ran the real thing both ways. On master,
python -Oraises; with this patch it returns a(7, 1)array.pytest tests/test_features.pyis 7 passed andblack --checkis clean.