]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-107155: Fix help() for lambda function with return annotation (GH-107401)
authorKirill Podoprigora <kirill.bast9@mail.ru>
Sat, 17 Feb 2024 12:47:51 +0000 (15:47 +0300)
committerGitHub <noreply@github.com>
Sat, 17 Feb 2024 12:47:51 +0000 (12:47 +0000)
Lib/pydoc.py
Lib/test/test_pydoc/test_pydoc.py
Misc/NEWS.d/next/Library/2023-08-02-01-17-32.gh-issue-107155.Mj1K9L.rst [new file with mode: 0644]

index 6d145abda9d4abe71e4348a2588fec56883b2254..9bb64feca8f93e847ef50c5dd626ceb187b6dcc6 100755 (executable)
@@ -1144,7 +1144,8 @@ class HTMLDoc(Doc):
                 # 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 = '(...)'
 
@@ -1586,7 +1587,8 @@ location listed above.
                 # 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]
         if not argspec:
             argspec = '(...)'
         decl = asyncqualifier + title + argspec + note
index 0dd24e6d3473647c27a08ec1f1dbd3b69a79df23..d7a333a1103eaca75d7d6873c90891158b4f781f 100644 (file)
@@ -693,6 +693,30 @@ class PydocDocTest(unittest.TestCase):
         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 (file)
index 0000000..8362dc0
--- /dev/null
@@ -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.