Skip to content

Commit

Permalink
feat: Add source property to docstrings, which return the docstring…
Browse files Browse the repository at this point in the history
… lines as written in the source

Issue-90: #90
  • Loading branch information
pawamoy committed Aug 9, 2024
1 parent a69cffd commit 3f6a71a
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/_griffe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ def lines(self) -> list[str]:
"""The lines of the docstring."""
return self.value.split("\n")

@property
def source(self) -> str:
"""The original, uncleaned value of the docstring as written in the source."""
if self.parent is None:
raise ValueError("Cannot get original docstring without parent object")
if isinstance(self.parent.filepath, list):
raise ValueError("Cannot get original docstring for namespace package") # noqa: TRY004
if self.lineno is None or self.endlineno is None:
raise ValueError("Cannot get original docstring without line numbers")
return "\n".join(self.parent.lines_collection[self.parent.filepath][self.lineno - 1 : self.endlineno])

@cached_property
def parsed(self) -> list[DocstringSection]:
"""The docstring sections, parsed into structured data."""
Expand Down

0 comments on commit 3f6a71a

Please sign in to comment.