Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Defer to autodoc for signatures #221

Merged
merged 12 commits into from
Jun 27, 2020
Merged
16 changes: 0 additions & 16 deletions numpydoc/docscrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,22 +566,6 @@ def __init__(self, func, role='func', doc=None, config={}):
doc = inspect.getdoc(func) or ''
NumpyDocString.__init__(self, doc, config)

if not self['Signature'] and func is not None:
func, func_name = self.get_func()
try:
try:
signature = str(inspect.signature(func))
except (AttributeError, ValueError):
# try to read signature, backward compat for older Python
if sys.version_info[0] >= 3:
argspec = inspect.getfullargspec(func)
else:
argspec = inspect.getargspec(func)
signature = inspect.formatargspec(*argspec)
signature = '%s%s' % (func_name, signature)
except TypeError:
signature = '%s()' % func_name
self['Signature'] = signature

def get_func(self):
func_name = getattr(self._f, '__name__', self.__class__.__name__)
Expand Down
18 changes: 17 additions & 1 deletion numpydoc/numpydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,28 @@ def mangle_signature(app, what, name, obj, options, sig, retann):
if not hasattr(obj, '__doc__'):
return
doc = get_doc_object(obj, config={'show_class_members': False})
sig = doc['Signature'] or getattr(obj, '__text_signature__', None)
sig = (doc['Signature'] or
_clean_text_signature(getattr(obj, '__text_signature__', None)))
if sig:
sig = re.sub("^[^(]*", "", sig)
return sig, ''


def _clean_text_signature(sig):
if sig is None:
return None
start_pattern = re.compile(r"^[^(]*\(*")
thequackdaddy marked this conversation as resolved.
Show resolved Hide resolved
start, end = start_pattern.search(sig).span()
start_sig = sig[start:end]
sig = sig[end:]
sig = re.sub(r"\)", "", sig)
thequackdaddy marked this conversation as resolved.
Show resolved Hide resolved
params = sig.replace(' ', '').split(',')
thequackdaddy marked this conversation as resolved.
Show resolved Hide resolved
for param in ('$self', '$module', '$type', '/'):
if param in params:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it'll happen often, but I'm still not comfortable with the fact that this processes all parts of the sig. Can't we check explicitly that the / precedes a *, and that the $ is at the beginning, rather than use the split and join logic?

sig = re.sub(r'^\$(self|module|type), ', '', sig, count=1)
sig = re.sub(r', /, \*', ', *', sig, count=1)

Is this not sufficient?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll confess that I have (almost) no understanding of how these C text signature things work.

What you have would make it so func($self) would still be func($self). Is that alright? I have no idea what the possible syntax is for this.

If you (or someone else) has some kind of documentation on what is possible, that would make this back-and-forth much easier.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I meant to apply those substitutions after

    start_pattern = re.compile(r"^[^(]*\(")
    start, end = start_pattern.search(sig).span()
    start_sig = sig[start:end]
    sig = sig[end:-1]

They're just a bit more careful than splitting on every comma.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I think this makes sense. Let me change that.

To handle func($self), I guess you could just add a ’, ‘ right before your code. Then use regex to strip off a trailing ’, ‘ if it exists.

Do you think that would work?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, didn't handle that case, did I?
Can use r'^\$(self|module|type)(, |$))?' which will strip a comma, or check that we're at the end.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take a peek at this. I had to do something to check whether the / was the start of the string or preceded by a , .

params.remove(param)
return start_sig + ', '.join(params) + ')'


def setup(app, get_doc_object_=get_doc_object):
if not hasattr(app, 'add_config_value'):
return # probably called by nose, better bail out
Expand Down
2 changes: 1 addition & 1 deletion numpydoc/tests/test_docscrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ def my_func(a, b, **kwargs):
pass

fdoc = FunctionDoc(func=my_func)
assert fdoc['Signature'] == 'my_func(a, b, **kwargs)'
assert fdoc['Signature'] == ''


doc4 = NumpyDocString(
Expand Down
3 changes: 2 additions & 1 deletion numpydoc/tests/test_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ def test_MyClass(sphinx_app):
html = fid.read()
# ensure that no autodoc weirdness ($) occurs
assert '$self' not in html
assert '/,' not in html
assert '__init__' in html # inherited
# escaped * chars should no longer be preceded by \'s,
# if we see a \* in the output we know it's incorrect:
assert r'\*' not in html
# "self" should not be in the parameter list for the class:
assert 'self,' in html # XXX should be "not in", bug!
assert 'self,' not in html
# check xref was embedded properly (dict should link using xref):
assert 'stdtypes.html#dict' in html

Expand Down
18 changes: 16 additions & 2 deletions numpydoc/tests/test_numpydoc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- encoding:utf-8 -*-
from copy import deepcopy
from numpydoc.numpydoc import mangle_docstrings
from numpydoc.numpydoc import mangle_docstrings, _clean_text_signature
from numpydoc.xref import DEFAULT_LINKS
from sphinx.ext.autodoc import ALL

Expand Down Expand Up @@ -36,7 +36,7 @@ class MockApp():


def test_mangle_docstrings():
s ='''
s = '''
A top section before

.. autoclass:: str
Expand Down Expand Up @@ -64,6 +64,20 @@ def test_mangle_docstrings():
assert 'upper' not in [x.strip() for x in lines]


def test_clean_text_signature():
assert _clean_text_signature(None) is None
assert _clean_text_signature('func($self)') == 'func()'
assert (_clean_text_signature('func($self, *args, **kwargs)')
== 'func(*args, **kwargs)')
assert _clean_text_signature('($self)') == '()'
assert _clean_text_signature('()') == '()'
assert _clean_text_signature('func()') == 'func()'
assert (_clean_text_signature('func($self, /, *args, **kwargs)')
== 'func(*args, **kwargs)')
assert _clean_text_signature('($module)') == '()'
assert _clean_text_signature('func($type)') == 'func()'

thequackdaddy marked this conversation as resolved.
Show resolved Hide resolved

if __name__ == "__main__":
import pytest
pytest.main()