Skip to content

Commit 88fb6b6

Browse files
committed
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: mkdocstrings/python#175
1 parent 85527c1 commit 88fb6b6

3 files changed

Lines changed: 9 additions & 6 deletions

File tree

src/_griffe/docstrings/google.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,9 @@ def _read_attributes_section(
286286
annotation = parse_docstring_annotation(annotation, docstring)
287287
else:
288288
name = name_with_type
289-
with suppress(AttributeError, KeyError):
290-
annotation = docstring.parent.members[name].annotation # type: ignore[union-attr]
289+
with suppress(AttributeError, KeyError, TypeError):
290+
# Use subscript syntax to fetch annotation from inherited members too.
291+
annotation = docstring.parent[name].annotation # type: ignore[index]
291292

292293
attributes.append(DocstringAttribute(name=name, annotation=annotation, description=description))
293294

src/_griffe/docstrings/numpy.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,9 @@ def _read_attributes_section(
558558
name = name_type
559559
annotation = None
560560
if annotation is None:
561-
with suppress(AttributeError, KeyError):
562-
annotation = docstring.parent.members[name].annotation # type: ignore[union-attr]
561+
with suppress(AttributeError, KeyError, TypeError):
562+
# Use subscript syntax to fetch annotation from inherited members too.
563+
annotation = docstring.parent[name].annotation # type: ignore[index]
563564
else:
564565
annotation = parse_docstring_annotation(annotation, docstring, log_level=LogLevel.debug)
565566
text = dedent("\n".join(item[1:]))

src/_griffe/docstrings/sphinx.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,9 @@ def _read_attribute(
264264
annotation = parsed_attribute_type
265265
else:
266266
# try to use the annotation from the parent
267-
with suppress(AttributeError, KeyError):
268-
annotation = docstring.parent.attributes[name].annotation # type: ignore[union-attr]
267+
with suppress(AttributeError, KeyError, TypeError):
268+
# Use subscript syntax to fetch annotation from inherited members too.
269+
annotation = docstring.parent[name].annotation # type: ignore[index]
269270
if name in parsed_values.attributes:
270271
docstring_warning(docstring, 0, f"Duplicate attribute entry for '{name}'")
271272
else:

0 commit comments

Comments
 (0)