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

improve section detection #327

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
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
28 changes: 24 additions & 4 deletions numpydoc/docscrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def is_unindented(line):
return self.read_to_condition(is_unindented)

def peek(self, n=0):
if self._l + n < len(self._str):
if 0 <= self._l + n < len(self._str):
return self[self._l + n]
else:
return ""
Expand Down Expand Up @@ -204,14 +204,34 @@ def _strip(self, doc):

return doc[i : len(doc) - j]

def read_to_next_empty_line(self):
data = []
while True:
old_line = self._doc._l
if self._is_at_section() and self._doc.peek(-1).strip() != "":
section_name = self._doc.peek()
self._error_location(
"missing empty line before the %s section" % section_name,
error=False,
)
self._doc._l = old_line

current = self._doc.read()
if not current.strip():
break

data.append(current)

return data

def _read_to_next_section(self):
section = self._doc.read_to_next_empty_line()
section = self.read_to_next_empty_line()

while not self._is_at_section() and not self._doc.eof():
if not self._doc.peek(-1).strip(): # previous line was empty
section += [""]

section += self._doc.read_to_next_empty_line()
section += self.read_to_next_empty_line()

return section

Expand Down Expand Up @@ -376,7 +396,7 @@ def _parse_summary(self):

# If several signatures present, take the last one
while True:
summary = self._doc.read_to_next_empty_line()
summary = self.read_to_next_empty_line()
summary_str = " ".join([s.strip() for s in summary]).strip()
compiled = re.compile(r"^([\w., ]+=)?\s*[\w\.]+\(.*\)$")
if compiled.match(summary_str):
Expand Down
37 changes: 37 additions & 0 deletions numpydoc/tests/test_docscrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,43 @@ def test_no_summary():
)


@pytest.mark.parametrize(
["docstring", "expected_warning"],
(
(
"""\
Parameters without separating empty line:
Parameters
----------
data
some parameter
""",
"missing empty line before the Parameters section",
),
(
"""\
Parameters
----------
data
some parameter
Returns
-------
int
""",
"missing empty line before the Returns section",
),
),
ids=[
"missing empty line after summary",
"missing empty line between sections",
],
)
def test_section_detection_warnings(docstring, expected_warning):
warning_re = ".*%s.*" % expected_warning
with pytest.warns(UserWarning, match=warning_re):
_ = NumpyDocString(docstring)


def test_unicode():
doc = SphinxDocString(
"""
Expand Down