Skip to content

Commit 01f8ac4

Browse files
committed
PyCQA#129 - Added a basic support for numpy section handlers
1 parent 727799d commit 01f8ac4

File tree

1 file changed

+65
-8
lines changed

1 file changed

+65
-8
lines changed

src/pydocstyle.py

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,13 +1762,27 @@ def check_numpy_content(self, definition, docstring):
17621762
"""Check the content of the docstring for numpy conventions."""
17631763
pass
17641764

1765-
@check_for(Definition)
1766-
def check_numpy(self, definition, docstring):
1767-
"""D403: First word of the first line should be properly capitalized.
1765+
def check_numpy_parameters(self, section, content, definition, docstring):
1766+
print "LALALAL"
1767+
yield
17681768

1769-
The [first line of a] docstring is a phrase ending in a period.
1769+
def _check_numpy_section(self, section, content, definition, docstring):
1770+
"""Check the content of the docstring for numpy conventions."""
1771+
method_name = "check_numpy_%s" % section
1772+
if hasattr(self, method_name):
1773+
gen_func = getattr(self, method_name)
17701774

1771-
"""
1775+
for err in gen_func(section, content, definition, docstring):
1776+
yield err
1777+
else:
1778+
print "Now checking numpy section %s" % section
1779+
for l in content:
1780+
print "##", l
1781+
1782+
1783+
@check_for(Definition)
1784+
def check_numpy(self, definition, docstring):
1785+
"""Parse the general structure of a numpy docstring and check it."""
17721786
if not docstring:
17731787
return
17741788

@@ -1780,6 +1794,10 @@ def check_numpy(self, definition, docstring):
17801794
lines_generator = ScrollableGenerator(lines[1:]) # Skipping first line
17811795
indent = self._get_docstring_indent(definition, docstring)
17821796

1797+
current_section = None
1798+
curr_section_lines = []
1799+
start_collecting_lines = False
1800+
17831801
for line in lines_generator:
17841802
for section in self.ALL_NUMPY_SECTIONS:
17851803
with_colon = section.lower() + ':'
@@ -1800,17 +1818,53 @@ def check_numpy(self, definition, docstring):
18001818
yield D214(section)
18011819

18021820
if section not in line:
1821+
# The capitalized section string is not in the line,
1822+
# meaning that the word appears there but not
1823+
# properly capitalized.
18031824
yield D405(section, line.strip())
18041825
elif line.strip().lower() == with_colon:
1826+
# The section name should not end with a colon.
18051827
yield D406(section, line.strip())
18061828

18071829
if next_line.strip() != "-" * len(section):
1830+
# The length of the underlining dashes does not
1831+
# match the length of the section name.
18081832
yield D407(section, len(section))
1833+
1834+
# At this point, we're done with the structured part of
1835+
# the section and its underline.
1836+
# We will not collect the content of each section and
1837+
# let section handlers deal with it.
1838+
1839+
if current_section is not None:
1840+
for err in self._check_numpy_section(
1841+
current_section,
1842+
curr_section_lines,
1843+
definition,
1844+
docstring):
1845+
yield err
1846+
1847+
start_collecting_lines = True
1848+
current_section = section.lower()
1849+
curr_section_lines = []
18091850
else:
18101851
# The next line does not contain only dashes, so it's
18111852
# not likely to be a section header.
18121853
lines_generator.scroll_back()
18131854

1855+
if current_section is not None:
1856+
if start_collecting_lines:
1857+
start_collecting_lines = False
1858+
else:
1859+
curr_section_lines.append(line)
1860+
1861+
if current_section is not None:
1862+
for err in self._check_numpy_section(current_section,
1863+
curr_section_lines,
1864+
definition,
1865+
docstring):
1866+
yield err
1867+
18141868

18151869
class ScrollableGenerator(object):
18161870
"""A generator over a list that can be moved back during iteration."""
@@ -1860,9 +1914,12 @@ def foo():
18601914
Parameters
18611915
----------
18621916
1863-
This is a string that defines some things, such as the following
1864-
parameters
1865-
a, b, d.
1917+
This is a string that defines some things.
1918+
1919+
Returns
1920+
-------
1921+
1922+
Nothing.
18661923
"""
18671924

18681925
if __name__ == '__main__':

0 commit comments

Comments
 (0)