From 14db5ffba1c045d6fa32c7fd7454f1c9e07f9ce6 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Wed, 22 Jan 2025 02:51:59 +0000 Subject: [PATCH] Remove ``sphinx.ext.autodoc`` from the no-any-generics whitelist --- pyproject.toml | 2 -- sphinx/ext/autodoc/__init__.py | 12 ++++++++---- sphinx/ext/autodoc/importer.py | 16 ++++++++++------ 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 6192ea679..a24718bb7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -234,8 +234,6 @@ module = [ "sphinx.domains", "sphinx.domains.c", "sphinx.domains.cpp", - "sphinx.ext.autodoc", - "sphinx.ext.autodoc.importer", "sphinx.util.docfields", "sphinx.util.docutils", "sphinx.util.inspect", diff --git a/sphinx/ext/autodoc/__init__.py b/sphinx/ext/autodoc/__init__.py index ff91e1b2c..cc616f32c 100644 --- a/sphinx/ext/autodoc/__init__.py +++ b/sphinx/ext/autodoc/__init__.py @@ -167,7 +167,7 @@ def bool_option(arg: Any) -> bool: return True -def merge_members_option(options: dict) -> None: +def merge_members_option(options: dict[str, Any]) -> None: """Merge :private-members: and :special-members: options to the :members: option. """ @@ -1553,7 +1553,9 @@ class FunctionDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # typ return overload.replace(parameters=parameters) - def annotate_to_first_argument(self, func: Callable, typ: type) -> Callable | None: + def annotate_to_first_argument( + self, func: Callable[..., Any], typ: type + ) -> Callable[..., Any] | None: """Annotate type hint to the first argument of function if needed.""" try: sig = inspect.signature(func, type_aliases=self.config.autodoc_type_aliases) @@ -2538,7 +2540,9 @@ class MethodDocumenter(DocstringSignatureMixin, ClassLevelDocumenter): # type: return overload.replace(parameters=parameters) - def annotate_to_first_argument(self, func: Callable, typ: type) -> Callable | None: + def annotate_to_first_argument( + self, func: Callable[..., Any], typ: type + ) -> Callable[..., Any] | None: """Annotate type hint to the first argument of function if needed.""" try: sig = inspect.signature(func, type_aliases=self.config.autodoc_type_aliases) @@ -3104,7 +3108,7 @@ class PropertyDocumenter(DocstringStripSignatureMixin, ClassLevelDocumenter): # except ValueError: pass - def _get_property_getter(self) -> Callable | None: + def _get_property_getter(self) -> Callable[..., Any] | None: if safe_getattr(self.object, 'fget', None): # property return self.object.fget if safe_getattr(self.object, 'func', None): # cached_property diff --git a/sphinx/ext/autodoc/importer.py b/sphinx/ext/autodoc/importer.py index c51a124cd..ca06fb57c 100644 --- a/sphinx/ext/autodoc/importer.py +++ b/sphinx/ext/autodoc/importer.py @@ -30,19 +30,23 @@ from sphinx.util.inspect import ( ) if TYPE_CHECKING: - from collections.abc import Callable, Iterator, Mapping + from collections.abc import Iterator, Mapping from types import ModuleType - from typing import Any + from typing import Any, Protocol from sphinx.ext.autodoc import ObjectMember + class _AttrGetter(Protocol): + def __call__(self, obj: Any, name: str, default: Any = ..., /) -> Any: ... + + _NATIVE_SUFFIXES: frozenset[str] = frozenset({'.pyx', *EXTENSION_SUFFIXES}) logger = logging.getLogger(__name__) def _filter_enum_dict( enum_class: type[Enum], - attrgetter: Callable[[Any, str, Any], Any], + attrgetter: _AttrGetter, enum_class_dict: Mapping[str, object], ) -> Iterator[tuple[str, type, Any]]: """Find the attributes to document of an enumeration class. @@ -241,7 +245,7 @@ def import_object( modname: str, objpath: list[str], objtype: str = '', - attrgetter: Callable[[Any, str], Any] = safe_getattr, + attrgetter: _AttrGetter = safe_getattr, ) -> Any: if objpath: logger.debug('[autodoc] from %s import %s', modname, '.'.join(objpath)) @@ -330,7 +334,7 @@ class Attribute(NamedTuple): def get_object_members( subject: Any, objpath: list[str], - attrgetter: Callable, + attrgetter: _AttrGetter, analyzer: ModuleAnalyzer | None = None, ) -> dict[str, Attribute]: """Get members and attributes of target object.""" @@ -403,7 +407,7 @@ def get_object_members( def get_class_members( - subject: Any, objpath: Any, attrgetter: Callable, inherit_docstrings: bool = True + subject: Any, objpath: Any, attrgetter: _AttrGetter, inherit_docstrings: bool = True ) -> dict[str, ObjectMember]: """Get members and attributes of target class.""" from sphinx.ext.autodoc import INSTANCEATTR, ObjectMember