Skip to content

ENH: Better warning for sections. #278

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

Merged
merged 2 commits into from
Jul 14, 2020
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
8 changes: 6 additions & 2 deletions numpydoc/docscrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ def _is_at_section(self):
return True

l2 = self._doc.peek(1).strip() # ---------- or ==========
if len(l2) >= 3 and (set(l2) in ({'-'}, {'='}) ) and len(l2) != len(l1):
snip = '\n'.join(self._doc._str[:2])+'...'
self._error_location("potentially wrong underline length... \n%s \n%s in \n%s"\
% (l1, l2, snip), error=False)
return l2.startswith('-'*len(l1)) or l2.startswith('='*len(l1))

def _strip(self, doc):
Expand Down Expand Up @@ -387,8 +391,8 @@ def _parse(self):
section = (s.capitalize() for s in section.split(' '))
section = ' '.join(section)
if self.get(section):
self._error_location("The section %s appears twice"
% section)
self._error_location("The section %s appears twice in %s"
% (section, '\n'.join(self._doc._str)))

if section in ('Parameters', 'Other Parameters', 'Attributes',
'Methods'):
Expand Down
30 changes: 30 additions & 0 deletions numpydoc/tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,22 @@ def directives_without_two_colons(self, first, second):
"""
pass

class WarnGenericFormat:
"""
Those contains things that _may_ be incorrect formatting.
"""

def too_short_header_underline(self, a, b):
"""
The header line is too short.

Parameters
------
a, b : int
Foo bar baz.
"""
pass


class BadSummaries:
def no_summary(self):
Expand Down Expand Up @@ -1037,6 +1053,20 @@ def test_bad_class(self, capsys):
assert isinstance(errors, list)
assert errors

@pytest.mark.parametrize(
"func",
[
"too_short_header_underline",
],
)
def test_bad_generic_functions(self, capsys, func):
with pytest.warns(UserWarning):
errors = validate_one(
self._import_path(klass="WarnGenericFormat", func=func) # noqa:F821
)
assert 'is too short' in w.msg


@pytest.mark.parametrize(
"func",
[
Expand Down