summaryrefslogtreecommitdiffstats
path: root/Lib/inspect.py
diff options
context:
space:
mode:
authorNikita Sobolev <mail@sobolevn.me>2023-12-02 23:39:43 (GMT)
committerGitHub <noreply@github.com>2023-12-02 23:39:43 (GMT)
commita9574c68f04695eecd19866faaf4cdee5965bc70 (patch)
tree6c4738f5b6b8e92c7fe8716418fb8b26a633e703 /Lib/inspect.py
parent0229d2a9b1d6ce6daa6a773f92e3754e7dc86d50 (diff)
downloadcpython-a9574c68f04695eecd19866faaf4cdee5965bc70.zip
cpython-a9574c68f04695eecd19866faaf4cdee5965bc70.tar.gz
cpython-a9574c68f04695eecd19866faaf4cdee5965bc70.tar.bz2
gh-112139: Add `inspect.Signature.format` and use it in `pydoc` (#112143)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r--Lib/inspect.py12
1 files changed, 12 insertions, 0 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index aaa22be..079385a 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -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)