https://github.com/python/cpython/commit/e32cde54f5b086bf994cb183f67d0b1a3a378c3e
commit: e32cde54f5b086bf994cb183f67d0b1a3a378c3e
branch: 3.12
author: Kirill Podoprigora <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2024-02-17T17:14:41+02:00
summary:

[3.12] gh-107155: Fix help() for lambda function with return annotation 
(GH-115612)

(cherry picked from commit b9a9e3dd62326b726ad2e8e8efd87ca6327b4019)

files:
A Misc/NEWS.d/next/Library/2023-08-02-01-17-32.gh-issue-107155.Mj1K9L.rst
M Lib/pydoc.py
M Lib/test/test_pydoc/test_pydoc.py

diff --git a/Lib/pydoc.py b/Lib/pydoc.py
index e770b299360ac6..bf855b45134631 100755
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -1131,7 +1131,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 = '(...)'
 
@@ -1583,7 +1584,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
diff --git a/Lib/test/test_pydoc/test_pydoc.py 
b/Lib/test/test_pydoc/test_pydoc.py
index b1aa9671a97ee6..a35257c8ffc784 100644
--- a/Lib/test/test_pydoc/test_pydoc.py
+++ b/Lib/test/test_pydoc/test_pydoc.py
@@ -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:
diff --git 
a/Misc/NEWS.d/next/Library/2023-08-02-01-17-32.gh-issue-107155.Mj1K9L.rst 
b/Misc/NEWS.d/next/Library/2023-08-02-01-17-32.gh-issue-107155.Mj1K9L.rst
new file mode 100644
index 00000000000000..8362dc0fcfaa74
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-08-02-01-17-32.gh-issue-107155.Mj1K9L.rst
@@ -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.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to