Skip to content

Commit

Permalink
fix: Don't trigger alias resolution while checking docstrings presence
Browse files Browse the repository at this point in the history
Issue #37: #37
  • Loading branch information
pawamoy committed Feb 7, 2022
1 parent 20f3d71 commit dda72ea
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/griffe/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,12 @@ def has_docstring(self) -> bool:
@property
def has_docstrings(self) -> bool:
"""Tell if this object or any of its members has a non-empty docstring."""
return self.has_docstring or any(member.has_docstrings for member in self.members.values()) # noqa: DAR201
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_docstring: # type: ignore[union-attr]
return True
return False

def member_is_exported(self, member: Object | Alias, explicitely: bool = True) -> bool:
"""Tell if a member of this object is "exported".
Expand Down
4 changes: 2 additions & 2 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def temporary_pyfile(code: str) -> Iterator[tuple[str, Path]]:
"""
with tempfile.TemporaryDirectory(prefix=TMPDIR_PREFIX) as tmpdir:
tmpfile = Path(tmpdir) / "module.py"
tmpfile.write_text(code)
tmpfile.write_text(dedent(code))
yield "module", tmpfile


Expand Down Expand Up @@ -96,7 +96,7 @@ def temporary_inspected_module(code: str) -> Iterator[Module]:
Yields:
The inspected module.
"""
with temporary_pyfile(dedent(code)) as (name, path):
with temporary_pyfile(code) as (name, path):
try:
yield inspect(name, filepath=path)
finally:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Tests for the `loader` module."""

from griffe.loader import GriffeLoader
from tests.helpers import temporary_pyfile


def test_has_docstrings_does_not_try_to_resolve_alias():
"""Assert that checkins presence of docstrings does not trigger alias resolution."""
with temporary_pyfile("""from abc import abstractmethod""") as (module_name, path):
loader = GriffeLoader(search_paths=[path.parent])
module = loader.load_module(module_name)
loader.resolve_aliases()
assert "abstractmethod" in module.members
assert not module.has_docstrings

0 comments on commit dda72ea

Please sign in to comment.