]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-118404: Fix inspect.signature() for non-comparable callables (GH-118405)
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 30 Apr 2024 12:04:16 +0000 (15:04 +0300)
committerGitHub <noreply@github.com>
Tue, 30 Apr 2024 12:04:16 +0000 (15:04 +0300)
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 1f4216f0389d28faee6416f59823809cbe19221b..d46514f4b104677479fbc2c6599d84e7a8f209e4 100644 (file)
@@ -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):
index 6b577090bdff6876db2096229ec5f1ae18d28748..fbef34eddacb3a4e515bc70ae63e6a4f291ffdcb 100644 (file)
@@ -4690,6 +4690,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.