diff options
author | Kirill Podoprigora <kirill.bast9@mail.ru> | 2024-02-17 12:47:51 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-17 12:47:51 (GMT) |
commit | b9a9e3dd62326b726ad2e8e8efd87ca6327b4019 (patch) | |
tree | 58a4285bc5da3660c464cdee21526308bd52ddcb /Lib | |
parent | 664965a1c141e8af5eb465d29099781a6a2fc3f3 (diff) | |
download | cpython-b9a9e3dd62326b726ad2e8e8efd87ca6327b4019.zip cpython-b9a9e3dd62326b726ad2e8e8efd87ca6327b4019.tar.gz cpython-b9a9e3dd62326b726ad2e8e8efd87ca6327b4019.tar.bz2 |
gh-107155: Fix help() for lambda function with return annotation (GH-107401)
Diffstat (limited to 'Lib')
-rwxr-xr-x | Lib/pydoc.py | 6 | ||||
-rw-r--r-- | Lib/test/test_pydoc/test_pydoc.py | 24 |
2 files changed, 28 insertions, 2 deletions
diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 6d145ab..9bb64fe 100755 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -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 diff --git a/Lib/test/test_pydoc/test_pydoc.py b/Lib/test/test_pydoc/test_pydoc.py index 0dd24e6..d7a333a 100644 --- a/Lib/test/test_pydoc/test_pydoc.py +++ b/Lib/test/test_pydoc/test_pydoc.py @@ -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: |