Skip to content

Commit 07b0a42

Browse files
NicolasHugjnothman
authored andcommitted
FIX builtin PrettyPrinter usage on estimators (#12938)
1 parent 47f66a3 commit 07b0a42

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

sklearn/utils/_pprint.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,10 @@ def _pprint_key_val_tuple(self, object, stream, indent, allowance, context,
321321
self._format(v, stream, indent + len(rep) + len(middle), allowance,
322322
context, level)
323323

324-
_dispatch = pprint.PrettyPrinter._dispatch
324+
# Note: need to copy _dispatch to prevent instances of the builtin
325+
# PrettyPrinter class to call methods of _EstimatorPrettyPrinter (see issue
326+
# 12906)
327+
_dispatch = pprint.PrettyPrinter._dispatch.copy()
325328
_dispatch[BaseEstimator.__repr__] = _pprint_estimator
326329
_dispatch[KeyValTuple.__repr__] = _pprint_key_val_tuple
327330

sklearn/utils/tests/test_pprint.py

+9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
from pprint import PrettyPrinter
23

34
from sklearn.utils._pprint import _EstimatorPrettyPrinter
45
from sklearn.pipeline import make_pipeline, Pipeline
@@ -311,3 +312,11 @@ def test_length_constraint():
311312
vectorizer = CountVectorizer(vocabulary=vocabulary)
312313
repr_ = vectorizer.__repr__()
313314
assert '...' in repr_
315+
316+
317+
def test_builtin_prettyprinter():
318+
# non regression test than ensures we can still use the builtin
319+
# PrettyPrinter class for estimators (as done e.g. by joblib).
320+
# Used to be a bug
321+
322+
PrettyPrinter().pprint(LogisticRegression())

0 commit comments

Comments
 (0)