summaryrefslogtreecommitdiffstats
path: root/Lib/inspect.py
diff options
context:
space:
mode:
authorWeipeng Hong <hongweichen8888@sina.com>2021-07-16 13:04:27 (GMT)
committerGitHub <noreply@github.com>2021-07-16 13:04:27 (GMT)
commit6aab5f9bf303a8e4cd8377fabcdcb499e0541f9a (patch)
tree2946fda6458e3e780826553446f6b006f47e40f3 /Lib/inspect.py
parent8ce3008585feed51bd08ec256a19923940d744d4 (diff)
downloadcpython-6aab5f9bf303a8e4cd8377fabcdcb499e0541f9a.zip
cpython-6aab5f9bf303a8e4cd8377fabcdcb499e0541f9a.tar.gz
cpython-6aab5f9bf303a8e4cd8377fabcdcb499e0541f9a.tar.bz2
bpo-40897:Give priority to using the current class constructor in `inspect.signature` (#27177)
Co-authored-by: Ɓukasz Langa <lukasz@langa.pl>
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r--Lib/inspect.py24
1 files changed, 16 insertions, 8 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 7a2eefe..7e9f7ce 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -2444,15 +2444,23 @@ def _signature_from_callable(obj, *,
if call is not None:
sig = _get_signature_of(call)
else:
- # Now we check if the 'obj' class has a '__new__' method
+ factory_method = None
new = _signature_get_user_defined_method(obj, '__new__')
- if new is not None:
- sig = _get_signature_of(new)
- else:
- # Finally, we should have at least __init__ implemented
- init = _signature_get_user_defined_method(obj, '__init__')
- if init is not None:
- sig = _get_signature_of(init)
+ init = _signature_get_user_defined_method(obj, '__init__')
+ # Now we check if the 'obj' class has an own '__new__' method
+ if '__new__' in obj.__dict__:
+ factory_method = new
+ # or an own '__init__' method
+ elif '__init__' in obj.__dict__:
+ factory_method = init
+ # If not, we take inherited '__new__' or '__init__', if present
+ elif new is not None:
+ factory_method = new
+ elif init is not None:
+ factory_method = init
+
+ if factory_method is not None:
+ sig = _get_signature_of(factory_method)
if sig is None:
# At this point we know, that `obj` is a class, with no user-