Skip to content

Commit b9a9e3d

Browse files
authored
gh-107155: Fix help() for lambda function with return annotation (GH-107401)
1 parent 664965a commit b9a9e3d

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

Lib/pydoc.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,8 @@ def docroutine(self, object, name=None, mod=None,
11441144
# XXX lambda's won't usually have func_annotations['return']
11451145
# since the syntax doesn't support but it is possible.
11461146
# So removing parentheses isn't truly safe.
1147-
argspec = argspec[1:-1] # remove parentheses
1147+
if not object.__annotations__:
1148+
argspec = argspec[1:-1] # remove parentheses
11481149
if not argspec:
11491150
argspec = '(...)'
11501151

@@ -1586,7 +1587,8 @@ def docroutine(self, object, name=None, mod=None, cl=None, homecls=None):
15861587
# XXX lambda's won't usually have func_annotations['return']
15871588
# since the syntax doesn't support but it is possible.
15881589
# So removing parentheses isn't truly safe.
1589-
argspec = argspec[1:-1] # remove parentheses
1590+
if not object.__annotations__:
1591+
argspec = argspec[1:-1]
15901592
if not argspec:
15911593
argspec = '(...)'
15921594
decl = asyncqualifier + title + argspec + note

Lib/test/test_pydoc/test_pydoc.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,30 @@ def test_help_output_redirect(self):
693693
finally:
694694
pydoc.getpager = getpager_old
695695

696+
def test_lambda_with_return_annotation(self):
697+
func = lambda a, b, c: 1
698+
func.__annotations__ = {"return": int}
699+
with captured_output('stdout') as help_io:
700+
pydoc.help(func)
701+
helptext = help_io.getvalue()
702+
self.assertIn("lambda (a, b, c) -> int", helptext)
703+
704+
def test_lambda_without_return_annotation(self):
705+
func = lambda a, b, c: 1
706+
func.__annotations__ = {"a": int, "b": int, "c": int}
707+
with captured_output('stdout') as help_io:
708+
pydoc.help(func)
709+
helptext = help_io.getvalue()
710+
self.assertIn("lambda (a: int, b: int, c: int)", helptext)
711+
712+
def test_lambda_with_return_and_params_annotation(self):
713+
func = lambda a, b, c: 1
714+
func.__annotations__ = {"a": int, "b": int, "c": int, "return": int}
715+
with captured_output('stdout') as help_io:
716+
pydoc.help(func)
717+
helptext = help_io.getvalue()
718+
self.assertIn("lambda (a: int, b: int, c: int) -> int", helptext)
719+
696720
def test_namedtuple_fields(self):
697721
Person = namedtuple('Person', ['nickname', 'firstname'])
698722
with captured_stdout() as help_io:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix incorrect output of ``help(x)`` where ``x`` is a :keyword:`lambda`
2+
function, which has an ``__annotations__`` dictionary attribute with a
3+
``"return"`` key.

0 commit comments

Comments
 (0)