Skip to content

Support scikit-learn 1.9 (DTYPE removed from sklearn.tree._tree)#152

Merged
reidjohnson merged 1 commit into
zillow:mainfrom
MaxGhenis:fix-sklearn-1.9-dtype
Jun 10, 2026
Merged

Support scikit-learn 1.9 (DTYPE removed from sklearn.tree._tree)#152
reidjohnson merged 1 commit into
zillow:mainfrom
MaxGhenis:fix-sklearn-1.9-dtype

Conversation

@MaxGhenis

Copy link
Copy Markdown
Contributor

Problem

import quantile_forest raises ImportError on scikit-learn 1.9:

ImportError: cannot import name 'DTYPE' from 'sklearn.tree._tree'

scikit-learn 1.9 no longer re-exports DTYPE from sklearn.tree._tree. quantile_forest/_quantile_forest.py imports it at module load, so the whole package fails to import under scikit-learn ≥ 1.9.

Fix

DTYPE has always been np.float32, and it is used here only as the coercion dtype passed to validate_data/check_X_y (the two "dtype": DTYPE call sites). So guard the import with a fallback to np.float32:

try:
    from sklearn.tree._tree import DTYPE
except ImportError:  # scikit-learn >= 1.9 no longer re-exports DTYPE here
    DTYPE = np.float32  # the value DTYPE has always held
  • Identical value — no behavior change on any scikit-learn version.
  • Backward compatible — older scikit-learn still imports the real symbol.
  • Forward compatible — verified import quantile_forest-equivalent against scikit-learn 1.9.0: the old import raises, the fallback yields np.float32.
  • Pure-Python, single line region; numpy as np is already imported in this module.

Verification

$ python -c "import sklearn; print(sklearn.__version__)"
1.9.0
$ python -c "from sklearn.tree._tree import DTYPE"
ImportError: cannot import name 'DTYPE' from 'sklearn.tree._tree'   # before
# after this change: import succeeds, DTYPE == np.float32

Happy to add a CI entry pinning the newest scikit-learn if that's useful.

scikit-learn 1.9 no longer re-exports DTYPE from sklearn.tree._tree, so
`import quantile_forest` raises ImportError on sklearn>=1.9. DTYPE has
always been np.float32 (it is used here only as the coercion dtype for
check_X_y at the two validate_data calls), so guard the import with a
fallback to np.float32 — identical value, backward compatible with older
scikit-learn, forward compatible with 1.9+.
@codecov

codecov Bot commented Jun 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 50.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 98.55%. Comparing base (e193836) to head (5b19a86).

Files with missing lines Patch % Lines
quantile_forest/_quantile_forest.py 50.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #152      +/-   ##
==========================================
- Coverage   98.72%   98.55%   -0.17%     
==========================================
  Files           5        5              
  Lines        1175     1178       +3     
==========================================
+ Hits         1160     1161       +1     
- Misses         15       17       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@reidjohnson

Copy link
Copy Markdown
Member

LGTM. Thank you for the contribution, @MaxGhenis!

@reidjohnson reidjohnson merged commit 9e8240b into zillow:main Jun 10, 2026
20 of 22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants