From 0beccb23c29477d43ec6106b20c03e9c1e4430ca Mon Sep 17 00:00:00 2001 From: thequackdaddy Date: Wed, 29 May 2019 00:00:53 -0500 Subject: [PATCH 01/12] TST: Desired test --- numpydoc/tests/test_docscrape.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/numpydoc/tests/test_docscrape.py b/numpydoc/tests/test_docscrape.py index 1780ec4a..40b68e87 100644 --- a/numpydoc/tests/test_docscrape.py +++ b/numpydoc/tests/test_docscrape.py @@ -1484,6 +1484,35 @@ def __init__(self, a, b): line_by_line_compare(str(doc), xref_doc_txt_expected) +def test_signature_inspect(): + + def func1(a): + pass + + def func2(a, b): + pass + + class klass: + def __init__(self, a, b): + pass + + def meth1(self, a, b): + pass + + def meth2(a, b): + pass + + @classmethod + def meth3(a, b): + pass + + assert get_doc_object(func1)['Signature'] == 'func1(a)' + assert get_doc_object(func2)['Signature'] == 'func2(a, b)' + assert get_doc_object(klass.meth1)['Signature'] == 'meth1(a, b)' + assert get_doc_object(klass.meth2)['Signature'] == 'meth2(b)' + assert get_doc_object(klass.meth3)['Signature'] == 'meth3(a, b)' + + if __name__ == "__main__": import pytest pytest.main() From 4022f5fb1b86db6de6cbc3ffd37a03945b00519c Mon Sep 17 00:00:00 2001 From: thequackdaddy Date: Fri, 31 May 2019 19:28:04 -0500 Subject: [PATCH 02/12] ENH: Allow numpydoc to defer signature creation to autodoc --- doc/install.rst | 4 ++++ numpydoc/docscrape.py | 5 ++++- numpydoc/numpydoc.py | 7 ++++++- numpydoc/tests/test_docscrape.py | 15 ++++++++++++--- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/doc/install.rst b/doc/install.rst index ba799e39..79071bc1 100644 --- a/doc/install.rst +++ b/doc/install.rst @@ -93,6 +93,10 @@ numpydoc_xref_ignore : set numpydoc_xref_ignore = {'type', 'optional', 'default'} The default is an empty set. +numpydoc_use_autodoc_signature : bool + Should numpydoc attempt to create signatures for functions or defer to + sphinx autodoc? + ``False`` by default. numpydoc_edit_link : bool .. deprecated:: edit your HTML template instead diff --git a/numpydoc/docscrape.py b/numpydoc/docscrape.py index ad0d99cd..cc36b2b8 100644 --- a/numpydoc/docscrape.py +++ b/numpydoc/docscrape.py @@ -566,7 +566,10 @@ 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: + use_autodoc_signature = config.get('use_autodoc_signature', False) + + if (not self['Signature'] and func is not None + and not use_autodoc_signature): func, func_name = self.get_func() try: try: diff --git a/numpydoc/numpydoc.py b/numpydoc/numpydoc.py index 652eedc6..85d532e7 100644 --- a/numpydoc/numpydoc.py +++ b/numpydoc/numpydoc.py @@ -202,7 +202,11 @@ 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}) + + cfg = {'use_autodoc_signature': app.config.numpydoc_use_autodoc_signature, + 'show_class_members': False} + + doc = get_doc_object(obj, config=cfg) sig = doc['Signature'] or getattr(obj, '__text_signature__', None) if sig: sig = re.sub("^[^(]*", "", sig) @@ -226,6 +230,7 @@ def setup(app, get_doc_object_=get_doc_object): app.add_config_value('numpydoc_edit_link', None, False) app.add_config_value('numpydoc_use_plots', None, False) app.add_config_value('numpydoc_use_blockquotes', None, False) + app.add_config_value('numpydoc_use_autodoc_signature', False, False) app.add_config_value('numpydoc_show_class_members', True, True) app.add_config_value('numpydoc_show_inherited_class_members', True, True) app.add_config_value('numpydoc_class_members_toctree', True, True) diff --git a/numpydoc/tests/test_docscrape.py b/numpydoc/tests/test_docscrape.py index 40b68e87..fa10262a 100644 --- a/numpydoc/tests/test_docscrape.py +++ b/numpydoc/tests/test_docscrape.py @@ -1508,9 +1508,18 @@ def meth3(a, b): assert get_doc_object(func1)['Signature'] == 'func1(a)' assert get_doc_object(func2)['Signature'] == 'func2(a, b)' - assert get_doc_object(klass.meth1)['Signature'] == 'meth1(a, b)' - assert get_doc_object(klass.meth2)['Signature'] == 'meth2(b)' - assert get_doc_object(klass.meth3)['Signature'] == 'meth3(a, b)' + assert get_doc_object(klass.meth1)['Signature'] == 'meth1(self, a, b)' + assert get_doc_object(klass.meth2)['Signature'] == 'meth2(a, b)' + assert get_doc_object(klass.meth3)['Signature'] == 'meth3(b)' + + assert get_doc_object(func1, 'use_autodoc_signature')['Signature'] == '' + assert get_doc_object(func2, 'use_autodoc_signature')['Signature'] == '' + assert get_doc_object(klass.meth1, + 'use_autodoc_signature')['Signature'] == '' + assert get_doc_object(klass.meth2, + 'use_autodoc_signature')['Signature'] == '' + assert get_doc_object(klass.meth3, + 'use_autodoc_signature')['Signature'] == '' if __name__ == "__main__": From 8e468cf2dbe56520d33818f32395cd936c3e12c1 Mon Sep 17 00:00:00 2001 From: thequackdaddy Date: Sat, 1 Jun 2019 07:43:58 -0500 Subject: [PATCH 03/12] Python 2.7 signatures for lcass methods are different --- doc/install.rst | 5 ++--- numpydoc/tests/test_docscrape.py | 5 ++++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/doc/install.rst b/doc/install.rst index 79071bc1..b434fd88 100644 --- a/doc/install.rst +++ b/doc/install.rst @@ -94,9 +94,8 @@ numpydoc_xref_ignore : set The default is an empty set. numpydoc_use_autodoc_signature : bool - Should numpydoc attempt to create signatures for functions or defer to - sphinx autodoc? - ``False`` by default. + Should numpydoc defer to Sphinx's autodoc to format the signature? + ``False`` by default. numpydoc_edit_link : bool .. deprecated:: edit your HTML template instead diff --git a/numpydoc/tests/test_docscrape.py b/numpydoc/tests/test_docscrape.py index fa10262a..2bd477da 100644 --- a/numpydoc/tests/test_docscrape.py +++ b/numpydoc/tests/test_docscrape.py @@ -1510,7 +1510,10 @@ def meth3(a, b): assert get_doc_object(func2)['Signature'] == 'func2(a, b)' assert get_doc_object(klass.meth1)['Signature'] == 'meth1(self, a, b)' assert get_doc_object(klass.meth2)['Signature'] == 'meth2(a, b)' - assert get_doc_object(klass.meth3)['Signature'] == 'meth3(b)' + if sys.version_info[0] >= 3: + assert get_doc_object(klass.meth3)['Signature'] == 'meth3(b)' + else: + assert get_doc_object(klass.meth3)['Signature'] == 'meth3(a, b)' assert get_doc_object(func1, 'use_autodoc_signature')['Signature'] == '' assert get_doc_object(func2, 'use_autodoc_signature')['Signature'] == '' From 3f7f7ce4289e83fd5283a38ab02a048598c3daad Mon Sep 17 00:00:00 2001 From: thequackdaddy Date: Thu, 1 Aug 2019 18:52:28 -0500 Subject: [PATCH 04/12] Don't generate signatures. Default to autodoc signatures --- doc/install.rst | 3 -- numpydoc/docscrape.py | 19 ------------ numpydoc/numpydoc.py | 7 +---- numpydoc/tests/test_docscrape.py | 52 -------------------------------- 4 files changed, 1 insertion(+), 80 deletions(-) diff --git a/doc/install.rst b/doc/install.rst index b434fd88..ba799e39 100644 --- a/doc/install.rst +++ b/doc/install.rst @@ -93,9 +93,6 @@ numpydoc_xref_ignore : set numpydoc_xref_ignore = {'type', 'optional', 'default'} The default is an empty set. -numpydoc_use_autodoc_signature : bool - Should numpydoc defer to Sphinx's autodoc to format the signature? - ``False`` by default. numpydoc_edit_link : bool .. deprecated:: edit your HTML template instead diff --git a/numpydoc/docscrape.py b/numpydoc/docscrape.py index cc36b2b8..7785e07c 100644 --- a/numpydoc/docscrape.py +++ b/numpydoc/docscrape.py @@ -566,25 +566,6 @@ def __init__(self, func, role='func', doc=None, config={}): doc = inspect.getdoc(func) or '' NumpyDocString.__init__(self, doc, config) - use_autodoc_signature = config.get('use_autodoc_signature', False) - - if (not self['Signature'] and func is not None - and not use_autodoc_signature): - 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__) diff --git a/numpydoc/numpydoc.py b/numpydoc/numpydoc.py index 85d532e7..652eedc6 100644 --- a/numpydoc/numpydoc.py +++ b/numpydoc/numpydoc.py @@ -202,11 +202,7 @@ def mangle_signature(app, what, name, obj, options, sig, retann): if not hasattr(obj, '__doc__'): return - - cfg = {'use_autodoc_signature': app.config.numpydoc_use_autodoc_signature, - 'show_class_members': False} - - doc = get_doc_object(obj, config=cfg) + doc = get_doc_object(obj, config={'show_class_members': False}) sig = doc['Signature'] or getattr(obj, '__text_signature__', None) if sig: sig = re.sub("^[^(]*", "", sig) @@ -230,7 +226,6 @@ def setup(app, get_doc_object_=get_doc_object): app.add_config_value('numpydoc_edit_link', None, False) app.add_config_value('numpydoc_use_plots', None, False) app.add_config_value('numpydoc_use_blockquotes', None, False) - app.add_config_value('numpydoc_use_autodoc_signature', False, False) app.add_config_value('numpydoc_show_class_members', True, True) app.add_config_value('numpydoc_show_inherited_class_members', True, True) app.add_config_value('numpydoc_class_members_toctree', True, True) diff --git a/numpydoc/tests/test_docscrape.py b/numpydoc/tests/test_docscrape.py index 2bd477da..bbf4dcca 100644 --- a/numpydoc/tests/test_docscrape.py +++ b/numpydoc/tests/test_docscrape.py @@ -691,17 +691,6 @@ def test_parameters_without_extended_description(): """) -def test_escape_stars(): - signature = str(doc3).split('\n')[0] - assert signature == r'my_signature(\*params, \*\*kwds)' - - def my_func(a, b, **kwargs): - pass - - fdoc = FunctionDoc(func=my_func) - assert fdoc['Signature'] == 'my_func(a, b, **kwargs)' - - doc4 = NumpyDocString( """a.conj() @@ -1484,47 +1473,6 @@ def __init__(self, a, b): line_by_line_compare(str(doc), xref_doc_txt_expected) -def test_signature_inspect(): - - def func1(a): - pass - - def func2(a, b): - pass - - class klass: - def __init__(self, a, b): - pass - - def meth1(self, a, b): - pass - - def meth2(a, b): - pass - - @classmethod - def meth3(a, b): - pass - - assert get_doc_object(func1)['Signature'] == 'func1(a)' - assert get_doc_object(func2)['Signature'] == 'func2(a, b)' - assert get_doc_object(klass.meth1)['Signature'] == 'meth1(self, a, b)' - assert get_doc_object(klass.meth2)['Signature'] == 'meth2(a, b)' - if sys.version_info[0] >= 3: - assert get_doc_object(klass.meth3)['Signature'] == 'meth3(b)' - else: - assert get_doc_object(klass.meth3)['Signature'] == 'meth3(a, b)' - - assert get_doc_object(func1, 'use_autodoc_signature')['Signature'] == '' - assert get_doc_object(func2, 'use_autodoc_signature')['Signature'] == '' - assert get_doc_object(klass.meth1, - 'use_autodoc_signature')['Signature'] == '' - assert get_doc_object(klass.meth2, - 'use_autodoc_signature')['Signature'] == '' - assert get_doc_object(klass.meth3, - 'use_autodoc_signature')['Signature'] == '' - - if __name__ == "__main__": import pytest pytest.main() From 96216c0e076260e746114c3baede0b8f98654191 Mon Sep 17 00:00:00 2001 From: thequackdaddy Date: Fri, 9 Aug 2019 21:23:13 -0500 Subject: [PATCH 05/12] TST: Correct for escapes before * and self in methods --- numpydoc/tests/test_full.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpydoc/tests/test_full.py b/numpydoc/tests/test_full.py index b727cde5..1732f742 100644 --- a/numpydoc/tests/test_full.py +++ b/numpydoc/tests/test_full.py @@ -58,7 +58,7 @@ def test_MyClass(sphinx_app): # 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 From 56d621ba40117ded0a27808b38a59acae08687a8 Mon Sep 17 00:00:00 2001 From: thequackdaddy Date: Sun, 21 Jun 2020 18:51:19 -0500 Subject: [PATCH 06/12] Remove $ from __text_signature__ Fix python C sigangure $ issues. Revert removal of star test. --- numpydoc/numpydoc.py | 1 + numpydoc/tests/test_docscrape.py | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/numpydoc/numpydoc.py b/numpydoc/numpydoc.py index 652eedc6..d141c42e 100644 --- a/numpydoc/numpydoc.py +++ b/numpydoc/numpydoc.py @@ -206,6 +206,7 @@ def mangle_signature(app, what, name, obj, options, sig, retann): sig = doc['Signature'] or getattr(obj, '__text_signature__', None) if sig: sig = re.sub("^[^(]*", "", sig) + sig = re.sub("$", "", sig) return sig, '' diff --git a/numpydoc/tests/test_docscrape.py b/numpydoc/tests/test_docscrape.py index bbf4dcca..1780ec4a 100644 --- a/numpydoc/tests/test_docscrape.py +++ b/numpydoc/tests/test_docscrape.py @@ -691,6 +691,17 @@ def test_parameters_without_extended_description(): """) +def test_escape_stars(): + signature = str(doc3).split('\n')[0] + assert signature == r'my_signature(\*params, \*\*kwds)' + + def my_func(a, b, **kwargs): + pass + + fdoc = FunctionDoc(func=my_func) + assert fdoc['Signature'] == 'my_func(a, b, **kwargs)' + + doc4 = NumpyDocString( """a.conj() From d2b943e7ce4db020612d647fab861a495232a14d Mon Sep 17 00:00:00 2001 From: thequackdaddy Date: Tue, 23 Jun 2020 04:24:48 -0500 Subject: [PATCH 07/12] Fix python C sigangure $ issues. Revert removal of star test. --- numpydoc/numpydoc.py | 3 ++- numpydoc/tests/test_docscrape.py | 2 +- numpydoc/tests/test_full.py | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/numpydoc/numpydoc.py b/numpydoc/numpydoc.py index d141c42e..dca245c3 100644 --- a/numpydoc/numpydoc.py +++ b/numpydoc/numpydoc.py @@ -206,7 +206,8 @@ def mangle_signature(app, what, name, obj, options, sig, retann): sig = doc['Signature'] or getattr(obj, '__text_signature__', None) if sig: sig = re.sub("^[^(]*", "", sig) - sig = re.sub("$", "", sig) + sig = re.sub(r"\$self,\s", "", sig) + sig = re.sub(r"\/,\s", "", sig) return sig, '' diff --git a/numpydoc/tests/test_docscrape.py b/numpydoc/tests/test_docscrape.py index 1780ec4a..2fb1fbda 100644 --- a/numpydoc/tests/test_docscrape.py +++ b/numpydoc/tests/test_docscrape.py @@ -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( diff --git a/numpydoc/tests/test_full.py b/numpydoc/tests/test_full.py index 1732f742..775036a2 100644 --- a/numpydoc/tests/test_full.py +++ b/numpydoc/tests/test_full.py @@ -53,6 +53,7 @@ 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: From 3c6d16d580bf3aa0314fb666106dab67e043b375 Mon Sep 17 00:00:00 2001 From: thequackdaddy Date: Tue, 23 Jun 2020 07:13:51 -0500 Subject: [PATCH 08/12] Add helper funciton to clean-up C __text_signatures__ --- numpydoc/numpydoc.py | 22 +++++++++++++++++++--- numpydoc/tests/test_numpydoc.py | 16 +++++++++++++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/numpydoc/numpydoc.py b/numpydoc/numpydoc.py index dca245c3..76c6a442 100644 --- a/numpydoc/numpydoc.py +++ b/numpydoc/numpydoc.py @@ -203,14 +203,30 @@ 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) - sig = re.sub(r"\$self,\s", "", sig) - sig = re.sub(r"\/,\s", "", sig) return sig, '' +def _clean_text_signature(sig): + if sig is None: + return None + sig = re.sub(r"^[^(]*\(", "", sig) + sig = re.sub(r"\)", "", sig) + params = sig.replace(' ', '').split(',') + if '$self' in params: + params.remove('$self') + if '$module' in params: + params.remove('$module') + if '$type' in params: + params.remove('$type') + if '/' in params: + params.remove('/') + return '(' + ', '.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 diff --git a/numpydoc/tests/test_numpydoc.py b/numpydoc/tests/test_numpydoc.py index 383ca189..a065608b 100644 --- a/numpydoc/tests/test_numpydoc.py +++ b/numpydoc/tests/test_numpydoc.py @@ -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 @@ -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)') == '()' + assert (_clean_text_signature('func($self, *args, **kwargs)') == + '(*args, **kwargs)') + assert _clean_text_signature('($self)') == '()' + assert _clean_text_signature('()') == '()' + assert _clean_text_signature('func()') == '()' + assert (_clean_text_signature('func($self, /, *args, **kwargs)') == + '(*args, **kwargs)') + assert _clean_text_signature('($module)') == '()' + assert _clean_text_signature('func($type)') == '()' + + if __name__ == "__main__": import pytest pytest.main() From aa2189d3294d83d50d92f2ef67b86207f93ea9bd Mon Sep 17 00:00:00 2001 From: thequackdaddy Date: Tue, 23 Jun 2020 10:27:53 -0500 Subject: [PATCH 09/12] PEP-8 fixes and readability --- numpydoc/numpydoc.py | 18 ++++++++---------- numpydoc/tests/test_numpydoc.py | 16 ++++++++-------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/numpydoc/numpydoc.py b/numpydoc/numpydoc.py index 76c6a442..53664c78 100644 --- a/numpydoc/numpydoc.py +++ b/numpydoc/numpydoc.py @@ -213,18 +213,16 @@ def mangle_signature(app, what, name, obj, options, sig, retann): def _clean_text_signature(sig): if sig is None: return None - sig = re.sub(r"^[^(]*\(", "", sig) + start_pattern = re.compile(r"^[^(]*\(*") + start, end = start_pattern.search(sig).span() + start_sig = sig[start:end] + sig = sig[end:] sig = re.sub(r"\)", "", sig) params = sig.replace(' ', '').split(',') - if '$self' in params: - params.remove('$self') - if '$module' in params: - params.remove('$module') - if '$type' in params: - params.remove('$type') - if '/' in params: - params.remove('/') - return '(' + ', '.join(params) + ')' + for param in ('$self', '$module', '$type', '/'): + if param in params: + params.remove(param) + return start_sig + ', '.join(params) + ')' def setup(app, get_doc_object_=get_doc_object): diff --git a/numpydoc/tests/test_numpydoc.py b/numpydoc/tests/test_numpydoc.py index a065608b..04fac9e6 100644 --- a/numpydoc/tests/test_numpydoc.py +++ b/numpydoc/tests/test_numpydoc.py @@ -36,7 +36,7 @@ class MockApp(): def test_mangle_docstrings(): - s =''' + s = ''' A top section before .. autoclass:: str @@ -66,16 +66,16 @@ def test_mangle_docstrings(): def test_clean_text_signature(): assert _clean_text_signature(None) is None - assert _clean_text_signature('func($self)') == '()' - assert (_clean_text_signature('func($self, *args, **kwargs)') == - '(*args, **kwargs)') + 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()') == '()' - assert (_clean_text_signature('func($self, /, *args, **kwargs)') == - '(*args, **kwargs)') + 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)') == '()' + assert _clean_text_signature('func($type)') == 'func()' if __name__ == "__main__": From 339cf8a6df70f2603f852424d57c405862b3bdfd Mon Sep 17 00:00:00 2001 From: thequackdaddy Date: Thu, 25 Jun 2020 09:49:56 -0500 Subject: [PATCH 10/12] Additional tests and less-strict space checking --- numpydoc/docscrape.py | 1 - numpydoc/numpydoc.py | 11 +++++------ numpydoc/tests/test_numpydoc.py | 12 ++++++++++++ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/numpydoc/docscrape.py b/numpydoc/docscrape.py index 7785e07c..807e9bc0 100644 --- a/numpydoc/docscrape.py +++ b/numpydoc/docscrape.py @@ -566,7 +566,6 @@ def __init__(self, func, role='func', doc=None, config={}): doc = inspect.getdoc(func) or '' NumpyDocString.__init__(self, doc, config) - def get_func(self): func_name = getattr(self._f, '__name__', self.__class__.__name__) if inspect.isclass(self._f): diff --git a/numpydoc/numpydoc.py b/numpydoc/numpydoc.py index 53664c78..ecde7bf7 100644 --- a/numpydoc/numpydoc.py +++ b/numpydoc/numpydoc.py @@ -203,8 +203,8 @@ 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 - _clean_text_signature(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, '' @@ -213,12 +213,11 @@ def mangle_signature(app, what, name, obj, options, sig, retann): def _clean_text_signature(sig): if sig is None: return None - start_pattern = re.compile(r"^[^(]*\(*") + start_pattern = re.compile(r"^[^(]*\(") start, end = start_pattern.search(sig).span() start_sig = sig[start:end] - sig = sig[end:] - sig = re.sub(r"\)", "", sig) - params = sig.replace(' ', '').split(',') + sig = sig[end:-1] + params = sig.split(', ') for param in ('$self', '$module', '$type', '/'): if param in params: params.remove(param) diff --git a/numpydoc/tests/test_numpydoc.py b/numpydoc/tests/test_numpydoc.py index 04fac9e6..4f0a251c 100644 --- a/numpydoc/tests/test_numpydoc.py +++ b/numpydoc/tests/test_numpydoc.py @@ -76,6 +76,18 @@ def test_clean_text_signature(): == 'func(*args, **kwargs)') assert _clean_text_signature('($module)') == '()' assert _clean_text_signature('func($type)') == 'func()' + assert (_clean_text_signature('func($self, foo="hello world")') + == 'func(foo="hello world")') + assert (_clean_text_signature("func($self, foo='hello world')") + == "func(foo='hello world')") + assert (_clean_text_signature('func(foo="hello world")') + == 'func(foo="hello world")') + assert (_clean_text_signature('func(foo="$self")') + == 'func(foo="$self")') + assert (_clean_text_signature('func($self, foo="$self")') + == 'func(foo="$self")') + assert _clean_text_signature('func(self, other)') == 'func(self, other)' + assert _clean_text_signature('func(self, $self)') == 'func(self)' if __name__ == "__main__": From 5df5f285a4b1a2b989fd8deaa9610bf86a0d2a80 Mon Sep 17 00:00:00 2001 From: thequackdaddy Date: Thu, 25 Jun 2020 20:34:03 -0500 Subject: [PATCH 11/12] Use more gentle regexp to remove $ and forward slash --- numpydoc/numpydoc.py | 8 +++----- numpydoc/tests/test_numpydoc.py | 4 +++- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/numpydoc/numpydoc.py b/numpydoc/numpydoc.py index ecde7bf7..7a77d55b 100644 --- a/numpydoc/numpydoc.py +++ b/numpydoc/numpydoc.py @@ -217,11 +217,9 @@ def _clean_text_signature(sig): start, end = start_pattern.search(sig).span() start_sig = sig[start:end] sig = sig[end:-1] - params = sig.split(', ') - for param in ('$self', '$module', '$type', '/'): - if param in params: - params.remove(param) - return start_sig + ', '.join(params) + ')' + sig = re.sub(r'^\$(self|module|type)(,\s|$)','' , sig, count=1) + sig = re.sub(r'(^|(?<=,\s))/,\s\*', '*', sig, count=1) + return start_sig + sig + ')' def setup(app, get_doc_object_=get_doc_object): diff --git a/numpydoc/tests/test_numpydoc.py b/numpydoc/tests/test_numpydoc.py index 4f0a251c..77e75400 100644 --- a/numpydoc/tests/test_numpydoc.py +++ b/numpydoc/tests/test_numpydoc.py @@ -74,6 +74,8 @@ def test_clean_text_signature(): assert _clean_text_signature('func()') == 'func()' assert (_clean_text_signature('func($self, /, *args, **kwargs)') == 'func(*args, **kwargs)') + assert (_clean_text_signature('func($self, other, /, *args, **kwargs)') + == 'func(other, *args, **kwargs)') assert _clean_text_signature('($module)') == '()' assert _clean_text_signature('func($type)') == 'func()' assert (_clean_text_signature('func($self, foo="hello world")') @@ -87,7 +89,7 @@ def test_clean_text_signature(): assert (_clean_text_signature('func($self, foo="$self")') == 'func(foo="$self")') assert _clean_text_signature('func(self, other)') == 'func(self, other)' - assert _clean_text_signature('func(self, $self)') == 'func(self)' + assert _clean_text_signature('func($self, *args)') == 'func(*args)' if __name__ == "__main__": From 88aaa5f6e251ac57d091ee385796a4b516642fe0 Mon Sep 17 00:00:00 2001 From: thequackdaddy Date: Thu, 25 Jun 2020 21:33:17 -0500 Subject: [PATCH 12/12] DOC: Add change to release notes [skip ci] --- doc/release_notes.rst | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/doc/release_notes.rst b/doc/release_notes.rst index 25c6b636..a324bd15 100644 --- a/doc/release_notes.rst +++ b/doc/release_notes.rst @@ -71,3 +71,21 @@ Additional notes - https://github.com/numpy/numpydoc/issues/215#issuecomment-568261611 - https://github.com/readthedocs/sphinx_rtd_theme/pull/838 + +1.1.0 +----- + +Fixed bugs +~~~~~~~~~~ + +- BUG: Defer to autodoc for signatures `#221 `__ (`thequackdaddy `__) + +Closed issues +~~~~~~~~~~~~~ + +- self included in list of params for method `#220 `__ + +Additional notes +~~~~~~~~~~~~~~~~ + +- Due to merging of `#221 `__, self and cls no longer will appear in method signatures.