Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2351,34 +2351,31 @@ def _signature_from_callable(obj, *,
sig = None
if isinstance(obj, type):
# obj is a class or a metaclass

create_method = None
# First, let's see if it has an overloaded __call__ defined
# in its metaclass
call = _signature_get_user_defined_method(type(obj), '__call__')
if call is not None:
create_method = call
else:
new = _signature_get_user_defined_method(obj, '__new__')
init = _signature_get_user_defined_method(obj, '__init__')
# Give priority to using the current class constructor
if '__new__' in obj.__dict__:
create_method = new
elif '__init__' in obj.__dict__:
create_method = init
elif new is not None:
create_method = new
elif init is not None:
create_method = init

if create_method is not None:
sig = _signature_from_callable(
call,
create_method,
follow_wrapper_chains=follow_wrapper_chains,
skip_bound_arg=skip_bound_arg,
sigcls=sigcls)
else:
# Now we check if the 'obj' class has a '__new__' method
new = _signature_get_user_defined_method(obj, '__new__')
if new is not None:
sig = _signature_from_callable(
new,
follow_wrapper_chains=follow_wrapper_chains,
skip_bound_arg=skip_bound_arg,
sigcls=sigcls)
else:
# Finally, we should have at least __init__ implemented
init = _signature_get_user_defined_method(obj, '__init__')
if init is not None:
sig = _signature_from_callable(
init,
follow_wrapper_chains=follow_wrapper_chains,
skip_bound_arg=skip_bound_arg,
sigcls=sigcls)

if sig is None:
# At this point we know, that `obj` is a class, with no user-
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2934,6 +2934,25 @@ def __init__(self, b):
('bar', 2, ..., "keyword_only")),
...))

def test_signature_on_subclass(self):
class A:
def __new__(cls, a=1, *args, **kwargs):
return object.__new__(cls)
class B(A):
def __init__(self, b):
pass
class C(A):
pass

self.assertEqual(self.signature(B),
((('b', ..., ..., "positional_or_keyword"),),
...))
self.assertEqual(self.signature(C),
((('a', 1, ..., 'positional_or_keyword'),
('args', ..., ..., 'var_positional'),
('kwargs', ..., ..., 'var_keyword')),
...))

@unittest.skipIf(MISSING_C_DOCSTRINGS,
"Signature information for builtins requires docstrings")
def test_signature_on_class_without_init(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Give priority to using the current class constructor in
:func:`inspect.signature`. Patch by Weipeng Hong.