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

Skip to content

Commit bf304fc

Browse files
committed
Issue #23934: Fix inspect.signature to fail correctly for builtin types.
Initial patch by James Powell.
1 parent dce09c3 commit bf304fc

3 files changed

Lines changed: 14 additions & 2 deletions

File tree

Lib/inspect.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2255,9 +2255,13 @@ def _signature_from_callable(obj, *,
22552255
if type not in obj.__mro__:
22562256
# We have a class (not metaclass), but no user-defined
22572257
# __init__ or __new__ for it
2258-
if obj.__init__ is object.__init__:
2258+
if (obj.__init__ is object.__init__ and
2259+
obj.__new__ is object.__new__):
22592260
# Return a signature of 'object' builtin.
22602261
return signature(object)
2262+
else:
2263+
raise ValueError(
2264+
'no signature found for builtin type {!r}'.format(obj))
22612265

22622266
elif not isinstance(obj, _NonUserDefinedCallables):
22632267
# An object with __call__

Lib/test/test_inspect.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1980,9 +1980,14 @@ def wrapper_like(*args, **kwargs) -> int: pass
19801980
@cpython_only
19811981
def test_signature_on_builtins_no_signature(self):
19821982
import _testcapi
1983-
with self.assertRaisesRegex(ValueError, 'no signature found for builtin'):
1983+
with self.assertRaisesRegex(ValueError,
1984+
'no signature found for builtin'):
19841985
inspect.signature(_testcapi.docstring_no_signature)
19851986

1987+
with self.assertRaisesRegex(ValueError,
1988+
'no signature found for builtin'):
1989+
inspect.signature(str)
1990+
19861991
def test_signature_on_non_function(self):
19871992
with self.assertRaisesRegex(TypeError, 'is not a callable object'):
19881993
inspect.signature(42)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ Library
3131

3232
- Issue #16991: Add a C implementation of OrderedDict.
3333

34+
- Issue #23934: Fix inspect.signature to fail correctly for builtin types
35+
lacking signature information. Initial patch by James Powell.
36+
3437

3538
What's New in Python 3.5.0 beta 1?
3639
==================================

0 commit comments

Comments
 (0)