aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-04-30 14:33:54 +0200
committerGitHub <noreply@github.com>2024-04-30 12:33:54 +0000
commit1b12ad59703b49a9240cbe037abe3d1c2f550877 (patch)
tree3e5f31d5c6432b79e1d9cc204f2b8cab3a0c41db
parent668163865c96ae141761b23895e128c8f7a9f29a (diff)
downloadcpython3-1b12ad59703b49a9240cbe037abe3d1c2f550877.tar.gz
[3.12] gh-118404: Fix inspect.signature() for non-comparable callables (GH-118405) (GH-118424)
(cherry picked from commit 11f8348d78c22f85694d7a424541b34d6054a8ee) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
-rw-r--r--Lib/inspect.py6
-rw-r--r--Lib/test/test_inspect/test_inspect.py10
-rw-r--r--Misc/NEWS.d/next/Library/2024-04-29-22-11-54.gh-issue-118404.GYfMaD.rst1
3 files changed, 15 insertions, 2 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index e8c60b77e2..41dea936b4 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -2145,8 +2145,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):
diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py
index 6db011a1de..80e68511e4 100644
--- a/Lib/test/test_inspect/test_inspect.py
+++ b/Lib/test/test_inspect/test_inspect.py
@@ -4341,6 +4341,16 @@ class TestSignatureObject(unittest.TestCase):
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):
diff --git a/Misc/NEWS.d/next/Library/2024-04-29-22-11-54.gh-issue-118404.GYfMaD.rst b/Misc/NEWS.d/next/Library/2024-04-29-22-11-54.gh-issue-118404.GYfMaD.rst
new file mode 100644
index 0000000000..b8f9ee061a
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-04-29-22-11-54.gh-issue-118404.GYfMaD.rst
@@ -0,0 +1 @@
+Fix :func:`inspect.signature` for non-comparable callables.