Skip to content

Commit

Permalink
pythongh-107155: Fix help(lambda_func) when lambda_func has `__an…
Browse files Browse the repository at this point in the history
…notations__` with `return` key.
  • Loading branch information
Eclips4 committed Jul 28, 2023
1 parent 3a1d819 commit d0d69c2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
7 changes: 3 additions & 4 deletions Lib/pydoc.py
Expand Up @@ -1500,10 +1500,9 @@ def docroutine(self, object, name=None, mod=None, cl=None):
argspec = str(signature)
if realname == '<lambda>':
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
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_pydoc.py
Expand Up @@ -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:
Expand Down

0 comments on commit d0d69c2

Please sign in to comment.