Skip to content

Commit

Permalink
Fixes references outside function (#214) (#259)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
hoxbro authored Apr 19, 2020
1 parent 40f6540 commit 7c42883
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
13 changes: 8 additions & 5 deletions numpydoc/numpydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,14 @@ def is_docstring_section(node):
for sibling_section in sibling_sections:
if not sibling_section.children:
continue
last_child = sibling_section.children[-1]
if not isinstance(last_child, comment):
continue
if last_child.rawsource.strip() == DEDUPLICATION_TAG.strip():
return True

for child in sibling_section.children[::-1]:
if not isinstance(child, comment):
continue

if child.rawsource.strip() == DEDUPLICATION_TAG.strip():
return True

return False


Expand Down
25 changes: 25 additions & 0 deletions numpydoc/tests/test_full.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os.path as op
import re
import shutil

import pytest
Expand Down Expand Up @@ -65,3 +66,27 @@ def test_my_function(sphinx_app):
assert '*args' in html
# check xref (iterable should link using xref):
assert 'glossary.html#term-iterable' in html


def test_reference(sphinx_app):
"""Test for bad references"""
out_dir = sphinx_app.outdir
html_files = [
["index.html"],
["generated", "numpydoc_test_module.my_function.html"],
["generated", "numpydoc_test_module.MyClass.html"],
]

expected_lengths = [3, 1, 1]

for html_file, expected_length in zip(html_files, expected_lengths):
html_file = op.join(out_dir, *html_file)

with open(html_file, 'r') as fid:
html = fid.read()

reference_list = re.findall(r'<a class="fn-backref" href="\#id\d+">(.*)<\/a>', html)

assert len(reference_list) == expected_length
for ref in reference_list:
assert '-' not in ref # Bad reference if it contains "-" e.g. R1896e33633d5-1
1 change: 1 addition & 0 deletions numpydoc/tests/tinybuild/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ numpydoc_test_module
====================

.. automodule:: numpydoc_test_module
:members:
22 changes: 20 additions & 2 deletions numpydoc/tests/tinybuild/numpydoc_test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
MyClass
my_function
Reference [1]_
References
----------
.. [1] https://numpydoc.readthedocs.io
"""

__all__ = ['MyClass', 'my_function']
Expand All @@ -15,22 +22,33 @@
class MyClass(object):
"""A class.
Reference [2]_
Parameters
----------
*args : iterable
Arguments.
**kwargs : dict
Keyword arguments.
References
----------
.. [2] https://numpydoc.readthedocs.io
"""

def __init__(self, *args, **kwargs):
pass

def example(self):
"""Exampel function
"""
pass


def my_function(*args, **kwargs):
"""Return None.
See [1]_.
See [3]_.
Parameters
----------
Expand All @@ -46,6 +64,6 @@ def my_function(*args, **kwargs):
References
----------
.. [1] https://numpydoc.readthedocs.io
.. [3] https://numpydoc.readthedocs.io
"""
return None

0 comments on commit 7c42883

Please sign in to comment.