Skip to content

Commit b9304b5

Browse files
yewentaorr-
authored andcommitted
google: support Example section
1 parent 53a2483 commit b9304b5

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

docstring_parser/common.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
DEPRECATION_KEYWORDS = {"deprecation", "deprecated"}
1616
RETURNS_KEYWORDS = {"return", "returns"}
1717
YIELDS_KEYWORDS = {"yield", "yields"}
18+
EXAMPLES_KEYWORDS = {"example", "examples"}
1819

1920

2021
class ParseError(RuntimeError):
@@ -130,6 +131,19 @@ def __init__(
130131
self.description = description
131132

132133

134+
class DocstringExample(DocstringMeta):
135+
"""DocstringMeta symbolizing example metadata."""
136+
137+
def __init__(
138+
self,
139+
args: T.List[str],
140+
description: T.Optional[str],
141+
) -> None:
142+
"""Initialize self."""
143+
super().__init__(args, description)
144+
self.description = description
145+
146+
133147
class Docstring:
134148
"""Docstring object representation."""
135149

@@ -184,3 +198,10 @@ def deprecation(self) -> T.Optional[DocstringDeprecated]:
184198
if isinstance(item, DocstringDeprecated):
185199
return item
186200
return None
201+
202+
@property
203+
def examples(self) -> T.List[DocstringExample]:
204+
"""Return a list of information on function examples."""
205+
return [
206+
item for item in self.meta if isinstance(item, DocstringExample)
207+
]

docstring_parser/google.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
from enum import IntEnum
88

99
from .common import (
10+
EXAMPLES_KEYWORDS,
1011
PARAM_KEYWORDS,
1112
RAISES_KEYWORDS,
1213
RETURNS_KEYWORDS,
1314
YIELDS_KEYWORDS,
1415
Docstring,
16+
DocstringExample,
1517
DocstringMeta,
1618
DocstringParam,
1719
DocstringRaises,
@@ -133,6 +135,8 @@ def _build_single_meta(section: Section, desc: str) -> DocstringMeta:
133135
return DocstringRaises(
134136
args=[section.key], description=desc, type_name=None
135137
)
138+
if section.key in EXAMPLES_KEYWORDS:
139+
return DocstringExample(args=[section.key], description=desc)
136140
if section.key in PARAM_KEYWORDS:
137141
raise ParseError("Expected paramenter name.")
138142
return DocstringMeta(args=[section.key], description=desc)

docstring_parser/tests/test_google.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -632,9 +632,9 @@ def test_examples() -> None:
632632
more here
633633
"""
634634
)
635-
assert len(docstring.meta) == 2
636-
assert docstring.meta[0].description == "example: 1"
637-
assert docstring.meta[1].description == "long example\n\nmore here"
635+
assert len(docstring.examples) == 2
636+
assert docstring.examples[0].description == "example: 1"
637+
assert docstring.examples[1].description == "long example\n\nmore here"
638638

639639

640640
def test_broken_meta() -> None:
@@ -696,9 +696,9 @@ def test_empty_example() -> None:
696696
"""
697697
)
698698

699-
assert len(docstring.meta) == 2
700-
assert docstring.meta[0].args == ["examples"]
701-
assert docstring.meta[0].description == ""
699+
assert len(docstring.examples) == 1
700+
assert docstring.examples[0].args == ["examples"]
701+
assert docstring.examples[0].description == ""
702702

703703

704704
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)