Commit 5b9ff7a0 authored by Gregory P. Smith's avatar Gregory P. Smith Committed by GitHub

bpo-34706: Preserve subclassing in inspect.Signature.from_callable (GH-16108)

https://bugs.python.org/issue34706

Specifically in the case of a class that does not override its
constructor signature inherited from object.

These are Buck Evan @bukzor's changes cherrypicked from GH-9344.
parent 2ccb50cf
......@@ -2367,7 +2367,7 @@ def _signature_from_callable(obj, *,
if (obj.__init__ is object.__init__ and
obj.__new__ is object.__new__):
# Return a signature of 'object' builtin.
return signature(object)
return sigcls.from_callable(object)
else:
raise ValueError(
'no signature found for builtin type {!r}'.format(obj))
......
......@@ -3153,14 +3153,21 @@ class TestSignatureObject(unittest.TestCase):
class MySignature(inspect.Signature): pass
def foo(a, *, b:1): pass
foo_sig = MySignature.from_callable(foo)
self.assertTrue(isinstance(foo_sig, MySignature))
self.assertIsInstance(foo_sig, MySignature)
def test_signature_from_callable_class(self):
# A regression test for a class inheriting its signature from `object`.
class MySignature(inspect.Signature): pass
class foo: pass
foo_sig = MySignature.from_callable(foo)
self.assertIsInstance(foo_sig, MySignature)
@unittest.skipIf(MISSING_C_DOCSTRINGS,
"Signature information for builtins requires docstrings")
def test_signature_from_callable_builtin_obj(self):
class MySignature(inspect.Signature): pass
sig = MySignature.from_callable(_pickle.Pickler)
self.assertTrue(isinstance(sig, MySignature))
self.assertIsInstance(sig, MySignature)
def test_signature_definition_order_preserved_on_kwonly(self):
for fn in signatures_with_lexicographic_keyword_only_parameters():
......
Preserve subclassing in inspect.Signature.from_callable.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment