Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support autodoc_mock_imports #35

Merged
merged 3 commits into from
Jul 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading