Skip to content

Commit 37e4f25

Browse files
BUG: Defer to autodoc for signatures (#221)
1 parent 2400630 commit 37e4f25

File tree

6 files changed

+65
-22
lines changed

6 files changed

+65
-22
lines changed

doc/release_notes.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,21 @@ Additional notes
7171

7272
- https://github.com/numpy/numpydoc/issues/215#issuecomment-568261611
7373
- https://github.com/readthedocs/sphinx_rtd_theme/pull/838
74+
75+
1.1.0
76+
-----
77+
78+
Fixed bugs
79+
~~~~~~~~~~
80+
81+
- BUG: Defer to autodoc for signatures `#221 <https://github.com/numpy/numpydoc/pull/221>`__ (`thequackdaddy <https://github.com/thequackdaddy>`__)
82+
83+
Closed issues
84+
~~~~~~~~~~~~~
85+
86+
- self included in list of params for method `#220 <https://github.com/numpy/numpydoc/issues/220>`__
87+
88+
Additional notes
89+
~~~~~~~~~~~~~~~~
90+
91+
- Due to merging of `#221 <https://github.com/numpy/numpydoc/pull/221>`__, self and cls no longer will appear in method signatures.

numpydoc/docscrape.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -566,23 +566,6 @@ def __init__(self, func, role='func', doc=None, config={}):
566566
doc = inspect.getdoc(func) or ''
567567
NumpyDocString.__init__(self, doc, config)
568568

569-
if not self['Signature'] and func is not None:
570-
func, func_name = self.get_func()
571-
try:
572-
try:
573-
signature = str(inspect.signature(func))
574-
except (AttributeError, ValueError):
575-
# try to read signature, backward compat for older Python
576-
if sys.version_info[0] >= 3:
577-
argspec = inspect.getfullargspec(func)
578-
else:
579-
argspec = inspect.getargspec(func)
580-
signature = inspect.formatargspec(*argspec)
581-
signature = '%s%s' % (func_name, signature)
582-
except TypeError:
583-
signature = '%s()' % func_name
584-
self['Signature'] = signature
585-
586569
def get_func(self):
587570
func_name = getattr(self._f, '__name__', self.__class__.__name__)
588571
if inspect.isclass(self._f):

numpydoc/numpydoc.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,25 @@ def mangle_signature(app, what, name, obj, options, sig, retann):
203203
if not hasattr(obj, '__doc__'):
204204
return
205205
doc = get_doc_object(obj, config={'show_class_members': False})
206-
sig = doc['Signature'] or getattr(obj, '__text_signature__', None)
206+
sig = (doc['Signature']
207+
or _clean_text_signature(getattr(obj, '__text_signature__', None)))
207208
if sig:
208209
sig = re.sub("^[^(]*", "", sig)
209210
return sig, ''
210211

211212

213+
def _clean_text_signature(sig):
214+
if sig is None:
215+
return None
216+
start_pattern = re.compile(r"^[^(]*\(")
217+
start, end = start_pattern.search(sig).span()
218+
start_sig = sig[start:end]
219+
sig = sig[end:-1]
220+
sig = re.sub(r'^\$(self|module|type)(,\s|$)','' , sig, count=1)
221+
sig = re.sub(r'(^|(?<=,\s))/,\s\*', '*', sig, count=1)
222+
return start_sig + sig + ')'
223+
224+
212225
def setup(app, get_doc_object_=get_doc_object):
213226
if not hasattr(app, 'add_config_value'):
214227
return # probably called by nose, better bail out

numpydoc/tests/test_docscrape.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ def my_func(a, b, **kwargs):
699699
pass
700700

701701
fdoc = FunctionDoc(func=my_func)
702-
assert fdoc['Signature'] == 'my_func(a, b, **kwargs)'
702+
assert fdoc['Signature'] == ''
703703

704704

705705
doc4 = NumpyDocString(

numpydoc/tests/test_full.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ def test_MyClass(sphinx_app):
5353
html = fid.read()
5454
# ensure that no autodoc weirdness ($) occurs
5555
assert '$self' not in html
56+
assert '/,' not in html
5657
assert '__init__' in html # inherited
5758
# escaped * chars should no longer be preceded by \'s,
5859
# if we see a \* in the output we know it's incorrect:
5960
assert r'\*' not in html
6061
# "self" should not be in the parameter list for the class:
61-
assert 'self,' in html # XXX should be "not in", bug!
62+
assert 'self,' not in html
6263
# check xref was embedded properly (dict should link using xref):
6364
assert 'stdtypes.html#dict' in html
6465

numpydoc/tests/test_numpydoc.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- encoding:utf-8 -*-
22
from copy import deepcopy
3-
from numpydoc.numpydoc import mangle_docstrings
3+
from numpydoc.numpydoc import mangle_docstrings, _clean_text_signature
44
from numpydoc.xref import DEFAULT_LINKS
55
from sphinx.ext.autodoc import ALL
66

@@ -36,7 +36,7 @@ class MockApp():
3636

3737

3838
def test_mangle_docstrings():
39-
s ='''
39+
s = '''
4040
A top section before
4141
4242
.. autoclass:: str
@@ -64,6 +64,34 @@ def test_mangle_docstrings():
6464
assert 'upper' not in [x.strip() for x in lines]
6565

6666

67+
def test_clean_text_signature():
68+
assert _clean_text_signature(None) is None
69+
assert _clean_text_signature('func($self)') == 'func()'
70+
assert (_clean_text_signature('func($self, *args, **kwargs)')
71+
== 'func(*args, **kwargs)')
72+
assert _clean_text_signature('($self)') == '()'
73+
assert _clean_text_signature('()') == '()'
74+
assert _clean_text_signature('func()') == 'func()'
75+
assert (_clean_text_signature('func($self, /, *args, **kwargs)')
76+
== 'func(*args, **kwargs)')
77+
assert (_clean_text_signature('func($self, other, /, *args, **kwargs)')
78+
== 'func(other, *args, **kwargs)')
79+
assert _clean_text_signature('($module)') == '()'
80+
assert _clean_text_signature('func($type)') == 'func()'
81+
assert (_clean_text_signature('func($self, foo="hello world")')
82+
== 'func(foo="hello world")')
83+
assert (_clean_text_signature("func($self, foo='hello world')")
84+
== "func(foo='hello world')")
85+
assert (_clean_text_signature('func(foo="hello world")')
86+
== 'func(foo="hello world")')
87+
assert (_clean_text_signature('func(foo="$self")')
88+
== 'func(foo="$self")')
89+
assert (_clean_text_signature('func($self, foo="$self")')
90+
== 'func(foo="$self")')
91+
assert _clean_text_signature('func(self, other)') == 'func(self, other)'
92+
assert _clean_text_signature('func($self, *args)') == 'func(*args)'
93+
94+
6795
if __name__ == "__main__":
6896
import pytest
6997
pytest.main()

0 commit comments

Comments
 (0)