summaryrefslogtreecommitdiffstats
path: root/Lib/inspect.py
diff options
context:
space:
mode:
authorEthan Furman <ethan@stoneleaf.us>2022-12-16 20:30:47 (GMT)
committerGitHub <noreply@github.com>2022-12-16 20:30:47 (GMT)
commita5a7cea202d34ca699d9594d428ba527ef7ff2ce (patch)
tree5176c390d975ad480718c43245f02a78a55a2b39 /Lib/inspect.py
parent5234e1cbea686e38392f113707db322ad8405048 (diff)
downloadcpython-a5a7cea202d34ca699d9594d428ba527ef7ff2ce.zip
cpython-a5a7cea202d34ca699d9594d428ba527ef7ff2ce.tar.gz
cpython-a5a7cea202d34ca699d9594d428ba527ef7ff2ce.tar.bz2
gh-100039: enhance __signature__ to work with str and callables (GH-100168)
Callables should be either class- or static-methods. Enum now uses the classmethod version to greatly improve the help given for enums and flags.
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r--Lib/inspect.py10
1 files changed, 9 insertions, 1 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index e165937..e92c355 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -2443,10 +2443,18 @@ def _signature_from_callable(obj, *,
pass
else:
if sig is not None:
+ # since __text_signature__ is not writable on classes, __signature__
+ # may contain text (or be a callable that returns text);
+ # if so, convert it
+ o_sig = sig
+ if not isinstance(sig, (Signature, str)) and callable(sig):
+ sig = sig()
+ if isinstance(sig, str):
+ sig = _signature_fromstr(sigcls, obj, sig)
if not isinstance(sig, Signature):
raise TypeError(
'unexpected object {!r} in __signature__ '
- 'attribute'.format(sig))
+ 'attribute'.format(o_sig))
return sig
try: