Skip to content

fix: Bypass deprecated feature_dtypes_ setter in fit() - #1013

Merged
Marc-Antoine Schmidt (MarcAntoineSchmidtQC) merged 2 commits into
mainfrom
fix/warnings
Jul 9, 2026
Merged

fix: Bypass deprecated feature_dtypes_ setter in fit()#1013
Marc-Antoine Schmidt (MarcAntoineSchmidtQC) merged 2 commits into
mainfrom
fix/warnings

Conversation

@jtilly

@jtilly Jan Tilly (jtilly) commented Jul 8, 2026

Copy link
Copy Markdown
Member

Two changes:

  • Instead of catching the warning, I'm assigning directly to _feature_dtypes_ instead of going through the deprecated feature_dtypes_ setter, eliminating the DeprecationWarning emitted on every fit() call when X is a DataFrame.
  • I'm removing the deprecation warning from the feature_dtypes_ getter, because that's also firing for things such as dir(...) and other introspection calls.
import warnings
import pandas as pd
from glum import GeneralizedLinearRegressor
warnings.filterwarnings("error", message="Use `categorical_levels_` instead.")

X = pd.DataFrame({"a": [1.0, 2.0, 3.0], "b": [4.0, 5.0, 6.0]})
y = [1.0, 2.0, 3.0]
model = GeneralizedLinearRegressor().fit(X, y)

dir(model)
/tmp via py v3.12.13 ❯ pixi exec --spec glum python reproducer.py
Traceback (most recent call last):
  File "/private/tmp/reproducer.py", line 10, in <module>
    dir(model)
    ~~~^^^^^^^
  File "/tmp/rattler/cache/cached-envs-v0/glum-12c16ede7e8029a1/lib/python3.14/site-packages/sklearn/base.py", line 214, in __dir__
    return [attr for attr in super().__dir__() if hasattr(self, attr)]
                                                  ~~~~~~~^^^^^^^^^^^^
  File "/tmp/rattler/cache/cached-envs-v0/glum-12c16ede7e8029a1/lib/python3.14/_py_warnings.py", line 799, in wrapper
    _wm.warn(msg, category=category, stacklevel=stacklevel + 1)
    ~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
DeprecationWarning: Use `categorical_levels_` instead.

Assign directly to _feature_dtypes_ to avoid triggering the deprecation warning
on every fit() call when X is a DataFrame.
The getter is triggered by sklearn's __dir__ override, which calls hasattr()
on every attribute of every fitted estimator. Keeping the deprecation warning
on the getter floods users with warnings they cannot act on. The setter
deprecation is retained to discourage external writes to feature_dtypes_.
@MarcAntoineSchmidtQC
Marc-Antoine Schmidt (MarcAntoineSchmidtQC) merged commit ca84caa into main Jul 9, 2026
26 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