From d0d69c255fb41aadee0370833731dbc2af24a6ff Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Fri, 28 Jul 2023 22:31:33 +0300 Subject: [PATCH] gh-107155: Fix `help(lambda_func)` when `lambda_func` has `__annotations__` with `return` key. --- Lib/pydoc.py | 7 +++---- Lib/test/test_pydoc.py | 8 ++++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 185f09e603df2e..22a7527e9237e3 100755 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -1500,10 +1500,9 @@ def docroutine(self, object, name=None, mod=None, cl=None): argspec = str(signature) if realname == '': title = self.bold(name) + ' lambda ' - # XXX lambda's won't usually have func_annotations['return'] - # since the syntax doesn't support but it is possible. - # So removing parentheses isn't truly safe. - argspec = argspec[1:-1] # remove parentheses + # Since lambda's cannot have a parenthesis in their signature, + # it's safe to replace them. + argspec = argspec.replace("(", "").replace(")", "") if not argspec: argspec = '(...)' decl = asyncqualifier + title + argspec + note diff --git a/Lib/test/test_pydoc.py b/Lib/test/test_pydoc.py index ddb5187f90da9b..8dc9a13e021c7d 100644 --- a/Lib/test/test_pydoc.py +++ b/Lib/test/test_pydoc.py @@ -684,6 +684,14 @@ def test_help_output_redirect(self): finally: pydoc.getpager = getpager_old + def test_lambda_with_annotations(self): + func = lambda a, b, c: 1 + func.__annotations__ = {"return": int} + with captured_stdout() as help_io: + pydoc.help(func) + helptext = help_io.getvalue() + self.assertIn("lambda a, b, c -> int", helptext) + def test_namedtuple_fields(self): Person = namedtuple('Person', ['nickname', 'firstname']) with captured_stdout() as help_io: