From ed486d3f42c1b09070fdd99ae5f22c54751206ea Mon Sep 17 00:00:00 2001 From: Prajeesh Ag Date: Tue, 14 Feb 2023 18:37:55 +0300 Subject: [PATCH 1/3] Sphinx autodoc mock_imports added --- sphinxarg/ext.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/sphinxarg/ext.py b/sphinxarg/ext.py index 871fb3d3..319473a1 100644 --- a/sphinxarg/ext.py +++ b/sphinxarg/ext.py @@ -13,6 +13,8 @@ from docutils.statemachine import StringList from sphinx.util.docutils import new_document from sphinx.util.nodes import nested_parse_with_titles +from sphinx.ext.autodoc import mock +from sphinx.util.docutils import SphinxDirective from sphinxarg import __version__ from sphinxarg.parser import parse_parser, parser_navigate @@ -278,7 +280,7 @@ def ensure_unique_ids(items): n['ids'] = ids -class ArgParseDirective(Directive): +class ArgParseDirective(SphinxDirective): has_content = True option_spec = { 'module': unchanged, @@ -503,24 +505,27 @@ def run(self): msg = ':module: and :func: should be specified, or :ref:, or :filename: and :func:' raise self.error(msg) + mock_imports = getattr(self.config,'autodoc_mock_imports',[]) + # 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 = ( + with mock(mock_imports): + try: + mod = importlib.import_module(module_name) + except ImportErroras 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 = ( + 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) + func = getattr(mod, attr_name) if isinstance(func, ArgumentParser): parser = func From 80e9325ae8583891ad76307c5f0b7ffe16627f85 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Mon, 15 Jul 2024 16:49:58 +0100 Subject: [PATCH 2/3] Updates --- sphinxarg/ext.py | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/sphinxarg/ext.py b/sphinxarg/ext.py index 319473a1..a150e83f 100644 --- a/sphinxarg/ext.py +++ b/sphinxarg/ext.py @@ -14,7 +14,6 @@ from sphinx.util.docutils import new_document from sphinx.util.nodes import nested_parse_with_titles from sphinx.ext.autodoc import mock -from sphinx.util.docutils import SphinxDirective from sphinxarg import __version__ from sphinxarg.parser import parse_parser, parser_navigate @@ -280,7 +279,7 @@ def ensure_unique_ids(items): n['ids'] = ids -class ArgParseDirective(SphinxDirective): +class ArgParseDirective(Directive): has_content = True option_spec = { 'module': unchanged, @@ -505,26 +504,24 @@ def run(self): msg = ':module: and :func: should be specified, or :ref:, or :filename: and :func:' raise self.error(msg) - mock_imports = getattr(self.config,'autodoc_mock_imports',[]) - # Skip this if we're dealing with a local file, since it obviously can't be imported if 'filename' not in self.options: - with mock(mock_imports): + with mock(self.config.autodoc_mock_imports): try: mod = importlib.import_module(module_name) - except ImportErroras exc: + 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 + 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) + 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): @@ -600,6 +597,7 @@ def run(self): def setup(app): + app.setup_extension('sphinx.ext.autodoc') app.add_directive('argparse', ArgParseDirective) return { 'version': __version__, From d76aa1ef40928615263fc1f19814f84f00a92a3c Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Mon, 15 Jul 2024 16:51:11 +0100 Subject: [PATCH 3/3] Ruff --- sphinxarg/ext.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinxarg/ext.py b/sphinxarg/ext.py index a150e83f..1f0004c6 100644 --- a/sphinxarg/ext.py +++ b/sphinxarg/ext.py @@ -11,9 +11,9 @@ 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 -from sphinx.ext.autodoc import mock from sphinxarg import __version__ from sphinxarg.parser import parse_parser, parser_navigate