Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse yields section in docstring #2

Merged
merged 3 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# (Fork) 0.0.2 (2023-08-26)

- Google: Added capability to parse the yields section


# (Fork) 0.0.1 (2023-08-18)

- Google: Fixed a bug where union style return types (such as `int | str`) are not parsed correctly
Expand Down
10 changes: 10 additions & 0 deletions docstring_parser/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,16 @@ def many_returns(self) -> T.List[DocstringReturns]:
item for item in self.meta if isinstance(item, DocstringReturns)
]

@property
def yields(self):
"""Return information on function yield.
Takes the first generator information.
"""
for item in self.meta:
if isinstance(item, DocstringReturns) and item.is_generator:
return item
return None

@property
def deprecation(self) -> T.Optional[DocstringDeprecated]:
"""Return a single information on function deprecation notes."""
Expand Down
107 changes: 107 additions & 0 deletions docstring_parser/tests/test_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,113 @@ def test_returns() -> None:
assert docstring.many_returns[0] == docstring.returns


def test_yields() -> None:
"""Test parsing returns."""
docstring = parse(
"""
Short description
"""
)
assert docstring.yields is None

docstring = parse(
"""
Returns:
int: description
"""
)
assert docstring.yields is None

docstring = parse(
"""
Short description
Yields:
description
"""
)
assert docstring.yields is not None
assert docstring.yields.type_name is None
assert docstring.yields.description == "description"
assert docstring.yields.is_generator is True

docstring = parse(
"""
Short description
Yields:
description with: a colon!
"""
)
assert docstring.yields is not None
assert docstring.yields.type_name is None
assert docstring.yields.description == "description with: a colon!"
assert docstring.yields.is_generator is True

docstring = parse(
"""
Short description
Yields:
int: description
"""
)
assert docstring.yields is not None
assert docstring.yields.type_name == "int"
assert docstring.yields.description == "description"
assert docstring.yields.is_generator is True

docstring = parse(
"""
Yields:
Optional[Mapping[str, List[int]]]: A description: with a colon
"""
)
assert docstring.yields is not None
assert docstring.yields.type_name == "Optional[Mapping[str, List[int]]]"
assert docstring.yields.description == "A description: with a colon"
assert docstring.yields.is_generator is True

docstring = parse(
"""
Short description
Yields:
int: description
with much text

even some spacing
"""
)
assert docstring.yields is not None
assert docstring.yields.type_name == "int"
assert docstring.yields.description == (
"description\nwith much text\n\neven some spacing"
)
assert docstring.returns.is_generator is True

docstring = parse(
"""
Returns:
int: description with return first

Yields:
Optional[Mapping[str, List[int]]]: A description: with a colon
"""
)
assert docstring.returns is not None
assert docstring.yields is not None
assert docstring.yields.type_name != docstring.returns.type_name

docstring = parse(
"""
Yields:
Optional[Mapping[str, List[int]]]: A description: with a colon
Returns:
int: description with return last
"""
)
assert docstring.returns is not None
assert docstring.yields is not None
assert docstring.yields.type_name == docstring.returns.type_name


def test_raises() -> None:
"""Test parsing raises."""
docstring = parse(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "docstring_parser_fork"
version = "0.0.1"
version = "0.0.2"
description = "Parse Python docstrings in reST, Google and Numpydoc format"
authors = ["Marcin Kurczewski <[email protected]>"]
license = "MIT"
Expand Down
Loading