Skip to content

Commit d0d69c2

Browse files
committed
pythongh-107155: Fix help(lambda_func) when lambda_func has __annotations__ with return key.
1 parent 3a1d819 commit d0d69c2

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

Lib/pydoc.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,10 +1500,9 @@ def docroutine(self, object, name=None, mod=None, cl=None):
15001500
argspec = str(signature)
15011501
if realname == '<lambda>':
15021502
title = self.bold(name) + ' lambda '
1503-
# XXX lambda's won't usually have func_annotations['return']
1504-
# since the syntax doesn't support but it is possible.
1505-
# So removing parentheses isn't truly safe.
1506-
argspec = argspec[1:-1] # remove parentheses
1503+
# Since lambda's cannot have a parenthesis in their signature,
1504+
# it's safe to replace them.
1505+
argspec = argspec.replace("(", "").replace(")", "")
15071506
if not argspec:
15081507
argspec = '(...)'
15091508
decl = asyncqualifier + title + argspec + note

Lib/test/test_pydoc.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,14 @@ def test_help_output_redirect(self):
684684
finally:
685685
pydoc.getpager = getpager_old
686686

687+
def test_lambda_with_annotations(self):
688+
func = lambda a, b, c: 1
689+
func.__annotations__ = {"return": int}
690+
with captured_stdout() as help_io:
691+
pydoc.help(func)
692+
helptext = help_io.getvalue()
693+
self.assertIn("lambda a, b, c -> int", helptext)
694+
687695
def test_namedtuple_fields(self):
688696
Person = namedtuple('Person', ['nickname', 'firstname'])
689697
with captured_stdout() as help_io:

0 commit comments

Comments
 (0)