11"""This module defines functions and classes to parse docstrings into structured data."""
22import re
3- from typing import List , Optional
3+ from typing import List , Optional , Pattern
44
55from docstring_parser import parse
66from docstring_parser .common import Docstring , DocstringMeta
77
88from pytkdocs .parsers .docstrings .base import AnnotatedObject , Attribute , Parameter , Parser , Section , empty
99
10+ RE_DOCTEST_BLANKLINE : Pattern = re .compile (r"^\s*<BLANKLINE>\s*$" )
11+ """Regular expression to match lines of the form `<BLANKLINE>`."""
12+ RE_DOCTEST_FLAGS : Pattern = re .compile (r"(\s*#\s*doctest:.+)$" )
13+ """Regular expression to match lines containing doctest flags of the form `# doctest: +FLAG`."""
14+
1015
1116class Numpy (Parser ):
1217 """A Numpy-style docstrings parser."""
1318
14- def __init__ (self ) -> None :
19+ def __init__ (self , trim_doctest_flags : bool = True ) -> None :
1520 """
1621 Initialize the objects.
22+
23+ Arguments:
24+ trim_doctest_flags: Whether to remove doctest flags.
1725 """
1826 super ().__init__ ()
27+ self .trim_doctest_flags = trim_doctest_flags
1928 self .section_reader = {
2029 Section .Type .PARAMETERS : self .read_parameters_section ,
2130 Section .Type .EXCEPTIONS : self .read_exceptions_section ,
@@ -229,6 +238,9 @@ def read_examples_section(
229238 current_text .append (line )
230239
231240 elif in_code_example :
241+ if self .trim_doctest_flags :
242+ line = RE_DOCTEST_FLAGS .sub ("" , line )
243+ line = RE_DOCTEST_BLANKLINE .sub ("" , line )
232244 current_example .append (line )
233245
234246 elif line .startswith ("```" ):
@@ -243,15 +255,21 @@ def read_examples_section(
243255 sub_sections .append ((Section .Type .MARKDOWN , "\n " .join (current_text )))
244256 current_text = []
245257 in_code_example = True
258+
259+ if self .trim_doctest_flags :
260+ line = RE_DOCTEST_FLAGS .sub ("" , line )
246261 current_example .append (line )
247262 else :
248263 current_text .append (line )
264+
249265 if current_text :
250266 sub_sections .append ((Section .Type .MARKDOWN , "\n " .join (current_text )))
251267 elif current_example :
252268 sub_sections .append ((Section .Type .EXAMPLES , "\n " .join (current_example )))
269+
253270 if sub_sections :
254271 return Section (Section .Type .EXAMPLES , sub_sections )
272+
255273 if re .search ("Examples\n " , docstring ):
256274 self .error ("Empty examples section" )
257275 return None
0 commit comments