Skip to content

Commit 011b761

Browse files
committed
fix numpydoc processing
fix #758
1 parent fd74e2e commit 011b761

5 files changed

Lines changed: 64 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
## Unreleased: pdoc next
66

7-
- Fix handling of URL-escaped module names ([#787](https://github.com/mitmproxy/pdoc/pull/787), @iFreilicht)
7+
- Fix a bug where invalid Numpydoc docstrings would raise an exception.
8+
- Improve debuggability of docstring processing
9+
([#784](https://github.com/mitmproxy/pdoc/pull/784), @tobiscode)
10+
- Fix handling of URL-escaped module names
11+
([#787](https://github.com/mitmproxy/pdoc/pull/787), @iFreilicht)
812

913
## 2024-12-12: pdoc 15.0.1
1014

pdoc/docstrings.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,15 @@ def numpy(docstring: str) -> str:
193193
)
194194
contents = sections[0]
195195
for heading, content in zip(sections[1::2], sections[2::2]):
196-
if content.startswith(" "):
196+
if content.startswith(" ") and "\n" in content:
197197
# If the first line of section content is indented, we consider the section to be finished
198198
# on the first non-indented line. We take out the rest - the tail - here.
199199
content, tail = re.split(r"\n(?![ \n])", content, maxsplit=1)
200200
else:
201201
tail = ""
202202

203+
content = dedent(content)
204+
203205
if heading in (
204206
"Parameters",
205207
"Returns",
@@ -214,7 +216,7 @@ def numpy(docstring: str) -> str:
214216
elif heading == "See Also":
215217
contents += f"###### {heading}\n{_numpy_seealso(content)}"
216218
else:
217-
contents += f"###### {heading}\n{dedent(content)}"
219+
contents += f"###### {heading}\n{content}"
218220
contents += tail
219221
return contents
220222

test/testdata/flavors_numpy.html

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ <h2>API Documentation</h2>
101101
<li>
102102
<a class="function" href="#invalid_format">invalid_format</a>
103103
</li>
104+
<li>
105+
<a class="function" href="#invalid_format2">invalid_format2</a>
106+
</li>
104107
</ul>
105108

106109

@@ -638,6 +641,15 @@ <h6 id="attributes">Attributes</h6>
638641
</span><span id="L-475"><a href="#L-475"><span class="linenos">475</span></a><span class="sd"> param1</span>
639642
</span><span id="L-476"><a href="#L-476"><span class="linenos">476</span></a>
640643
</span><span id="L-477"><a href="#L-477"><span class="linenos">477</span></a><span class="sd"> &quot;&quot;&quot;</span>
644+
</span><span id="L-478"><a href="#L-478"><span class="linenos">478</span></a>
645+
</span><span id="L-479"><a href="#L-479"><span class="linenos">479</span></a><span class="k">def</span><span class="w"> </span><span class="nf">invalid_format2</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
646+
</span><span id="L-480"><a href="#L-480"><span class="linenos">480</span></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
647+
</span><span id="L-481"><a href="#L-481"><span class="linenos">481</span></a><span class="sd"> Another example without description, but this time indented.</span>
648+
</span><span id="L-482"><a href="#L-482"><span class="linenos">482</span></a>
649+
</span><span id="L-483"><a href="#L-483"><span class="linenos">483</span></a><span class="sd"> Returns</span>
650+
</span><span id="L-484"><a href="#L-484"><span class="linenos">484</span></a><span class="sd"> -------</span>
651+
</span><span id="L-485"><a href="#L-485"><span class="linenos">485</span></a><span class="sd"> Text describing the return value.</span>
652+
</span><span id="L-486"><a href="#L-486"><span class="linenos">486</span></a><span class="sd"> &quot;&quot;&quot;</span>
641653
</span></pre></div>
642654

643655

@@ -1810,6 +1822,39 @@ <h6 id="parameters">Parameters</h6>
18101822
</div>
18111823

18121824

1825+
</section>
1826+
<section id="invalid_format2">
1827+
<input id="invalid_format2-view-source" class="view-source-toggle-state" type="checkbox" aria-hidden="true" tabindex="-1">
1828+
<div class="attr function">
1829+
1830+
<span class="def">def</span>
1831+
<span class="name">invalid_format2</span><span class="signature pdoc-code condensed">(<span class="return-annotation">) -> <span class="kc">None</span>:</span></span>
1832+
1833+
<label class="view-source-button" for="invalid_format2-view-source"><span>View Source</span></label>
1834+
1835+
</div>
1836+
<a class="headerlink" href="#invalid_format2"></a>
1837+
<div class="pdoc-code codehilite"><pre><span></span><span id="invalid_format2-480"><a href="#invalid_format2-480"><span class="linenos">480</span></a><span class="k">def</span><span class="w"> </span><span class="nf">invalid_format2</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
1838+
</span><span id="invalid_format2-481"><a href="#invalid_format2-481"><span class="linenos">481</span></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
1839+
</span><span id="invalid_format2-482"><a href="#invalid_format2-482"><span class="linenos">482</span></a><span class="sd"> Another example without description, but this time indented.</span>
1840+
</span><span id="invalid_format2-483"><a href="#invalid_format2-483"><span class="linenos">483</span></a>
1841+
</span><span id="invalid_format2-484"><a href="#invalid_format2-484"><span class="linenos">484</span></a><span class="sd"> Returns</span>
1842+
</span><span id="invalid_format2-485"><a href="#invalid_format2-485"><span class="linenos">485</span></a><span class="sd"> -------</span>
1843+
</span><span id="invalid_format2-486"><a href="#invalid_format2-486"><span class="linenos">486</span></a><span class="sd"> Text describing the return value.</span>
1844+
</span><span id="invalid_format2-487"><a href="#invalid_format2-487"><span class="linenos">487</span></a><span class="sd"> &quot;&quot;&quot;</span>
1845+
</span></pre></div>
1846+
1847+
1848+
<div class="docstring"><p>Another example without description, but this time indented.</p>
1849+
1850+
<h6 id="returns">Returns</h6>
1851+
1852+
<ul>
1853+
<li><strong>Text describing the return value.</strong></li>
1854+
</ul>
1855+
</div>
1856+
1857+
18131858
</section>
18141859
</main>
18151860
</body>

test/testdata/flavors_numpy.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,3 +475,12 @@ def invalid_format(test):
475475
param1
476476
477477
"""
478+
479+
def invalid_format2() -> None:
480+
"""
481+
Another example without description, but this time indented.
482+
483+
Returns
484+
-------
485+
Text describing the return value.
486+
"""

test/testdata/flavors_numpy.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@
2626
>
2727
<function def foo(var1, var2, *args, long_var_name='hi', **kwargs): ... # Summarize the functi…>
2828
<function def invalid_format(test): ... # In this example, the…>
29+
<function def invalid_format2() -> None: ... # Another example with…>
2930
>

0 commit comments

Comments
 (0)