Skip to content

Commit 7c42883

Browse files
authored
Fixes references outside function (#214) (#259)
* Fixes references outside function (#214) * Fixes reference in class with a method in it (#259) * Added unittest to reference * Check if reference list in test_full is correct size
1 parent 40f6540 commit 7c42883

File tree

4 files changed

+54
-7
lines changed

4 files changed

+54
-7
lines changed

numpydoc/numpydoc.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,14 @@ def is_docstring_section(node):
8888
for sibling_section in sibling_sections:
8989
if not sibling_section.children:
9090
continue
91-
last_child = sibling_section.children[-1]
92-
if not isinstance(last_child, comment):
93-
continue
94-
if last_child.rawsource.strip() == DEDUPLICATION_TAG.strip():
95-
return True
91+
92+
for child in sibling_section.children[::-1]:
93+
if not isinstance(child, comment):
94+
continue
95+
96+
if child.rawsource.strip() == DEDUPLICATION_TAG.strip():
97+
return True
98+
9699
return False
97100

98101

numpydoc/tests/test_full.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os.path as op
2+
import re
23
import shutil
34

45
import pytest
@@ -65,3 +66,27 @@ def test_my_function(sphinx_app):
6566
assert '*args' in html
6667
# check xref (iterable should link using xref):
6768
assert 'glossary.html#term-iterable' in html
69+
70+
71+
def test_reference(sphinx_app):
72+
"""Test for bad references"""
73+
out_dir = sphinx_app.outdir
74+
html_files = [
75+
["index.html"],
76+
["generated", "numpydoc_test_module.my_function.html"],
77+
["generated", "numpydoc_test_module.MyClass.html"],
78+
]
79+
80+
expected_lengths = [3, 1, 1]
81+
82+
for html_file, expected_length in zip(html_files, expected_lengths):
83+
html_file = op.join(out_dir, *html_file)
84+
85+
with open(html_file, 'r') as fid:
86+
html = fid.read()
87+
88+
reference_list = re.findall(r'<a class="fn-backref" href="\#id\d+">(.*)<\/a>', html)
89+
90+
assert len(reference_list) == expected_length
91+
for ref in reference_list:
92+
assert '-' not in ref # Bad reference if it contains "-" e.g. R1896e33633d5-1

numpydoc/tests/tinybuild/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ numpydoc_test_module
22
====================
33

44
.. automodule:: numpydoc_test_module
5+
:members:

numpydoc/tests/tinybuild/numpydoc_test_module.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
88
MyClass
99
my_function
10+
11+
Reference [1]_
12+
13+
References
14+
----------
15+
.. [1] https://numpydoc.readthedocs.io
16+
1017
"""
1118

1219
__all__ = ['MyClass', 'my_function']
@@ -15,22 +22,33 @@
1522
class MyClass(object):
1623
"""A class.
1724
25+
Reference [2]_
26+
1827
Parameters
1928
----------
2029
*args : iterable
2130
Arguments.
2231
**kwargs : dict
2332
Keyword arguments.
33+
34+
References
35+
----------
36+
.. [2] https://numpydoc.readthedocs.io
2437
"""
2538

2639
def __init__(self, *args, **kwargs):
2740
pass
2841

42+
def example(self):
43+
"""Exampel function
44+
"""
45+
pass
46+
2947

3048
def my_function(*args, **kwargs):
3149
"""Return None.
3250
33-
See [1]_.
51+
See [3]_.
3452
3553
Parameters
3654
----------
@@ -46,6 +64,6 @@ def my_function(*args, **kwargs):
4664
4765
References
4866
----------
49-
.. [1] https://numpydoc.readthedocs.io
67+
.. [3] https://numpydoc.readthedocs.io
5068
"""
5169
return None

0 commit comments

Comments
 (0)