]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-74044: inspect.signature for wrappers around decorated bound methods (GH-736)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 10 Nov 2022 12:59:25 +0000 (04:59 -0800)
committerGitHub <noreply@github.com>
Thu, 10 Nov 2022 12:59:25 +0000 (04:59 -0800)
(cherry picked from commit dbf2faf579b4094387d65ee41f049456ca67c446)

Co-authored-by: Anton Ryzhov <anton@ryzhov.me>
Lib/inspect.py
Lib/test/test_inspect.py
Misc/NEWS.d/next/Library/2022-11-09-20-48-42.gh-issue-74044.zBj26K.rst [new file with mode: 0644]

index 60740c63b2ae1923c58ff0d2d3add00ba850696a..ad16f5c46e3b9d6c0d08b43bad489961664eef5d 100644 (file)
@@ -2406,7 +2406,10 @@ def _signature_from_callable(obj, *,
 
     # Was this function wrapped by a decorator?
     if follow_wrapper_chains:
-        obj = unwrap(obj, stop=(lambda f: hasattr(f, "__signature__")))
+        # Unwrap until we find an explicit signature or a MethodType (which will be
+        # handled explicitly below).
+        obj = unwrap(obj, stop=(lambda f: hasattr(f, "__signature__")
+                                or isinstance(f, types.MethodType)))
         if isinstance(obj, types.MethodType):
             # If the unwrapped object is a *method*, we might want to
             # skip its first parameter (self).
index 16fef5cab18d2a9483de4e0c9fea6f5ad40e2d24..03cb3bdddc2c478b5a3624210f3ec59792f4f239 100644 (file)
@@ -2957,8 +2957,6 @@ class TestSignatureObject(unittest.TestCase):
         self.assertEqual(str(inspect.signature(foo)), '(a)')
 
     def test_signature_on_decorated(self):
-        import functools
-
         def decorator(func):
             @functools.wraps(func)
             def wrapper(*args, **kwargs) -> int:
@@ -2970,6 +2968,8 @@ class TestSignatureObject(unittest.TestCase):
             def bar(self, a, b):
                 pass
 
+        bar = decorator(Foo().bar)
+
         self.assertEqual(self.signature(Foo.bar),
                          ((('self', ..., ..., "positional_or_keyword"),
                            ('a', ..., ..., "positional_or_keyword"),
@@ -2988,6 +2988,11 @@ class TestSignatureObject(unittest.TestCase):
                                 # from "func" to "wrapper", hence no
                                 # return_annotation
 
+        self.assertEqual(self.signature(bar),
+                         ((('a', ..., ..., "positional_or_keyword"),
+                           ('b', ..., ..., "positional_or_keyword")),
+                          ...))
+
         # Test that we handle method wrappers correctly
         def decorator(func):
             @functools.wraps(func)
diff --git a/Misc/NEWS.d/next/Library/2022-11-09-20-48-42.gh-issue-74044.zBj26K.rst b/Misc/NEWS.d/next/Library/2022-11-09-20-48-42.gh-issue-74044.zBj26K.rst
new file mode 100644 (file)
index 0000000..3102ef4
--- /dev/null
@@ -0,0 +1 @@
+Fixed bug where :func:`inspect.signature` reported incorrect arguments for decorated methods.