Repository navigation
Description
Introduce yourself
I use scikit-learn for classical ML pipelines, and a good part of that is the incremental-learning API: we feed data in batches and chain the calls, e.g. est.partial_fit(X_batch, y_batch).predict(X_batch). I found the two cases below while auditing every partial_fit implementation in the library against its own documented Returns section.
Describe the bug and give evidence about its user-facing impact
partial_fit is documented (and, in this library, uniformly implemented) as returning self, so that it can be chained like fit. Two implementations return None instead, while their own docstrings still promise self:
MultiOutputRegressor.partial_fit— the override just calls the parent's implementation without returning it (sklearn/multioutput.py:430), even though its docstring documentsself(sklearn/multioutput.py:425-428). Its siblingMultiOutputClassifierinherits_MultiOutputEstimator.partial_fit, which doesreturn self(sklearn/multioutput.py:203) — so within the same class hierarchy one of the two subclasses returns the estimator and the other returnsNone.BernoulliRBM.partial_fit— ends afterself._fit(...)(sklearn/neural_network/_rbm.py:310), although its docstring documentsself(sklearn/neural_network/_rbm.py:282-285) andBernoulliRBM.fitdoesreturn self(sklearn/neural_network/_rbm.py:437).
User-facing impact. The documented chaining idiom raises AttributeError: 'NoneType' object has no attribute ..., which does not point at the real problem. Worse, the natural incremental-training loop
est = MultiOutputRegressor(SGDRegressor())
for X_batch, y_batch in batches:
est = est.partial_fit(X_batch, y_batch) # silently becomes Nonesilently replaces the estimator with None and only fails at the next batch, far from the cause. This is especially confusing for people who write the same loop generically over several estimators, since it works for every other incremental estimator in scikit-learn (including MultiOutputClassifier) and fails only for these two.
Scope of the audit. I checked all 24 partial_fit definitions under sklearn/: 21 end with return self, one (CategoricalNB) returns the result of super().partial_fit(...), and these two are the only ones that return None.
Why the test suite does not catch it. sklearn/tests/test_multioutput.py:78 (test_multi_target_regression_partial_fit) does exercise MultiOutputRegressor.partial_fit, but it only checks the predictions afterwards, never the return value, so a None return passes unnoticed.
Steps/Code to Reproduce
import numpy as np
from sklearn.linear_model import SGDClassifier, SGDRegressor
from sklearn.multioutput import MultiOutputClassifier, MultiOutputRegressor
from sklearn.neural_network import BernoulliRBM
rng = np.random.RandomState(0)
X = rng.rand(20, 4)
y = rng.rand(20, 2)
# 1. both return None
print(MultiOutputRegressor(SGDRegressor(random_state=0)).partial_fit(X, y))
print(BernoulliRBM(n_components=3, random_state=0).partial_fit(X))
# 2. the classifier sibling returns the estimator, as documented
print(
MultiOutputClassifier(SGDClassifier(random_state=0)).partial_fit(
X, (y > 0.5).astype(int), classes=[np.array([0, 1])] * 2
)
)
# 3. the documented chaining idiom breaks
MultiOutputRegressor(SGDRegressor(random_state=0)).partial_fit(X, y).predict(X)
BernoulliRBM(n_components=3, random_state=0).partial_fit(X).transform(X)Expected Results
Both calls return the fitted estimator, i.e. partial_fit(...) is est, and the two chained expressions of step 3 return an array of shape (20, 2) and (20, 3) respectively — consistent with the docstrings and with fit.
Actual Results
None
None
MultiOutputClassifier(estimator=SGDClassifier(random_state=0))
Traceback (most recent call last):
...
AttributeError: 'NoneType' object has no attribute 'predict'Traceback (most recent call last):
...
AttributeError: 'NoneType' object has no attribute 'transform'Versions
System:
python: 3.13.12 (main, Feb 4 2026, 00:05:32) [Clang 21.1.4 ]
machine: macOS-14.5-arm64-arm-64bit-Mach-O
Python dependencies:
sklearn: 1.10.dev0 (editable build of main @ 6e37063)
pip: 26.3
numpy: 2.5.4
scipy: 1.18.1
Cython: 3.3.1
joblib: 1.6.0
threadpoolctl: 3.8.0
Built with OpenMP: FalseInterest in fixing the bug
Yes, I would like to open the PR for this once the issue is triaged — no PR before that, as requested.
Root cause. Both are overrides/rewrites of an existing partial_fit that dropped the return value, while the docstring was copied over unchanged:
sklearn/multioutput.py:430:super().partial_fit(...)→ needsreturn super().partial_fit(...). The parent already returns the right object, so no logic is duplicated.sklearn/neural_network/_rbm.py:310: the method ends afterself._fit(...)→ needsreturn self.
Proposed fix (2 lines of production code). Exactly the two changes above, plus one non-regression test per module. Each test also asserts the sibling that already works (MultiOutputClassifier.partial_fit / BernoulliRBM.fit), so that the failing assertion is attributable to the fix and not to the test scaffolding.
Evidence from my local experiment (branch fix/partial-fit-return-self on main @ 6e37063):
- on
main, the two new tests fail with exactlyassert None is MultiOutputRegressor(estimator=SGDRegressor(max_iter=5, random_state=0))andassert None is BernoulliRBM(n_components=16, random_state=0); - with the fix, the two target files go from
96 passed(base) to98 passed; pytest sklearn/tests/test_common.py -k "BernoulliRBM or MultiOutputRegressor": 106 passed, 13 skipped, 2 xfailed;ruff check/ruff format --check(0.12.2, the version pinned in.pre-commit-config.yaml) clean on the four touched files.
The change is strictly backward compatible for callers: None is not a usable value, and no code in sklearn/ consumes the return value of either method.
Disclosure: I used an AI coding assistant to help scan the code base and run the experiments. I reproduced, verified and wrote the analysis above myself on a local editable build of main @ 6e37063.
Activity