From 88fb6b6abd286b5552887023faa1a22f30cb11e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Mon, 25 Nov 2024 00:05:59 +0100 Subject: [PATCH] fix: Fetch attribute annotations from inherited members too when parsing docstrings I'm aware that computing inherited members will cache the MRO, but docstrings are usually never parsed *before* a package has finished loading. We do have to emphasize this in our docs though: extensions must not parse docstrings and modify parsed sections, and should rather modify the docstring value directly (even though it can be less convenient). We should also stop caching parsed docstrings and MRO anyway: turns out the cache invalidation problem is really problematic. Issue-mkdocstrings/python#175: https://github.com/mkdocstrings/python/issues/175 --- src/_griffe/docstrings/google.py | 5 +++-- src/_griffe/docstrings/numpy.py | 5 +++-- src/_griffe/docstrings/sphinx.py | 5 +++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/_griffe/docstrings/google.py b/src/_griffe/docstrings/google.py index ed686601..e8defadf 100644 --- a/src/_griffe/docstrings/google.py +++ b/src/_griffe/docstrings/google.py @@ -286,8 +286,9 @@ def _read_attributes_section( annotation = parse_docstring_annotation(annotation, docstring) else: name = name_with_type - with suppress(AttributeError, KeyError): - annotation = docstring.parent.members[name].annotation # type: ignore[union-attr] + with suppress(AttributeError, KeyError, TypeError): + # Use subscript syntax to fetch annotation from inherited members too. + annotation = docstring.parent[name].annotation # type: ignore[index] attributes.append(DocstringAttribute(name=name, annotation=annotation, description=description)) diff --git a/src/_griffe/docstrings/numpy.py b/src/_griffe/docstrings/numpy.py index 29612993..d20ad300 100644 --- a/src/_griffe/docstrings/numpy.py +++ b/src/_griffe/docstrings/numpy.py @@ -558,8 +558,9 @@ def _read_attributes_section( name = name_type annotation = None if annotation is None: - with suppress(AttributeError, KeyError): - annotation = docstring.parent.members[name].annotation # type: ignore[union-attr] + with suppress(AttributeError, KeyError, TypeError): + # Use subscript syntax to fetch annotation from inherited members too. + annotation = docstring.parent[name].annotation # type: ignore[index] else: annotation = parse_docstring_annotation(annotation, docstring, log_level=LogLevel.debug) text = dedent("\n".join(item[1:])) diff --git a/src/_griffe/docstrings/sphinx.py b/src/_griffe/docstrings/sphinx.py index 72415052..73c1487b 100644 --- a/src/_griffe/docstrings/sphinx.py +++ b/src/_griffe/docstrings/sphinx.py @@ -264,8 +264,9 @@ def _read_attribute( annotation = parsed_attribute_type else: # try to use the annotation from the parent - with suppress(AttributeError, KeyError): - annotation = docstring.parent.attributes[name].annotation # type: ignore[union-attr] + with suppress(AttributeError, KeyError, TypeError): + # Use subscript syntax to fetch annotation from inherited members too. + annotation = docstring.parent[name].annotation # type: ignore[index] if name in parsed_values.attributes: docstring_warning(docstring, 0, f"Duplicate attribute entry for '{name}'") else: