Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cf25765

Browse files
miss-islingtongpshead
authored andcommitted
bpo-34706: Preserve subclassing in inspect.Signature.from_callable (GH-16108) (GH-16113)
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 1087383 commit cf25765

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
@@ -2367,7 +2367,7 @@ def _signature_from_callable(obj, *,
23672367
if (obj.__init__ is object.__init__ and
23682368
obj.__new__ is object.__new__):
23692369
# Return a signature of 'object' builtin.
2370-
return signature(object)
2370+
return sigcls.from_callable(object)
23712371
else:
23722372
raise ValueError(
23732373
'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
@@ -3157,14 +3157,21 @@ def test_signature_from_callable_python_obj(self):
31573157
class MySignature(inspect.Signature): pass
31583158
def foo(a, *, b:1): pass
31593159
foo_sig = MySignature.from_callable(foo)
3160-
self.assertTrue(isinstance(foo_sig, MySignature))
3160+
self.assertIsInstance(foo_sig, MySignature)
3161+
3162+
def test_signature_from_callable_class(self):
3163+
# A regression test for a class inheriting its signature from `object`.
3164+
class MySignature(inspect.Signature): pass
3165+
class foo: pass
3166+
foo_sig = MySignature.from_callable(foo)
3167+
self.assertIsInstance(foo_sig, MySignature)
31613168

31623169
@unittest.skipIf(MISSING_C_DOCSTRINGS,
31633170
"Signature information for builtins requires docstrings")
31643171
def test_signature_from_callable_builtin_obj(self):
31653172
class MySignature(inspect.Signature): pass
31663173
sig = MySignature.from_callable(_pickle.Pickler)
3167-
self.assertTrue(isinstance(sig, MySignature))
3174+
self.assertIsInstance(sig, MySignature)
31683175

31693176
def test_signature_definition_order_preserved_on_kwonly(self):
31703177
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)