gh-107155: Fix help() for lambda function with return annotation (GH-107401)

This commit is contained in:
Kirill Podoprigora 2024-02-17 15:47:51 +03:00 committed by GitHub
parent 664965a1c1
commit b9a9e3dd62
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 31 additions and 2 deletions

View file

@ -1144,7 +1144,8 @@ class HTMLDoc(Doc):
# XXX lambda's won't usually have func_annotations['return'] # XXX lambda's won't usually have func_annotations['return']
# since the syntax doesn't support but it is possible. # since the syntax doesn't support but it is possible.
# So removing parentheses isn't truly safe. # 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: if not argspec:
argspec = '(...)' argspec = '(...)'
@ -1586,7 +1587,8 @@ location listed above.
# XXX lambda's won't usually have func_annotations['return'] # XXX lambda's won't usually have func_annotations['return']
# since the syntax doesn't support but it is possible. # since the syntax doesn't support but it is possible.
# So removing parentheses isn't truly safe. # So removing parentheses isn't truly safe.
argspec = argspec[1:-1] # remove parentheses if not object.__annotations__:
argspec = argspec[1:-1]
if not argspec: if not argspec:
argspec = '(...)' argspec = '(...)'
decl = asyncqualifier + title + argspec + note decl = asyncqualifier + title + argspec + note

View file

@ -693,6 +693,30 @@ class PydocDocTest(unittest.TestCase):
finally: finally:
pydoc.getpager = getpager_old 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): def test_namedtuple_fields(self):
Person = namedtuple('Person', ['nickname', 'firstname']) Person = namedtuple('Person', ['nickname', 'firstname'])
with captured_stdout() as help_io: with captured_stdout() as help_io:

View file

@ -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.