Skip to content

Commit

Permalink
Support autodoc_mock_imports (#35)
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Turner <9087854+aa-turner@users.noreply.github.com>
  • Loading branch information
prajeeshag and AA-Turner authored Jul 15, 2024
1 parent c8beb8f commit fc253ed
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions sphinxarg/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from docutils.parsers.rst import Directive, Parser
from docutils.parsers.rst.directives import flag, unchanged
from docutils.statemachine import StringList
from sphinx.ext.autodoc import mock
from sphinx.util.docutils import new_document
from sphinx.util.nodes import nested_parse_with_titles

Expand Down Expand Up @@ -505,22 +506,23 @@ def run(self):

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

if not hasattr(mod, attr_name):
msg = (
f'Module "{module_name}" has no attribute "{attr_name}"\n'
f'Incorrect argparse :module: or :func: values?'
)
raise self.error(msg)
func = getattr(mod, attr_name)
if not hasattr(mod, attr_name):
msg = (
f'Module "{module_name}" has no attribute "{attr_name}"\n'
f'Incorrect argparse :module: or :func: values?'
)
raise self.error(msg)
func = getattr(mod, attr_name)

if isinstance(func, ArgumentParser):
parser = func
Expand Down Expand Up @@ -595,6 +597,7 @@ def run(self):


def setup(app):
app.setup_extension('sphinx.ext.autodoc')
app.add_directive('argparse', ArgParseDirective)
return {
'version': __version__,
Expand Down

0 comments on commit fc253ed

Please sign in to comment.