Skip to content

Commit

Permalink
fix: Fetch attribute annotations from inherited members too when pars…
Browse files Browse the repository at this point in the history
…ing 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
  • Loading branch information
pawamoy committed Nov 24, 2024
1 parent 85527c1 commit 88fb6b6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/_griffe/docstrings/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
5 changes: 3 additions & 2 deletions src/_griffe/docstrings/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:]))
Expand Down
5 changes: 3 additions & 2 deletions src/_griffe/docstrings/sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 88fb6b6

Please sign in to comment.