Skip to content

Commit 3906920

Browse files
miss-islingtongpshead
authored andcommitted
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 5b9ff7a) Co-authored-by: Gregory P. Smith <[email protected]>
1 parent 77878ca commit 3906920

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

Lib/inspect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2358,7 +2358,7 @@ def _signature_from_callable(obj, *,
23582358
if (obj.__init__ is object.__init__ and
23592359
obj.__new__ is object.__new__):
23602360
# Return a signature of 'object' builtin.
2361-
return signature(object)
2361+
return sigcls.from_callable(object)
23622362
else:
23632363
raise ValueError(
23642364
'no signature found for builtin type {!r}'.format(obj))

Lib/test/test_inspect.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3050,14 +3050,21 @@ def test_signature_from_callable_python_obj(self):
30503050
class MySignature(inspect.Signature): pass
30513051
def foo(a, *, b:1): pass
30523052
foo_sig = MySignature.from_callable(foo)
3053-
self.assertTrue(isinstance(foo_sig, MySignature))
3053+
self.assertIsInstance(foo_sig, MySignature)
3054+
3055+
def test_signature_from_callable_class(self):
3056+
# A regression test for a class inheriting its signature from `object`.
3057+
class MySignature(inspect.Signature): pass
3058+
class foo: pass
3059+
foo_sig = MySignature.from_callable(foo)
3060+
self.assertIsInstance(foo_sig, MySignature)
30543061

30553062
@unittest.skipIf(MISSING_C_DOCSTRINGS,
30563063
"Signature information for builtins requires docstrings")
30573064
def test_signature_from_callable_builtin_obj(self):
30583065
class MySignature(inspect.Signature): pass
30593066
sig = MySignature.from_callable(_pickle.Pickler)
3060-
self.assertTrue(isinstance(sig, MySignature))
3067+
self.assertIsInstance(sig, MySignature)
30613068

30623069
def test_signature_definition_order_preserved_on_kwonly(self):
30633070
for fn in signatures_with_lexicographic_keyword_only_parameters():
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Preserve subclassing in inspect.Signature.from_callable.

0 commit comments

Comments
 (0)