]> granicus.if.org Git - python/commitdiff
bpo-34706: Preserve subclassing in inspect.Signature.from_callable (GH-16108) (GH...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 13 Sep 2019 17:42:35 +0000 (10:42 -0700)
committerStéphane Wirtel <stephane@wirtel.be>
Fri, 13 Sep 2019 17:42:35 +0000 (18:42 +0100)
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.
(cherry picked from commit 5b9ff7a0dcb16d6f5c3cd4f1f52e0ca6a4bde586)

Co-authored-by: Gregory P. Smith <greg@krypto.org>
Lib/inspect.py
Lib/test/test_inspect.py
Misc/NEWS.d/next/Library/2019-09-13-14-54-33.bpo-34706.HWVpOY.rst [new file with mode: 0644]

index 0a57749ccdd4587451e472c0a8571c35039785d1..c2a1ed4148e74922dd20966b02aa20bbeec4f7da 100644 (file)
@@ -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))
index 1cd4ea28939d8ac0fd8e270cef0445bc367f42e1..db9ac36f7198e5ef0c3f0c131f46b4aaa169e08d 100644 (file)
@@ -3157,14 +3157,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():
diff --git a/Misc/NEWS.d/next/Library/2019-09-13-14-54-33.bpo-34706.HWVpOY.rst b/Misc/NEWS.d/next/Library/2019-09-13-14-54-33.bpo-34706.HWVpOY.rst
new file mode 100644 (file)
index 0000000..1df3742
--- /dev/null
@@ -0,0 +1 @@
+Preserve subclassing in inspect.Signature.from_callable.