Skip to content

[3.11] gh-107155: Fix help() for lambda function with return annotati… #115613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Lib/pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,8 @@ def docroutine(self, object, name=None, mod=None,
# 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
if not object.__annotations__:
argspec = argspec[1:-1] # remove parentheses
if not argspec:
argspec = '(...)'

Expand Down Expand Up @@ -1581,7 +1582,8 @@ def docroutine(self, object, name=None, mod=None, cl=None, homecls=None):
# 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
if not object.__annotations__:
argspec = argspec[1:-1] # remove parentheses
if not argspec:
argspec = '(...)'
decl = asyncqualifier + title + argspec + note
Expand Down
24 changes: 24 additions & 0 deletions Lib/test/test_pydoc/test_pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,30 @@ def test_help_output_redirect(self):
finally:
pydoc.getpager = getpager_old

def test_lambda_with_return_annotation(self):
func = lambda a, b, c: 1
func.__annotations__ = {"return": int}
with captured_output('stdout') as help_io:
pydoc.help(func)
helptext = help_io.getvalue()
self.assertIn("lambda (a, b, c) -> int", helptext)

def test_lambda_without_return_annotation(self):
func = lambda a, b, c: 1
func.__annotations__ = {"a": int, "b": int, "c": int}
with captured_output('stdout') as help_io:
pydoc.help(func)
helptext = help_io.getvalue()
self.assertIn("lambda (a: int, b: int, c: int)", helptext)

def test_lambda_with_return_and_params_annotation(self):
func = lambda a, b, c: 1
func.__annotations__ = {"a": int, "b": int, "c": int, "return": int}
with captured_output('stdout') as help_io:
pydoc.help(func)
helptext = help_io.getvalue()
self.assertIn("lambda (a: int, b: int, c: int) -> int", helptext)

def test_namedtuple_fields(self):
Person = namedtuple('Person', ['nickname', 'firstname'])
with captured_stdout() as help_io:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix incorrect output of ``help(x)`` where ``x`` is a :keyword:`lambda`
function, which has an ``__annotations__`` dictionary attribute with a
``"return"`` key.