diff options
author | Gregory P. Smith <greg@krypto.org> | 2019-09-13 17:13:51 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-13 17:13:51 +0100 |
commit | 5b9ff7a0dcb16d6f5c3cd4f1f52e0ca6a4bde586 (patch) | |
tree | 617e86d74e51ffe2b915e2c89279c04793a838e4 | |
parent | bpo-37449: Move ensurepip off of pkgutil and to importlib.resources (GH-15109) (diff) | |
download | cpython-5b9ff7a0dcb16d6f5c3cd4f1f52e0ca6a4bde586.tar.gz cpython-5b9ff7a0dcb16d6f5c3cd4f1f52e0ca6a4bde586.tar.bz2 cpython-5b9ff7a0dcb16d6f5c3cd4f1f52e0ca6a4bde586.zip |
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.
-rw-r--r-- | Lib/inspect.py | 2 | ||||
-rw-r--r-- | Lib/test/test_inspect.py | 11 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2019-09-13-14-54-33.bpo-34706.HWVpOY.rst | 1 |
3 files changed, 11 insertions, 3 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py index 0a57749ccdd..c2a1ed4148e 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -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)) diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 6c9f5297780..7148dfa0bab 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -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(): 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 index 00000000000..1df3742abd2 --- /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. |