summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorYury Selivanov <yselivanov@sprymix.com>2015-05-20 18:30:08 (GMT)
committerYury Selivanov <yselivanov@sprymix.com>2015-05-20 18:30:08 (GMT)
commitbcd4fc161ae40aaed4c8812d0bf37aa94c6cc2df (patch)
tree0d676ad431eba126313a76b126f6bc45d83b30f1 /Lib
parent1f507a81405dd7234dd97465d68fcc8437b5da0e (diff)
downloadcpython-bcd4fc161ae40aaed4c8812d0bf37aa94c6cc2df.zip
cpython-bcd4fc161ae40aaed4c8812d0bf37aa94c6cc2df.tar.gz
cpython-bcd4fc161ae40aaed4c8812d0bf37aa94c6cc2df.tar.bz2
Issue 20691: Add follow_wrapped arg to inspect.signature/from_callable.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/inspect.py9
-rw-r--r--Lib/test/test_inspect.py16
2 files changed, 19 insertions, 6 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 13d0c7b..9ad55a9 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -2664,9 +2664,10 @@ class Signature:
return _signature_from_builtin(cls, func)
@classmethod
- def from_callable(cls, obj):
+ def from_callable(cls, obj, *, follow_wrapped=True):
"""Constructs Signature for the given callable object."""
- return _signature_from_callable(obj, sigcls=cls)
+ return _signature_from_callable(obj, sigcls=cls,
+ follow_wrapper_chains=follow_wrapped)
@property
def parameters(self):
@@ -2915,9 +2916,9 @@ class Signature:
return rendered
-def signature(obj):
+def signature(obj, *, follow_wrapped=True):
"""Get a signature object for the passed callable."""
- return Signature.from_callable(obj)
+ return Signature.from_callable(obj, follow_wrapped=follow_wrapped)
def _main():
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index 92bc096..8d11783 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -1735,8 +1735,8 @@ class MyParameter(inspect.Parameter):
class TestSignatureObject(unittest.TestCase):
@staticmethod
- def signature(func):
- sig = inspect.signature(func)
+ def signature(func, **kw):
+ sig = inspect.signature(func, **kw)
return (tuple((param.name,
(... if param.default is param.empty else param.default),
(... if param.annotation is param.empty
@@ -1956,6 +1956,11 @@ class TestSignatureObject(unittest.TestCase):
self.assertEqual(inspect.signature(func),
inspect.signature(decorated_func))
+ def wrapper_like(*args, **kwargs) -> int: pass
+ self.assertEqual(inspect.signature(decorated_func,
+ follow_wrapped=False),
+ inspect.signature(wrapper_like))
+
@cpython_only
def test_signature_on_builtins_no_signature(self):
import _testcapi
@@ -2384,6 +2389,13 @@ class TestSignatureObject(unittest.TestCase):
('b', ..., ..., "positional_or_keyword")),
...))
+ self.assertEqual(self.signature(Foo.bar, follow_wrapped=False),
+ ((('args', ..., ..., "var_positional"),
+ ('kwargs', ..., ..., "var_keyword")),
+ ...)) # functools.wraps will copy __annotations__
+ # from "func" to "wrapper", hence no
+ # return_annotation
+
# Test that we handle method wrappers correctly
def decorator(func):
@functools.wraps(func)