`_ that should not be confused with HTML tags.
b : str
"""
__module__ = "sklearn.mock_module"
__qualname__ = "MockEstimator"
params = ParamsDict(
params={"a": 1, "b": "value"},
non_default=("a",),
estimator_class=MockEstimator,
doc_link="mock_module.MockEstimator.html",
)
html_output = _params_html_repr(params)
html_param_a = (
r''
r'\s*'
r"\s*a"
r'\s*\s*a:'
r"\sint
"
r"Description of a which can include `<formatted text "
r"https://example.com>`_ that should not be confused with HTML tags."
r"\s*"
r"\s* | "
)
assert re.search(html_param_a, html_output, flags=re.DOTALL)
html_param_b = (
r''
r'.*'
r"\s*b"
r'\s*\s*b:'
r"\sstr
"
r"\s*"
r"\s* | "
)
assert re.search(html_param_b, html_output, flags=re.DOTALL)
def test_params_html_repr_without_doc_links():
"""Test `_params_html_repr` when `link_to_param_doc` returns None."""
class MockEstimatorWithoutDoc:
__module__ = "sklearn.mock_module"
__qualname__ = "MockEstimatorWithoutDoc"
# No docstring defined on this test class.
params = ParamsDict(
params={"a": 1, "b": "value"},
non_default=("a",),
estimator_class=MockEstimatorWithoutDoc,
)
html_output = _params_html_repr(params)
# Check that no doc links are generated
assert "?" not in html_output
assert "Click to access" not in html_output
html_param_a = (
r'a | '
r'\s*1 | '
)
assert re.search(html_param_a, html_output, flags=re.DOTALL)
html_param_b = (
r'b | '
r'\s*'value' | '
)
assert re.search(html_param_b, html_output, flags=re.DOTALL)
def test_generate_link_to_param_doc_basic():
"""Return anchor URLs for documented parameters in the estimator."""
class MockEstimator:
"""Mock class.
Parameters
----------
alpha : float
Regularization strength.
beta : int
Some integer parameter.
"""
doc_link = "mock_module.MockEstimator.html"
url = generate_link_to_param_doc(MockEstimator, "alpha", doc_link)
assert url == "mock_module.MockEstimator.html#:~:text=alpha,-float"
url = generate_link_to_param_doc(MockEstimator, "beta", doc_link)
assert url == "mock_module.MockEstimator.html#:~:text=beta,-int"
def test_generate_link_to_param_doc_param_not_found():
"""Ensure None is returned when the parameter is not documented."""
class MockEstimator:
"""Mock class
Parameters
----------
alpha : float
Regularization strength.
"""
doc_link = "mock_module.MockEstimator.html"
url = generate_link_to_param_doc(MockEstimator, "gamma", doc_link)
assert url is None
def test_generate_link_to_param_doc_empty_docstring():
"""Ensure None is returned when the estimator has no docstring."""
class MockEstimator:
pass
doc_link = "mock_module.MockEstimator.html"
url = generate_link_to_param_doc(MockEstimator, "alpha", doc_link)
assert url is None
def test_generate_link_to_param_doc_special_char():
"""Non-regression test for
https://github.com/scikit-learn/scikit-learn/issues/33830
"""
class MockEstimator:
"""Mock class.
Attributes
----------
weird_attr_ : ndarray of shape (`n_features_in_`,)
"""
doc_link = "mock_module.MockEstimator.html"
url = generate_link_to_param_doc(MockEstimator, "weird_attr_", doc_link)
expected_url = (
"mock_module.MockEstimator.html#:~:text=weird_attr_,"
"-ndarray%20of%20shape%20%28n_features_in_%2C%29"
)
assert url == expected_url