Skip to content

Commit

Permalink
fix: Handle aliases chains in has_docstrings method
Browse files Browse the repository at this point in the history
Also prevent accidental alias resolution.
  • Loading branch information
pawamoy committed Apr 8, 2022
1 parent 0399b9a commit 77c6943
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/griffe/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def has_docstrings(self) -> bool:
if self.has_docstring: # noqa: DAR201
return True
for member in self.members.values():
if (not member.is_alias or member.resolved) and member.has_docstrings: # type: ignore[union-attr]
if member.has_docstrings: # type: ignore[union-attr]
return True
return False

Expand Down Expand Up @@ -842,6 +842,16 @@ def endlineno(self) -> int | None:
return self.target.endlineno
return self.alias_endlineno

@property
def has_docstring(self) -> bool:
"""Tell if this alias' target has a non-empty docstring."""
return self.resolved and self.target.has_docstring # noqa: DAR201

@property
def has_docstrings(self) -> bool:
"""Tell if this alias' target or any of its members has a non-empty docstring."""
return self.resolved and self.target.has_docstrings # noqa: DAR201

@property
def parent(self) -> Module | Class | None:
"""Return the parent of this alias.
Expand Down
30 changes: 30 additions & 0 deletions tests/test_dataclasses.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Tests for the `dataclasses` module."""

from griffe.dataclasses import Docstring, Module
from griffe.loader import GriffeLoader
from tests.helpers import module_vtree, temporary_pypackage


def test_submodule_exports():
Expand All @@ -27,3 +29,31 @@ def test_has_docstrings():
module["b.c.d"].docstring = Docstring("Hello.")
assert module.has_docstrings


def test_handle_aliases_chain_in_has_docstrings():
"""Assert the `.has_docstrings` method can handle aliases chains in members."""
with temporary_pypackage("package", ["mod_a.py", "mod_b.py"]) as tmp_package:
mod_a = tmp_package.path / "mod_a.py"
mod_b = tmp_package.path / "mod_b.py"
mod_a.write_text("from .mod_b import someobj")
mod_b.write_text("from somelib import someobj")

loader = GriffeLoader(search_paths=[tmp_package.tmpdir])
package = loader.load_module(tmp_package.name)
assert not package.has_docstrings
loader.resolve_aliases(only_exported=False)
assert not package.has_docstrings


def test_has_docstrings_does_not_trigger_alias_resolution():
"""Assert the `.has_docstrings` method does not trigger alias resolution."""
with temporary_pypackage("package", ["mod_a.py", "mod_b.py"]) as tmp_package:
mod_a = tmp_package.path / "mod_a.py"
mod_b = tmp_package.path / "mod_b.py"
mod_a.write_text("from .mod_b import someobj")
mod_b.write_text("from somelib import someobj")

loader = GriffeLoader(search_paths=[tmp_package.tmpdir])
package = loader.load_module(tmp_package.name)
assert not package.has_docstrings
assert not package["mod_a.someobj"].resolved

0 comments on commit 77c6943

Please # to comment.