8000 FIX Show a HTML repr for meta-estimatosr with invalid parameters (#24… · scikit-learn/scikit-learn@7675a6f · GitHub
[go: up one dir, main page]

Skip to content

Commit 7675a6f

Browse files
thomasjpfanjeremiedbb
authored andcommitted
FIX Show a HTML repr for meta-estimatosr with invalid parameters (#24015)
Co-authored-by: Jérémie du Boisberranger <34657725+jeremiedbb@users.noreply.github.com>
1 parent 2d3a2c2 commit 7675a6f

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

doc/whats_new/v1.1.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ Version 1.1.2
1212
Changelog
1313
---------
1414

15+
- |Fix| A default HTML representation is shown for meta-estimators with invalid
16+
parameters. :pr:`24015` by `Thomas Fan`_.
17+
1518
:mod:`sklearn.cluster`
1619
......................
1720

sklearn/utils/_estimator_html_repr.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from contextlib import closing
2-
from contextlib import suppress
32
from io import StringIO
43
from string import Template
54
import html
@@ -103,8 +102,16 @@ def _write_label_html(
103102

104103
def _get_visual_block(estimator):
105104
"""Generate information about how to display an estimator."""
106-
with suppress(AttributeError):
107-
return estimator._sk_visual_block_()
105+
if hasattr(estimator, "_sk_visual_block_"):
106+
try:
107+
return estimator._sk_visual_block_()
108+
except Exception:
109+
return _VisualBlock(
110+
"single",
111+
estimator,
112+
names=estimator.__class__.__name__,
113+
name_details=str(estimator),
114+
)
108115

109116
if isinstance(estimator, str):
110117
return _VisualBlock(

sklearn/utils/tests/test_estimator_html_repr.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,14 @@ def test_show_arrow_pipeline():
309309
'class="sk-toggleable__label sk-toggleable__label-arrow">Pipeline'
310310
in html_output
311311
)
312+
313+
314+
def test_invalid_parameters_in_stacking():
315+
"""Invalidate stacking configuration uses default repr.
316+
317+
Non-regression test for #24009.
318+
"""
319+
stacker = StackingClassifier(estimators=[])
320+
321+
html_output = estimator_html_repr(stacker)
322+
assert html.escape(str(stacker)) in html_output

0 commit comments

Comments
 (0)
0