[Python-checkins] (no subject)

Stéphane Wirtel webhook-mailer at python.org
Fri Sep 13 13:42:58 EDT 2019




To: python-checkins at python.org
Subject: bpo-34706: Preserve subclassing in inspect.Signature.from_callable
 (GH-16108) (GH-16114)
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0

https://github.com/python/cpython/commit/3906920cfecba83d529f8755f5ec2e53e49f=
9e90
commit: 3906920cfecba83d529f8755f5ec2e53e49f9e90
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.co=
m>
committer: St=C3=A9phane Wirtel <stephane at wirtel.be>
date: 2019-09-13T18:42:53+01:00
summary:

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

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 at krypto.org>

files:
A Misc/NEWS.d/next/Library/2019-09-13-14-54-33.bpo-34706.HWVpOY.rst
M Lib/inspect.py
M Lib/test/test_inspect.py

diff --git a/Lib/inspect.py b/Lib/inspect.py
index 7f0517207d15..bbecd98f94ab 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -2358,7 +2358,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(ob=
j))
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index f3eb2f62c37e..75ac40ba4f81 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -3050,14 +3050,21 @@ def test_signature_from_callable_python_obj(self):
         class MySignature(inspect.Signature): pass
         def foo(a, *, b:1): pass
         foo_sig =3D 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 `objec=
t`.
+        class MySignature(inspect.Signature): pass
+        class foo: pass
+        foo_sig =3D MySignature.from_callable(foo)
+        self.assertIsInstance(foo_sig, MySignature)
=20
     @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 =3D MySignature.from_callable(_pickle.Pickler)
-        self.assertTrue(isinstance(sig, MySignature))
+        self.assertIsInstance(sig, MySignature)
=20
     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.rs=
t b/Misc/NEWS.d/next/Library/2019-09-13-14-54-33.bpo-34706.HWVpOY.rst
new file mode 100644
index 000000000000..1df3742abd27
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-09-13-14-54-33.bpo-34706.HWVpOY.rst
@@ -0,0 +1 @@
+Preserve subclassing in inspect.Signature.from_callable.



More information about the Python-checkins mailing list