1616from sphinx import application
1717from sphinx .util import logging
1818from sphinx .util import nodes as sphinx_nodes
19+ from sphinx .ext .autodoc import mock
1920
2021LOG = logging .getLogger (__name__ )
2122
@@ -423,7 +424,8 @@ def _load_module(self, module_path: str) -> ty.Union[click.Command, click.Group]
423424 )
424425
425426 try :
426- mod = __import__ (module_name , globals (), locals (), [attr_name ])
427+ with mock (self .env .config .sphinx_click_mock_imports ):
428+ mod = __import__ (module_name , globals (), locals (), [attr_name ])
427429 except (Exception , SystemExit ) as exc : # noqa
428430 err_msg = 'Failed to import "{}" from "{}". ' .format (attr_name , module_name )
429431 if isinstance (exc , SystemExit ):
@@ -562,6 +564,8 @@ def run(self) -> ty.Iterable[nodes.section]:
562564
563565
564566def setup (app : application .Sphinx ) -> ty .Dict [str , ty .Any ]:
567+ # Need autodoc to support mocking modules
568+ app .setup_extension ('sphinx.ext.autodoc' )
565569 app .add_directive ('click' , ClickDirective )
566570
567571 app .add_event ("sphinx-click-process-description" )
@@ -570,6 +574,9 @@ def setup(app: application.Sphinx) -> ty.Dict[str, ty.Any]:
570574 app .add_event ("sphinx-click-process-arguments" )
571575 app .add_event ("sphinx-click-process-envvars" )
572576 app .add_event ("sphinx-click-process-epilog" )
577+ app .add_config_value (
578+ 'sphinx_click_mock_imports' , lambda config : config .autodoc_mock_imports , 'env'
579+ )
573580
574581 return {
575582 'parallel_read_safe' : True ,
0 commit comments