Skip to content

gh-118404: Fix inspect.signature() for non-comparable callables #118405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading