1
1
import html
2
2
import locale
3
3
import re
4
+ import types
4
5
from contextlib import closing
5
6
from functools import partial
6
7
from io import StringIO
@@ -453,7 +454,9 @@ def test_html_documentation_link_mixin_sklearn(mock_version):
453
454
("prefix.mypackage.mymodule.submodule" , "prefix.mypackage.mymodule.submodule" ),
454
455
],
455
456
)
456
- def test_html_documentation_link_mixin_get_doc_link (module_path , expected_module ):
457
+ def test_html_documentation_link_mixin_get_doc_link_instance (
458
+ module_path , expected_module
459
+ ):
457
460
"""Check the behaviour of the `_get_doc_link` with various parameter."""
458
461
459
462
class FooBar (_HTMLDocumentationLinkMixin ):
@@ -469,6 +472,32 @@ class FooBar(_HTMLDocumentationLinkMixin):
469
472
assert est ._get_doc_link () == f"https://website.com/{ expected_module } .FooBar.html"
470
473
471
474
475
+ @pytest .mark .parametrize (
476
+ "module_path,expected_module" ,
477
+ [
478
+ ("prefix.mymodule" , "prefix.mymodule" ),
479
+ ("prefix._mymodule" , "prefix" ),
480
+ ("prefix.mypackage._mymodule" , "prefix.mypackage" ),
481
+ ("prefix.mypackage._mymodule.submodule" , "prefix.mypackage" ),
482
+ ("prefix.mypackage.mymodule.submodule" , "prefix.mypackage.mymodule.submodule" ),
483
+ ],
484
+ )
485
+ def test_html_documentation_link_mixin_get_doc_link_class (module_path , expected_module ):
486
+ """Check the behaviour of the `_get_doc_link` when `_doc_link_module` and
487
+ `_doc_link_template` are defined at the class level and not at the instance
488
+ level."""
489
+
490
+ class FooBar (_HTMLDocumentationLinkMixin ):
491
+ _doc_link_module = "prefix"
492
+ _doc_link_template = (
493
+ "https://website.com/{estimator_module}.{estimator_name}.html"
494
+ )
495
+
496
+ FooBar .__module__ = module_path
497
+ est = FooBar ()
498
+ assert est ._get_doc_link () == f"https://website.com/{ expected_module } .FooBar.html"
499
+
500
+
472
501
def test_html_documentation_link_mixin_get_doc_link_out_of_library ():
473
502
"""Check the behaviour of the `_get_doc_link` with various parameter."""
474
503
mixin = _HTMLDocumentationLinkMixin ()
@@ -479,7 +508,7 @@ def test_html_documentation_link_mixin_get_doc_link_out_of_library():
479
508
assert mixin ._get_doc_link () == ""
480
509
481
510
482
- def test_html_documentation_link_mixin_doc_link_url_param_generator ():
511
+ def test_html_documentation_link_mixin_doc_link_url_param_generator_instance ():
483
512
mixin = _HTMLDocumentationLinkMixin ()
484
513
# we can bypass the generation by providing our own callable
485
514
mixin ._doc_link_template = (
@@ -492,11 +521,30 @@ def url_param_generator(estimator):
492
521
"another_variable" : "value_2" ,
493
522
}
494
523
495
- mixin ._doc_link_url_param_generator = url_param_generator
524
+ mixin ._doc_link_url_param_generator = types . MethodType ( url_param_generator , mixin )
496
525
497
526
assert mixin ._get_doc_link () == "https://website.com/value_1.value_2.html"
498
527
499
528
529
+ def test_html_documentation_link_mixin_doc_link_url_param_generator_class ():
530
+ # we can bypass the generation by providing our own callable
531
+
532
+ def url_param_generator (estimator ):
533
+ return {
534
+ "my_own_variable" : "value_1" ,
535
+ "another_variable" : "value_2" ,
536
+ }
537
+
538
+ class FooBar (_HTMLDocumentationLinkMixin ):
539
+ _doc_link_template = (
540
+ "https://website.com/{my_own_variable}.{another_variable}.html"
541
+ )
542
+ _doc_link_url_param_generator = url_param_generator
543
+
544
+ estimator = FooBar ()
545
+ assert estimator ._get_doc_link () == "https://website.com/value_1.value_2.html"
546
+
547
+
500
548
@pytest .fixture
501
549
def set_non_utf8_locale ():
502
550
"""Pytest fixture to set non utf-8 locale during the test.
0 commit comments