Skip to content

Commit

Permalink
chore: Differentiate __repr__ and help for report and accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
glemaitre committed Jan 14, 2025
1 parent ec67dbb commit 0915f32
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
11 changes: 9 additions & 2 deletions skore/src/skore/sklearn/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,17 @@ def help(self):

console.print(self._create_help_panel())

def __repr__(self):
def _rich_repr(self, class_name, help_method_name):
"""Return a string representation using rich."""
console = Console(file=StringIO(), force_terminal=False)
console.print(self._create_help_panel())
console.print(
Panel(
f"Get guidance using the {help_method_name} method",
title=f"[cyan]{class_name}[/cyan]",
border_style="orange1",
expand=False,
)
)
return console.file.getvalue()


Expand Down
14 changes: 14 additions & 0 deletions skore/src/skore/sklearn/_estimator/metrics_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,13 @@ def _get_help_legend(self):
def _get_help_tree_title(self):
return "[bold cyan]reporter.metrics[/bold cyan]"

def __repr__(self):
"""Return a string representation using rich."""
return self._rich_repr(
class_name="skore.EstimatorReport.metrics",
help_method_name="reporter.metrics.help()",
)


########################################################################################
# Sub-accessors
Expand Down Expand Up @@ -1096,3 +1103,10 @@ def _get_help_panel_title(self):

def _get_help_tree_title(self):
return "[bold cyan]reporter.metrics.plot[/bold cyan]"

def __repr__(self):
"""Return a string representation using rich."""
return self._rich_repr(
class_name="skore.EstimatorReport.metrics.plot",
help_method_name="reporter.metrics.plot.help()",
)
6 changes: 6 additions & 0 deletions skore/src/skore/sklearn/_estimator/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,9 @@ def estimator_name(self):
else:
name = self._estimator.__class__.__name__
return name

def __repr__(self):
"""Return a string representation using rich."""
return self._rich_repr(
class_name="skore.EstimatorReport", help_method_name="reporter.help()"
)
6 changes: 3 additions & 3 deletions skore/tests/unit/sklearn/test_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def test_estimator_report_repr(binary_classification_data):
report = EstimatorReport(estimator, X_test=X_test, y_test=y_test)

repr_str = repr(report)
assert f"📓 Tools to diagnose estimator {estimator.__class__.__name__}" in repr_str
assert "skore.EstimatorReport" in repr_str


@pytest.mark.parametrize(
Expand Down Expand Up @@ -344,7 +344,7 @@ def test_estimator_report_plot_repr(binary_classification_data):
report = EstimatorReport(estimator, X_test=X_test, y_test=y_test)

repr_str = repr(report.metrics.plot)
assert "🎨 Available plot methods" in repr_str
assert "skore.EstimatorReport.metrics.plot" in repr_str


def test_estimator_report_plot_roc(binary_classification_data):
Expand Down Expand Up @@ -483,7 +483,7 @@ def test_estimator_report_metrics_repr(binary_classification_data):
report = EstimatorReport(estimator, X_test=X_test, y_test=y_test)

repr_str = repr(report.metrics)
assert "📏 Available metrics methods" in repr_str
assert "skore.EstimatorReport.metrics" in repr_str


@pytest.mark.parametrize(
Expand Down

0 comments on commit 0915f32

Please sign in to comment.