Skip to content

Commit 1b12ad5

Browse files
[3.12] gh-118404: Fix inspect.signature() for non-comparable callables (GH-118405) (GH-118424)
(cherry picked from commit 11f8348) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 6681638 commit 1b12ad5

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

Lib/inspect.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,8 +2145,10 @@ def _signature_is_builtin(obj):
21452145
ismethoddescriptor(obj) or
21462146
isinstance(obj, _NonUserDefinedCallables) or
21472147
# Can't test 'isinstance(type)' here, as it would
2148-
# also be True for regular python classes
2149-
obj in (type, object))
2148+
# also be True for regular python classes.
2149+
# Can't use the `in` operator here, as it would
2150+
# invoke the custom __eq__ method.
2151+
obj is type or obj is object)
21502152

21512153

21522154
def _signature_is_functionlike(obj):

Lib/test/test_inspect/test_inspect.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4341,6 +4341,16 @@ class D2(D1):
43414341

43424342
self.assertEqual(inspect.signature(D2), inspect.signature(D1))
43434343

4344+
def test_signature_on_non_comparable(self):
4345+
class NoncomparableCallable:
4346+
def __call__(self, a):
4347+
pass
4348+
def __eq__(self, other):
4349+
1/0
4350+
self.assertEqual(self.signature(NoncomparableCallable()),
4351+
((('a', ..., ..., 'positional_or_keyword'),),
4352+
...))
4353+
43444354

43454355
class TestParameterObject(unittest.TestCase):
43464356
def test_signature_parameter_kinds(self):
Lines changed: 1 addition & 0 deletions
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)