diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-05 16:20:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-05 16:20:58 +0000 |
commit | 5bb0bb4be543fd5eca41673696a62ed80d493591 (patch) | |
tree | ad2c464f140e86c7f178a6276d7ea4a93e3e6c92 /sphinx/ext/autosummary | |
parent | Adding upstream version 7.2.6. (diff) | |
download | sphinx-upstream.tar.xz sphinx-upstream.zip |
Adding upstream version 7.3.7.upstream/7.3.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'sphinx/ext/autosummary')
-rw-r--r-- | sphinx/ext/autosummary/__init__.py | 38 | ||||
-rw-r--r-- | sphinx/ext/autosummary/generate.py | 18 |
2 files changed, 30 insertions, 26 deletions
diff --git a/sphinx/ext/autosummary/__init__.py b/sphinx/ext/autosummary/__init__.py index edb8f0d..7057f43 100644 --- a/sphinx/ext/autosummary/__init__.py +++ b/sphinx/ext/autosummary/__init__.py @@ -48,7 +48,9 @@ This can be used as the default role to make links 'smart'. from __future__ import annotations +import functools import inspect +import operator import os import posixpath import re @@ -56,7 +58,7 @@ import sys from inspect import Parameter from os import path from types import ModuleType -from typing import TYPE_CHECKING, Any, cast +from typing import TYPE_CHECKING, Any, ClassVar, cast from docutils import nodes from docutils.parsers.rst import directives @@ -93,7 +95,7 @@ if TYPE_CHECKING: from sphinx.application import Sphinx from sphinx.extension import Extension - from sphinx.util.typing import OptionSpec + from sphinx.util.typing import ExtensionMetadata, OptionSpec from sphinx.writers.html import HTML5Translator logger = logging.getLogger(__name__) @@ -162,7 +164,7 @@ class FakeDirective(DocumenterBridge): settings = Struct(tab_width=8) document = Struct(settings=settings) app = FakeApplication() - app.config.add('autodoc_class_signature', 'mixed', True, None) + app.config.add('autodoc_class_signature', 'mixed', 'env', ()) env = BuildEnvironment(app) # type: ignore[arg-type] state = Struct(document=document) super().__init__(env, None, Options(), 0, state) @@ -216,7 +218,7 @@ class Autosummary(SphinxDirective): optional_arguments = 0 final_argument_whitespace = False has_content = True - option_spec: OptionSpec = { + option_spec: ClassVar[OptionSpec] = { 'caption': directives.unchanged_required, 'toctree': directives.unchanged, 'nosignatures': directives.flag, @@ -284,9 +286,9 @@ class Autosummary(SphinxDirective): return import_ivar_by_name(name, prefixes) except ImportError as exc2: if exc2.__cause__: - errors: list[BaseException] = exc.exceptions + [exc2.__cause__] + errors: list[BaseException] = [*exc.exceptions, exc2.__cause__] else: - errors = exc.exceptions + [exc2] + errors = [*exc.exceptions, exc2] raise ImportExceptionGroup(exc.args[0], errors) from None @@ -591,7 +593,7 @@ def limited_join(sep: str, items: list[str], max_chars: int = 30, else: break - return sep.join(list(items[:n_items]) + [overflow_marker]) + return sep.join([*list(items[:n_items]), overflow_marker]) # -- Importing items ----------------------------------------------------------- @@ -603,7 +605,7 @@ class ImportExceptionGroup(Exception): It contains an error messages and a list of exceptions as its arguments. """ - def __init__(self, message: str | None, exceptions: Sequence[BaseException]): + def __init__(self, message: str | None, exceptions: Sequence[BaseException]) -> None: super().__init__(message) self.exceptions = list(exceptions) @@ -640,7 +642,7 @@ def import_by_name( for prefix in prefixes: try: if prefix: - prefixed_name = '.'.join([prefix, name]) + prefixed_name = f'{prefix}.{name}' else: prefixed_name = name obj, parent, modname = _import_by_name(prefixed_name, grouped_exception=True) @@ -651,7 +653,8 @@ def import_by_name( tried.append(prefixed_name) errors.append(exc) - exceptions: list[BaseException] = sum((e.exceptions for e in errors), []) + exceptions: list[BaseException] = functools.reduce( + operator.iadd, (e.exceptions for e in errors), []) raise ImportExceptionGroup('no module named %s' % ' or '.join(tried), exceptions) @@ -742,6 +745,7 @@ class AutoLink(SphinxRole): Expands to ':obj:`text`' if `text` is an object that can be imported; otherwise expands to '*text*'. """ + def run(self) -> tuple[list[Node], list[system_message]]: pyobj_role = self.env.get_domain('py').role('obj') assert pyobj_role is not None @@ -766,7 +770,7 @@ class AutoLink(SphinxRole): def get_rst_suffix(app: Sphinx) -> str | None: def get_supported_format(suffix: str) -> tuple[str, ...]: - parser_class = app.registry.get_source_parsers().get(suffix) + parser_class = app.registry.get_source_parsers().get(suffix.removeprefix('.')) if parser_class is None: return ('restructuredtext',) return parser_class.supported @@ -803,7 +807,7 @@ def process_generate_options(app: Sphinx) -> None: suffix = get_rst_suffix(app) if suffix is None: - logger.warning(__('autosummary generats .rst files internally. ' + logger.warning(__('autosummary generates .rst files internally. ' 'But your source_suffix does not contain .rst. Skipped.')) return @@ -817,7 +821,7 @@ def process_generate_options(app: Sphinx) -> None: encoding=app.config.source_encoding) -def setup(app: Sphinx) -> dict[str, Any]: +def setup(app: Sphinx) -> ExtensionMetadata: # I need autodoc app.setup_extension('sphinx.ext.autodoc') app.add_node(autosummary_toc, @@ -835,13 +839,13 @@ def setup(app: Sphinx) -> dict[str, Any]: app.add_directive('autosummary', Autosummary) app.add_role('autolink', AutoLink()) app.connect('builder-inited', process_generate_options) - app.add_config_value('autosummary_context', {}, True) + app.add_config_value('autosummary_context', {}, 'env') app.add_config_value('autosummary_filename_map', {}, 'html') - app.add_config_value('autosummary_generate', True, True, [bool, list]) - app.add_config_value('autosummary_generate_overwrite', True, False) + app.add_config_value('autosummary_generate', True, 'env', {bool, list}) + app.add_config_value('autosummary_generate_overwrite', True, '') app.add_config_value('autosummary_mock_imports', lambda config: config.autodoc_mock_imports, 'env') - app.add_config_value('autosummary_imported_members', [], False, [bool]) + app.add_config_value('autosummary_imported_members', [], '', bool) app.add_config_value('autosummary_ignore_module_all', True, 'env', bool) return {'version': sphinx.__display_version__, 'parallel_read_safe': True} diff --git a/sphinx/ext/autosummary/generate.py b/sphinx/ext/autosummary/generate.py index 06814f9..83497f9 100644 --- a/sphinx/ext/autosummary/generate.py +++ b/sphinx/ext/autosummary/generate.py @@ -71,10 +71,9 @@ class DummyApplication: self._warncount = 0 self.warningiserror = False - self.config.add('autosummary_context', {}, True, None) - self.config.add('autosummary_filename_map', {}, True, None) + self.config.add('autosummary_context', {}, 'env', ()) + self.config.add('autosummary_filename_map', {}, 'env', ()) self.config.add('autosummary_ignore_module_all', True, 'env', bool) - self.config.init_values() def emit_firstresult(self, *args: Any) -> None: pass @@ -134,7 +133,8 @@ class AutosummaryRenderer: if app.translator: self.env.add_extension("jinja2.ext.i18n") - self.env.install_gettext_translations(app.translator) + # ``install_gettext_translations`` is injected by the ``jinja2.ext.i18n`` extension + self.env.install_gettext_translations(app.translator) # type: ignore[attr-defined] def render(self, template_name: str, context: dict) -> str: """Render a template file.""" @@ -249,8 +249,8 @@ class ModuleScanner: def members_of(obj: Any, conf: Config) -> Sequence[str]: """Get the members of ``obj``, possibly ignoring the ``__all__`` module attribute - Follows the ``conf.autosummary_ignore_module_all`` setting.""" - + Follows the ``conf.autosummary_ignore_module_all`` setting. + """ if conf.autosummary_ignore_module_all: return dir(obj) else: @@ -331,7 +331,7 @@ def generate_autosummary_content(name: str, obj: Any, parent: Any, if doc.objtype in ('method', 'attribute', 'property'): ns['class'] = qualname.rsplit(".", 1)[0] - if doc.objtype in ('class',): + if doc.objtype == 'class': shortname = qualname else: shortname = qualname.rsplit(".", 1)[-1] @@ -509,9 +509,9 @@ def generate_autosummary_docs(sources: list[str], qualname = name.replace(modname + ".", "") except ImportError as exc2: if exc2.__cause__: - exceptions: list[BaseException] = exc.exceptions + [exc2.__cause__] + exceptions: list[BaseException] = [*exc.exceptions, exc2.__cause__] else: - exceptions = exc.exceptions + [exc2] + exceptions = [*exc.exceptions, exc2] errors = list({f"* {type(e).__name__}: {e}" for e in exceptions}) logger.warning(__('[autosummary] failed to import %s.\nPossible hints:\n%s'), |