mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
gh-74044: inspect.signature for wrappers around decorated bound methods (GH-736)
This commit is contained in:
parent
9d69284169
commit
dbf2faf579
3 changed files with 12 additions and 3 deletions
|
@ -2442,7 +2442,10 @@ def _signature_from_callable(obj, *,
|
||||||
|
|
||||||
# Was this function wrapped by a decorator?
|
# Was this function wrapped by a decorator?
|
||||||
if follow_wrapper_chains:
|
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 isinstance(obj, types.MethodType):
|
||||||
# If the unwrapped object is a *method*, we might want to
|
# If the unwrapped object is a *method*, we might want to
|
||||||
# skip its first parameter (self).
|
# skip its first parameter (self).
|
||||||
|
|
|
@ -2960,8 +2960,6 @@ class TestSignatureObject(unittest.TestCase):
|
||||||
self.assertEqual(str(inspect.signature(foo)), '(a)')
|
self.assertEqual(str(inspect.signature(foo)), '(a)')
|
||||||
|
|
||||||
def test_signature_on_decorated(self):
|
def test_signature_on_decorated(self):
|
||||||
import functools
|
|
||||||
|
|
||||||
def decorator(func):
|
def decorator(func):
|
||||||
@functools.wraps(func)
|
@functools.wraps(func)
|
||||||
def wrapper(*args, **kwargs) -> int:
|
def wrapper(*args, **kwargs) -> int:
|
||||||
|
@ -2973,6 +2971,8 @@ class TestSignatureObject(unittest.TestCase):
|
||||||
def bar(self, a, b):
|
def bar(self, a, b):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
bar = decorator(Foo().bar)
|
||||||
|
|
||||||
self.assertEqual(self.signature(Foo.bar),
|
self.assertEqual(self.signature(Foo.bar),
|
||||||
((('self', ..., ..., "positional_or_keyword"),
|
((('self', ..., ..., "positional_or_keyword"),
|
||||||
('a', ..., ..., "positional_or_keyword"),
|
('a', ..., ..., "positional_or_keyword"),
|
||||||
|
@ -2991,6 +2991,11 @@ class TestSignatureObject(unittest.TestCase):
|
||||||
# from "func" to "wrapper", hence no
|
# from "func" to "wrapper", hence no
|
||||||
# return_annotation
|
# return_annotation
|
||||||
|
|
||||||
|
self.assertEqual(self.signature(bar),
|
||||||
|
((('a', ..., ..., "positional_or_keyword"),
|
||||||
|
('b', ..., ..., "positional_or_keyword")),
|
||||||
|
...))
|
||||||
|
|
||||||
# Test that we handle method wrappers correctly
|
# Test that we handle method wrappers correctly
|
||||||
def decorator(func):
|
def decorator(func):
|
||||||
@functools.wraps(func)
|
@functools.wraps(func)
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fixed bug where :func:`inspect.signature` reported incorrect arguments for decorated methods.
|
Loading…
Add table
Add a link
Reference in a new issue