|
11 | 11 | from docutils.parsers.rst import Directive, Parser |
12 | 12 | from docutils.parsers.rst.directives import flag, unchanged |
13 | 13 | from docutils.statemachine import StringList |
| 14 | +from sphinx.ext.autodoc import mock |
14 | 15 | from sphinx.util.docutils import new_document |
15 | 16 | from sphinx.util.nodes import nested_parse_with_titles |
16 | 17 |
|
@@ -505,22 +506,23 @@ def run(self): |
505 | 506 |
|
506 | 507 | # Skip this if we're dealing with a local file, since it obviously can't be imported |
507 | 508 | 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 |
516 | 518 |
|
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) |
524 | 526 |
|
525 | 527 | if isinstance(func, ArgumentParser): |
526 | 528 | parser = func |
@@ -595,6 +597,7 @@ def run(self): |
595 | 597 |
|
596 | 598 |
|
597 | 599 | def setup(app): |
| 600 | + app.setup_extension('sphinx.ext.autodoc') |
598 | 601 | app.add_directive('argparse', ArgParseDirective) |
599 | 602 | return { |
600 | 603 | 'version': __version__, |
|
0 commit comments