gh-112139: Add inspect.Signature.format and use it in pydoc (#112143)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
Nikita Sobolev 2023-12-03 02:39:43 +03:00 committed by GitHub
parent 0229d2a9b1
commit a9574c68f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 205 additions and 11 deletions

View file

@ -870,6 +870,95 @@ class B(A)
for expected_line in expected_lines:
self.assertIn(expected_line, as_text)
def test_long_signatures(self):
from collections.abc import Callable
from typing import Literal, Annotated
class A:
def __init__(self,
arg1: Callable[[int, int, int], str],
arg2: Literal['some value', 'other value'],
arg3: Annotated[int, 'some docs about this type'],
) -> None:
...
doc = pydoc.render_doc(A)
# clean up the extra text formatting that pydoc performs
doc = re.sub('\b.', '', doc)
self.assertEqual(doc, '''Python Library Documentation: class A in module %s
class A(builtins.object)
| A(
| arg1: collections.abc.Callable[[int, int, int], str],
| arg2: Literal['some value', 'other value'],
| arg3: Annotated[int, 'some docs about this type']
| ) -> None
|
| Methods defined here:
|
| __init__(
| self,
| arg1: collections.abc.Callable[[int, int, int], str],
| arg2: Literal['some value', 'other value'],
| arg3: Annotated[int, 'some docs about this type']
| ) -> None
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
''' % __name__)
def func(
arg1: Callable[[Annotated[int, 'Some doc']], str],
arg2: Literal[1, 2, 3, 4, 5, 6, 7, 8],
) -> Annotated[int, 'Some other']:
...
doc = pydoc.render_doc(func)
# clean up the extra text formatting that pydoc performs
doc = re.sub('\b.', '', doc)
self.assertEqual(doc, '''Python Library Documentation: function func in module %s
func(
arg1: collections.abc.Callable[[typing.Annotated[int, 'Some doc']], str],
arg2: Literal[1, 2, 3, 4, 5, 6, 7, 8]
) -> Annotated[int, 'Some other']
''' % __name__)
def function_with_really_long_name_so_annotations_can_be_rather_small(
arg1: int,
arg2: str,
):
...
doc = pydoc.render_doc(function_with_really_long_name_so_annotations_can_be_rather_small)
# clean up the extra text formatting that pydoc performs
doc = re.sub('\b.', '', doc)
self.assertEqual(doc, '''Python Library Documentation: function function_with_really_long_name_so_annotations_can_be_rather_small in module %s
function_with_really_long_name_so_annotations_can_be_rather_small(
arg1: int,
arg2: str
)
''' % __name__)
does_not_have_name = lambda \
very_long_parameter_name_that_should_not_fit_into_a_single_line, \
second_very_long_parameter_name: ...
doc = pydoc.render_doc(does_not_have_name)
# clean up the extra text formatting that pydoc performs
doc = re.sub('\b.', '', doc)
self.assertEqual(doc, '''Python Library Documentation: function <lambda> in module %s
<lambda> lambda very_long_parameter_name_that_should_not_fit_into_a_single_line, second_very_long_parameter_name
''' % __name__)
def test__future__imports(self):
# __future__ features are excluded from module help,
# except when it's the __future__ module itself