Skip to content

Handle a None encoding in load_data when assertions are disabled - #979

Merged
jeongyoonlee merged 1 commit into
uber:masterfrom
arpitjain099:fix/load-data-optimized-mode
Jul 31, 2026
Merged

Handle a None encoding in load_data when assertions are disabled#979
jeongyoonlee merged 1 commit into
uber:masterfrom
arpitjain099:fix/load-data-optimized-mode

Conversation

@arpitjain099

Copy link
Copy Markdown
Contributor

Proposed changes

Follow-up to #978, taking @jeongyoonlee's note from that review.

OneHotEncoder.transform signals "no column produced a non-empty encoding" with an assert, and load_data catches it. Under python -O (or PYTHONOPTIMIZE=1) that assertion is compiled out, so transform returns None instead of raising, the except never runs, and .toarray() lands on None:

$ python -O -c "from causalml.features import load_data; ..."
AttributeError: 'NoneType' object has no attribute 'toarray'

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_transform in the try and moves .toarray() out behind a None check, so both signals converge on the same branch. No re-deriving of the encoder's own rule.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update (if none of the other choices apply)

Checklist

  • I have read the CONTRIBUTING doc
  • I have signed the CLA
  • Lint and unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • Any dependent changes have been merged and published in downstream modules

Further comments

The test monkeypatches OneHotEncoder.fit_transform to return None, which reproduces the optimized-mode behavior deterministically without spawning a second interpreter. It fails on current master with the AttributeError above and passes with this change.

I also ran the real thing both ways. On master, python -O raises; with this patch it returns a (7, 1) array. pytest tests/test_features.py is 7 passed and black --check is clean.

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>

@jeongyoonlee jeongyoonlee left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jeongyoonlee
jeongyoonlee merged commit 08a035b into uber:master Jul 31, 2026
7 checks passed
@jeongyoonlee jeongyoonlee added the enhancement New feature or request label Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants