Skip to content

Commit 4ee8ad2

Browse files
serhiy-storchakaSonicField
authored andcommitted
pythongh-118404: Fix inspect.signature() for non-comparable callables (pythonGH-118405)
1 parent 573070c commit 4ee8ad2

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

Lib/inspect.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -2179,8 +2179,10 @@ def _signature_is_builtin(obj):
21792179
ismethoddescriptor(obj) or
21802180
isinstance(obj, _NonUserDefinedCallables) or
21812181
# Can't test 'isinstance(type)' here, as it would
2182-
# also be True for regular python classes
2183-
obj in (type, object))
2182+
# also be True for regular python classes.
2183+
# Can't use the `in` operator here, as it would
2184+
# invoke the custom __eq__ method.
2185+
obj is type or obj is object)
21842186

21852187

21862188
def _signature_is_functionlike(obj):

Lib/test/test_inspect/test_inspect.py

+10
Original file line numberDiff line numberDiff line change
@@ -4690,6 +4690,16 @@ class D2(D1):
46904690

46914691
self.assertEqual(inspect.signature(D2), inspect.signature(D1))
46924692

4693+
def test_signature_on_non_comparable(self):
4694+
class NoncomparableCallable:
4695+
def __call__(self, a):
4696+
pass
4697+
def __eq__(self, other):
4698+
1/0
4699+
self.assertEqual(self.signature(NoncomparableCallable()),
4700+
((('a', ..., ..., 'positional_or_keyword'),),
4701+
...))
4702+
46934703

46944704
class TestParameterObject(unittest.TestCase):
46954705
def test_signature_parameter_kinds(self):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :func:`inspect.signature` for non-comparable callables.

0 commit comments

Comments
 (0)