From 49027a9ae61d0d00016182025f68239a50ba4c09 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Date: Fri, 28 Apr 2023 10:32:04 +0100 Subject: [PATCH] Autosummary: Always emit grouped ``ImportError`` exceptions (#11380) --- CHANGES | 1 + sphinx/ext/autosummary/__init__.py | 20 +++++--------------- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/CHANGES b/CHANGES index af7b50cf1ff..4821f5f99b1 100644 --- a/CHANGES +++ b/CHANGES @@ -24,6 +24,7 @@ Incompatible changes * #11378: Remove deprecated ``sphinx.util.docutils.is_html5_writer_available()`` function. * #11379: Make the ``env`` argument to ``Builder`` subclasses required. +* #11380: autosummary: Always emit grouped import exceptions. Deprecated ---------- diff --git a/sphinx/ext/autosummary/__init__.py b/sphinx/ext/autosummary/__init__.py index 7f458b8bf9c..76d99b83e8b 100644 --- a/sphinx/ext/autosummary/__init__.py +++ b/sphinx/ext/autosummary/__init__.py @@ -53,7 +53,6 @@ import posixpath import re import sys -import warnings from inspect import Parameter from os import path from types import ModuleType @@ -69,7 +68,6 @@ from sphinx import addnodes from sphinx.application import Sphinx from sphinx.config import Config -from sphinx.deprecation import RemovedInSphinx70Warning from sphinx.environment import BuildEnvironment from sphinx.ext.autodoc import INSTANCEATTR, Documenter from sphinx.ext.autodoc.directive import DocumenterBridge, Options @@ -627,17 +625,11 @@ def get_import_prefixes_from_env(env: BuildEnvironment) -> list[str | None]: def import_by_name( - name: str, prefixes: list[str | None] = [None], grouped_exception: bool = True, + name: str, prefixes: list[str | None] = [None], ) -> tuple[str, Any, Any, str]: """Import a Python object that has the given *name*, under one of the *prefixes*. The first name that succeeds is used. """ - if grouped_exception is False: - warnings.warn('Using grouped_exception keyword for import_by_name() is not ' - 'recommended. It will be removed at v7.0. Therefore you should ' - 'catch ImportExceptionGroup exception instead of ImportError.', - RemovedInSphinx70Warning, stacklevel=2) - tried = [] errors: list[ImportExceptionGroup] = [] for prefix in prefixes: @@ -646,7 +638,7 @@ def import_by_name( prefixed_name = '.'.join([prefix, name]) else: prefixed_name = name - obj, parent, modname = _import_by_name(prefixed_name, grouped_exception) + obj, parent, modname = _import_by_name(prefixed_name, grouped_exception=True) return prefixed_name, obj, parent, modname except ImportError: tried.append(prefixed_name) @@ -654,10 +646,8 @@ def import_by_name( tried.append(prefixed_name) errors.append(exc) - if grouped_exception: - exceptions: list[BaseException] = sum((e.exceptions for e in errors), []) - raise ImportExceptionGroup('no module named %s' % ' or '.join(tried), exceptions) - raise ImportError('no module named %s' % ' or '.join(tried)) + exceptions: list[BaseException] = sum((e.exceptions for e in errors), []) + raise ImportExceptionGroup('no module named %s' % ' or '.join(tried), exceptions) def _import_by_name(name: str, grouped_exception: bool = True) -> tuple[Any, Any, str]: @@ -714,7 +704,7 @@ def import_ivar_by_name(name: str, prefixes: list[str | None] = [None], """ try: name, attr = name.rsplit(".", 1) - real_name, obj, parent, modname = import_by_name(name, prefixes, grouped_exception) + real_name, obj, parent, modname = import_by_name(name, prefixes) # Get ancestors of the object (class.__mro__ includes the class itself as # the first entry)