Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f5e981e

Browse filesBrowse files
FIX improve doc and test for HTMLDocumentationLinkMixin (#29774)
Co-authored-by: Adrin Jalali <adrin.jalali@gmail.com>
1 parent 7a50457 commit f5e981e
Copy full SHA for f5e981e

File tree

Expand file treeCollapse file tree

2 files changed

+73
-11
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+73
-11
lines changed

‎sklearn/utils/_estimator_html_repr.py

Copy file name to clipboardExpand all lines: sklearn/utils/_estimator_html_repr.py
+22-8Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -427,15 +427,31 @@ class _HTMLDocumentationLinkMixin:
427427
Examples
428428
--------
429429
If the default values for `_doc_link_module`, `_doc_link_template` are not suitable,
430-
then you can override them:
430+
then you can override them and provide a method to generate the URL parameters:
431431
>>> from sklearn.base import BaseEstimator
432-
>>> estimator = BaseEstimator()
433-
>>> estimator._doc_link_template = "https://website.com/{single_param}.html"
432+
>>> doc_link_template = "https://address.local/{single_param}.html"
434433
>>> def url_param_generator(estimator):
435434
... return {"single_param": estimator.__class__.__name__}
436-
>>> estimator._doc_link_url_param_generator = url_param_generator
435+
>>> class MyEstimator(BaseEstimator):
436+
... # use "builtins" since it is the associated module when declaring
437+
... # the class in a docstring
438+
... _doc_link_module = "builtins"
439+
... _doc_link_template = doc_link_template
440+
... _doc_link_url_param_generator = url_param_generator
441+
>>> estimator = MyEstimator()
437442
>>> estimator._get_doc_link()
438-
'https://website.com/BaseEstimator.html'
443+
'https://address.local/MyEstimator.html'
444+
445+
If instead of overriding the attributes inside the class definition, you want to
446+
override a class instance, you can use `types.MethodType` to bind the method to the
447+
instance:
448+
>>> import types
449+
>>> estimator = BaseEstimator()
450+
>>> estimator._doc_link_template = doc_link_template
451+
>>> estimator._doc_link_url_param_generator = types.MethodType(
452+
... url_param_generator, estimator)
453+
>>> estimator._get_doc_link()
454+
'https://address.local/BaseEstimator.html'
439455
"""
440456

441457
_doc_link_module = "sklearn"
@@ -491,6 +507,4 @@ def _get_doc_link(self):
491507
return self._doc_link_template.format(
492508
estimator_module=estimator_module, estimator_name=estimator_name
493509
)
494-
return self._doc_link_template.format(
495-
**self._doc_link_url_param_generator(self)
496-
)
510+
return self._doc_link_template.format(**self._doc_link_url_param_generator())

‎sklearn/utils/tests/test_estimator_html_repr.py

Copy file name to clipboardExpand all lines: sklearn/utils/tests/test_estimator_html_repr.py
+51-3Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import html
22
import locale
33
import re
4+
import types
45
from contextlib import closing
56
from io import StringIO
67
from unittest.mock import patch
@@ -443,7 +444,9 @@ def test_html_documentation_link_mixin_sklearn(mock_version):
443444
("prefix.mypackage.mymodule.submodule", "prefix.mypackage.mymodule.submodule"),
444445
],
445446
)
446-
def test_html_documentation_link_mixin_get_doc_link(module_path, expected_module):
447+
def test_html_documentation_link_mixin_get_doc_link_instance(
448+
module_path, expected_module
449+
):
447450
"""Check the behaviour of the `_get_doc_link` with various parameter."""
448451

449452
class FooBar(_HTMLDocumentationLinkMixin):
@@ -459,6 +462,32 @@ class FooBar(_HTMLDocumentationLinkMixin):
459462
assert est._get_doc_link() == f"https://website.com/{expected_module}.FooBar.html"
460463

461464

465+
@pytest.mark.parametrize(
466+
"module_path,expected_module",
467+
[
468+
("prefix.mymodule", "prefix.mymodule"),
469+
("prefix._mymodule", "prefix"),
470+
("prefix.mypackage._mymodule", "prefix.mypackage"),
471+
("prefix.mypackage._mymodule.submodule", "prefix.mypackage"),
472+
("prefix.mypackage.mymodule.submodule", "prefix.mypackage.mymodule.submodule"),
473+
],
474+
)
475+
def test_html_documentation_link_mixin_get_doc_link_class(module_path, expected_module):
476+
"""Check the behaviour of the `_get_doc_link` when `_doc_link_module` and
477+
`_doc_link_template` are defined at the class level and not at the instance
478+
level."""
479+
480+
class FooBar(_HTMLDocumentationLinkMixin):
481+
_doc_link_module = "prefix"
482+
_doc_link_template = (
483+
"https://website.com/{estimator_module}.{estimator_name}.html"
484+
)
485+
486+
FooBar.__module__ = module_path
487+
est = FooBar()
488+
assert est._get_doc_link() == f"https://website.com/{expected_module}.FooBar.html"
489+
490+
462491
def test_html_documentation_link_mixin_get_doc_link_out_of_library():
463492
"""Check the behaviour of the `_get_doc_link` with various parameter."""
464493
mixin = _HTMLDocumentationLinkMixin()
@@ -469,7 +498,7 @@ def test_html_documentation_link_mixin_get_doc_link_out_of_library():
469498
assert mixin._get_doc_link() == ""
470499

471500

472-
def test_html_documentation_link_mixin_doc_link_url_param_generator():
501+
def test_html_documentation_link_mixin_doc_link_url_param_generator_instance():
473502
mixin = _HTMLDocumentationLinkMixin()
474503
# we can bypass the generation by providing our own callable
475504
mixin._doc_link_template = (
@@ -482,11 +511,30 @@ def url_param_generator(estimator):
482511
"another_variable": "value_2",
483512
}
484513

485-
mixin._doc_link_url_param_generator = url_param_generator
514+
mixin._doc_link_url_param_generator = types.MethodType(url_param_generator, mixin)
486515

487516
assert mixin._get_doc_link() == "https://website.com/value_1.value_2.html"
488517

489518

519+
def test_html_documentation_link_mixin_doc_link_url_param_generator_class():
520+
# we can bypass the generation by providing our own callable
521+
522+
def url_param_generator(estimator):
523+
return {
524+
"my_own_variable": "value_1",
525+
"another_variable": "value_2",
526+
}
527+
528+
class FooBar(_HTMLDocumentationLinkMixin):
529+
_doc_link_template = (
530+
"https://website.com/{my_own_variable}.{another_variable}.html"
531+
)
532+
_doc_link_url_param_generator = url_param_generator
533+
534+
estimator = FooBar()
535+
assert estimator._get_doc_link() == "https://website.com/value_1.value_2.html"
536+
537+
490538
@pytest.fixture
491539
def set_non_utf8_locale():
492540
"""Pytest fixture to set non utf-8 locale during the test.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.