From dff45a11b7a49d13a5aa3b9845aff64a8f889a64 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sat, 16 May 2020 14:51:18 +0900 Subject: [PATCH] Fix #7668: autodoc: wrong retann value is passed to autodoc-proccess-signature --- CHANGES | 2 ++ sphinx/ext/autodoc/__init__.py | 10 ++++++++-- tests/test_ext_autodoc.py | 22 +++++++++++++++++++++- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/CHANGES b/CHANGES index 3ee4586a9..a799c249a 100644 --- a/CHANGES +++ b/CHANGES @@ -90,6 +90,8 @@ Bugs fixed * #7362: autodoc: does not render correct signatures for built-in functions * #7654: autodoc: ``Optional[Union[foo, bar]]`` is presented as ``Union[foo, bar, None]`` +* #7668: autodoc: wrong retann value is passed to a handler of + autodoc-proccess-signature * #7551: autosummary: a nested class is indexed as non-nested class * #7535: sphinx-autogen: crashes when custom template uses inheritance * #7536: sphinx-autogen: crashes when template uses i18n feature diff --git a/sphinx/ext/autodoc/__init__.py b/sphinx/ext/autodoc/__init__.py index 80bcc0e8d..f81a53d02 100644 --- a/sphinx/ext/autodoc/__init__.py +++ b/sphinx/ext/autodoc/__init__.py @@ -389,21 +389,27 @@ class Documenter: if self.args is not None: # signature given explicitly args = "(%s)" % self.args + retann = self.retann else: # try to introspect the signature try: + retann = None try: args = self.format_args(**kwargs) except TypeError: # retry without arguments for old documenters args = self.format_args() + + if args: + matched = re.match(r'^(\(.*\))\s+->\s+(.*)$', args) + if matched: + args = matched.group(1) + retann = matched.group(2) except Exception: logger.warning(__('error while formatting arguments for %s:') % self.fullname, type='autodoc', exc_info=True) args = None - retann = self.retann - result = self.env.events.emit_firstresult('autodoc-process-signature', self.objtype, self.fullname, self.object, self.options, args, retann) diff --git a/tests/test_ext_autodoc.py b/tests/test_ext_autodoc.py index 76b970dbb..58a93dd22 100644 --- a/tests/test_ext_autodoc.py +++ b/tests/test_ext_autodoc.py @@ -161,7 +161,6 @@ def test_format_signature(app): pass assert formatsig('function', 'f', f, None, None) == '(a, b, c=1, **d)' assert formatsig('function', 'f', f, 'a, b, c, d', None) == '(a, b, c, d)' - assert formatsig('function', 'f', f, None, 'None') == '(a, b, c=1, **d) -> None' assert formatsig('function', 'g', g, None, None) == r"(a='\n')" # test for classes @@ -246,6 +245,27 @@ def test_format_signature(app): '(b, c=42, *d, **e)' +def test_autodoc_process_signature_typehints(app): + captured = [] + + def process_signature(*args): + captured.append(args) + + app.connect('autodoc-process-signature', process_signature) + + def func(x: int, y: int) -> int: + pass + + directive = make_directive_bridge(app.env) + inst = app.registry.documenters['function'](directive, 'func') + inst.fullname = 'func' + inst.object = func + inst.objpath = ['func'] + inst.format_signature() + assert captured == [(app, 'function', 'func', func, + directive.genopt, '(x: int, y: int)', 'int')] + + def test_get_doc(app): directive = make_directive_bridge(app.env)