]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-118404: Fix inspect.signature() for non-comparable callables (GH-118405...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 30 Apr 2024 12:33:54 +0000 (14:33 +0200)
committerGitHub <noreply@github.com>
Tue, 30 Apr 2024 12:33:54 +0000 (12:33 +0000)
(cherry picked from commit 11f8348d78c22f85694d7a424541b34d6054a8ee)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/inspect.py
Lib/test/test_inspect/test_inspect.py
Misc/NEWS.d/next/Library/2024-04-29-22-11-54.gh-issue-118404.GYfMaD.rst [new file with mode: 0644]

index e8c60b77e272013a61b33710e70ba97030042b6e..41dea936b4dd7aa08c00ca761dd97583b1b37732 100644 (file)
@@ -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):
index 6db011a1de3242c34934fa56fdc25fddd589974f..80e68511e428d16f800a20357c6d2ca2b51e2c33 100644 (file)
@@ -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 (file)
index 0000000..b8f9ee0
--- /dev/null
@@ -0,0 +1 @@
+Fix :func:`inspect.signature` for non-comparable callables.