Skip to content

Commit

Permalink
fix: Have shorter repr for display and longer str (#1154)
Browse files Browse the repository at this point in the history
closes #1150 

This changes proposed to have a conventional `repr` for display to avoid
to interfere too much with the output and have a richer `str`.
  • Loading branch information
glemaitre authored Jan 20, 2025
1 parent 4bd8f2f commit c43c8fe
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
8 changes: 7 additions & 1 deletion skore/src/skore/sklearn/_plot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def help(self):

console.print(self._create_help_panel())

def __repr__(self):
def __str__(self):
"""Return a string representation using rich."""
console = Console(file=StringIO(), force_terminal=False)
console.print(
Expand All @@ -92,6 +92,12 @@ def __repr__(self):
)
return console.file.getvalue()

def __repr__(self):
"""Return a string representation using rich."""
console = Console(file=StringIO(), force_terminal=False)
console.print(f"[cyan]skore.{self.__class__.__name__}(...)[/cyan]")
return console.file.getvalue()


class _ClassifierCurveDisplayMixin:
"""Mixin class to be used in Displays requiring a binary classifier.
Expand Down
28 changes: 26 additions & 2 deletions skore/tests/unit/sklearn/plot/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,31 @@ def test_display_help(pyplot, capsys, plot_func, estimator, dataset):
assert f"{display.__class__.__name__}" in captured.out


@pytest.mark.parametrize(
"plot_func, estimator, dataset",
[
("roc", LogisticRegression(), make_classification(random_state=42)),
(
"precision_recall",
LogisticRegression(),
make_classification(random_state=42),
),
("prediction_error", LinearRegression(), make_regression(random_state=42)),
],
)
def test_display_str(pyplot, plot_func, estimator, dataset):
"""Check that __str__ returns a string starting with the expected prefix."""
X_train, X_test, y_train, y_test = train_test_split(*dataset, random_state=42)
report = EstimatorReport(
estimator, X_train=X_train, y_train=y_train, X_test=X_test, y_test=y_test
)
display = getattr(report.metrics.plot, plot_func)()

str_str = str(display)
assert f"{display.__class__.__name__}" in str_str
assert "display.help()" in str_str


@pytest.mark.parametrize(
"plot_func, estimator, dataset",
[
Expand All @@ -52,8 +77,7 @@ def test_display_repr(pyplot, plot_func, estimator, dataset):
display = getattr(report.metrics.plot, plot_func)()

repr_str = repr(display)
assert f"{display.__class__.__name__}" in repr_str
assert "display.help()" in repr_str
assert f"skore.{display.__class__.__name__}(...)" in repr_str


@pytest.mark.parametrize(
Expand Down

0 comments on commit c43c8fe

Please sign in to comment.