Skip to content

Commit

Permalink
pythongh-118404: Fix inspect.signature() for non-comparable callables (
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka committed Apr 30, 2024
1 parent 5b05d45 commit 11f8348
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2179,8 +2179,10 @@ def _signature_is_builtin(obj):
ismethoddescriptor(obj) or
isinstance(obj, _NonUserDefinedCallables) or
# Can't test 'isinstance(type)' here, as it would
# also be True for regular python classes
obj in (type, object))
# also be True for regular python classes.
# Can't use the `in` operator here, as it would
# invoke the custom __eq__ method.
obj is type or obj is object)


def _signature_is_functionlike(obj):
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -4690,6 +4690,16 @@ class D2(D1):

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

def test_signature_on_non_comparable(self):
class NoncomparableCallable:
def __call__(self, a):
pass
def __eq__(self, other):
1/0
self.assertEqual(self.signature(NoncomparableCallable()),
((('a', ..., ..., 'positional_or_keyword'),),
...))


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

0 comments on commit 11f8348

Please sign in to comment.