Skip to content

Commit fc253ed

Browse files
Support autodoc_mock_imports (#35)
Co-authored-by: Adam Turner <9087854+aa-turner@users.noreply.github.com>
1 parent c8beb8f commit fc253ed

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

sphinxarg/ext.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from docutils.parsers.rst import Directive, Parser
1212
from docutils.parsers.rst.directives import flag, unchanged
1313
from docutils.statemachine import StringList
14+
from sphinx.ext.autodoc import mock
1415
from sphinx.util.docutils import new_document
1516
from sphinx.util.nodes import nested_parse_with_titles
1617

@@ -505,22 +506,23 @@ def run(self):
505506

506507
# Skip this if we're dealing with a local file, since it obviously can't be imported
507508
if 'filename' not in self.options:
508-
try:
509-
mod = importlib.import_module(module_name)
510-
except ImportError as exc:
511-
msg = (
512-
f'Failed to import "{attr_name}" from "{module_name}".\n'
513-
f'{sys.exc_info()[1]}'
514-
)
515-
raise self.error(msg) from exc
509+
with mock(self.config.autodoc_mock_imports):
510+
try:
511+
mod = importlib.import_module(module_name)
512+
except ImportError as exc:
513+
msg = (
514+
f'Failed to import "{attr_name}" from "{module_name}".\n'
515+
f'{sys.exc_info()[1]}'
516+
)
517+
raise self.error(msg) from exc
516518

517-
if not hasattr(mod, attr_name):
518-
msg = (
519-
f'Module "{module_name}" has no attribute "{attr_name}"\n'
520-
f'Incorrect argparse :module: or :func: values?'
521-
)
522-
raise self.error(msg)
523-
func = getattr(mod, attr_name)
519+
if not hasattr(mod, attr_name):
520+
msg = (
521+
f'Module "{module_name}" has no attribute "{attr_name}"\n'
522+
f'Incorrect argparse :module: or :func: values?'
523+
)
524+
raise self.error(msg)
525+
func = getattr(mod, attr_name)
524526

525527
if isinstance(func, ArgumentParser):
526528
parser = func
@@ -595,6 +597,7 @@ def run(self):
595597

596598

597599
def setup(app):
600+
app.setup_extension('sphinx.ext.autodoc')
598601
app.add_directive('argparse', ArgParseDirective)
599602
return {
600603
'version': __version__,

0 commit comments

Comments
 (0)