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

@ -3316,6 +3316,16 @@ class Signature:
return '<{} {}>'.format(self.__class__.__name__, self)
def __str__(self):
return self.format()
def format(self, *, max_width=None):
"""Convert signature object to string.
If *max_width* integer is passed,
signature will try to fit into the *max_width*.
If signature is longer than *max_width*,
all parameters will be on separate lines.
"""
result = []
render_pos_only_separator = False
render_kw_only_separator = True
@ -3353,6 +3363,8 @@ class Signature:
result.append('/')
rendered = '({})'.format(', '.join(result))
if max_width is not None and len(rendered) > max_width:
rendered = '(\n {}\n)'.format(',\n '.join(result))
if self.return_annotation is not _empty:
anno = formatannotation(self.return_annotation)