Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(32596)

Side by Side Diff: Lib/test/test_inspect.py

Issue 20691: inspect.signature: Consider exposing 'follow_wrapper_chains' option in public API
Patch Set: Created 4 years, 4 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/inspect.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import builtins 1 import builtins
2 import collections 2 import collections
3 import datetime 3 import datetime
4 import functools 4 import functools
5 import importlib 5 import importlib
6 import inspect 6 import inspect
7 import io 7 import io
8 import linecache 8 import linecache
9 import os 9 import os
10 from os.path import normcase 10 from os.path import normcase
(...skipping 1717 matching lines...) Expand 10 before | Expand all | Expand 10 after
1728 1728
1729 class MyParameter(inspect.Parameter): 1729 class MyParameter(inspect.Parameter):
1730 # Top-level to make it picklable; 1730 # Top-level to make it picklable;
1731 # used in test_signature_object_pickle 1731 # used in test_signature_object_pickle
1732 pass 1732 pass
1733 1733
1734 1734
1735 1735
1736 class TestSignatureObject(unittest.TestCase): 1736 class TestSignatureObject(unittest.TestCase):
1737 @staticmethod 1737 @staticmethod
1738 def signature(func): 1738 def signature(func, **kw):
1739 sig = inspect.signature(func) 1739 sig = inspect.signature(func, **kw)
1740 return (tuple((param.name, 1740 return (tuple((param.name,
1741 (... if param.default is param.empty else param.default), 1741 (... if param.default is param.empty else param.default),
1742 (... if param.annotation is param.empty 1742 (... if param.annotation is param.empty
1743 else param.annotation), 1743 else param.annotation),
1744 str(param.kind).lower()) 1744 str(param.kind).lower())
1745 for param in sig.parameters.values()), 1745 for param in sig.parameters.values()),
1746 (... if sig.return_annotation is sig.empty 1746 (... if sig.return_annotation is sig.empty
1747 else sig.return_annotation)) 1747 else sig.return_annotation))
1748 1748
1749 def test_signature_object(self): 1749 def test_signature_object(self):
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
1948 def decorator(func): 1948 def decorator(func):
1949 @functools.wraps(func) 1949 @functools.wraps(func)
1950 def wrapper(*args, **kwargs) -> int: 1950 def wrapper(*args, **kwargs) -> int:
1951 return func(*args, **kwargs) 1951 return func(*args, **kwargs)
1952 return wrapper 1952 return wrapper
1953 1953
1954 decorated_func = decorator(func) 1954 decorated_func = decorator(func)
1955 1955
1956 self.assertEqual(inspect.signature(func), 1956 self.assertEqual(inspect.signature(func),
1957 inspect.signature(decorated_func)) 1957 inspect.signature(decorated_func))
1958
1959 def wrapper_like(*args, **kwargs) -> int: pass
1960 self.assertEqual(inspect.signature(decorated_func,
1961 follow_wrapped=False),
1962 inspect.signature(wrapper_like))
1958 1963
1959 @cpython_only 1964 @cpython_only
1960 def test_signature_on_builtins_no_signature(self): 1965 def test_signature_on_builtins_no_signature(self):
1961 import _testcapi 1966 import _testcapi
1962 with self.assertRaisesRegex(ValueError, 'no signature found for builtin' ): 1967 with self.assertRaisesRegex(ValueError, 'no signature found for builtin' ):
1963 inspect.signature(_testcapi.docstring_no_signature) 1968 inspect.signature(_testcapi.docstring_no_signature)
1964 1969
1965 def test_signature_on_non_function(self): 1970 def test_signature_on_non_function(self):
1966 with self.assertRaisesRegex(TypeError, 'is not a callable object'): 1971 with self.assertRaisesRegex(TypeError, 'is not a callable object'):
1967 inspect.signature(42) 1972 inspect.signature(42)
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after
2376 self.assertEqual(self.signature(Foo.bar), 2381 self.assertEqual(self.signature(Foo.bar),
2377 ((('self', ..., ..., "positional_or_keyword"), 2382 ((('self', ..., ..., "positional_or_keyword"),
2378 ('a', ..., ..., "positional_or_keyword"), 2383 ('a', ..., ..., "positional_or_keyword"),
2379 ('b', ..., ..., "positional_or_keyword")), 2384 ('b', ..., ..., "positional_or_keyword")),
2380 ...)) 2385 ...))
2381 2386
2382 self.assertEqual(self.signature(Foo().bar), 2387 self.assertEqual(self.signature(Foo().bar),
2383 ((('a', ..., ..., "positional_or_keyword"), 2388 ((('a', ..., ..., "positional_or_keyword"),
2384 ('b', ..., ..., "positional_or_keyword")), 2389 ('b', ..., ..., "positional_or_keyword")),
2385 ...)) 2390 ...))
2391
2392 self.assertEqual(self.signature(Foo.bar, follow_wrapped=False),
2393 ((('args', ..., ..., "var_positional"),
2394 ('kwargs', ..., ..., "var_keyword")),
2395 ...)) # functools.wraps will copy __annotations__
2396 # from "func" to "wrapper", hence no
2397 # return_annotation
2386 2398
2387 # Test that we handle method wrappers correctly 2399 # Test that we handle method wrappers correctly
2388 def decorator(func): 2400 def decorator(func):
2389 @functools.wraps(func) 2401 @functools.wraps(func)
2390 def wrapper(*args, **kwargs) -> int: 2402 def wrapper(*args, **kwargs) -> int:
2391 return func(42, *args, **kwargs) 2403 return func(42, *args, **kwargs)
2392 sig = inspect.signature(func) 2404 sig = inspect.signature(func)
2393 new_params = tuple(sig.parameters.values())[1:] 2405 new_params = tuple(sig.parameters.values())[1:]
2394 wrapper.__signature__ = sig.replace(parameters=new_params) 2406 wrapper.__signature__ = sig.replace(parameters=new_params)
2395 return wrapper 2407 return wrapper
(...skipping 1060 matching lines...) Expand 10 before | Expand all | Expand 10 after
3456 TestGetcallargsFunctions, TestGetcallargsMethods, 3468 TestGetcallargsFunctions, TestGetcallargsMethods,
3457 TestGetcallargsUnboundMethods, TestGetattrStatic, TestGetGeneratorState, 3469 TestGetcallargsUnboundMethods, TestGetattrStatic, TestGetGeneratorState,
3458 TestNoEOL, TestSignatureObject, TestSignatureBind, TestParameterObject, 3470 TestNoEOL, TestSignatureObject, TestSignatureBind, TestParameterObject,
3459 TestBoundArguments, TestSignaturePrivateHelpers, 3471 TestBoundArguments, TestSignaturePrivateHelpers,
3460 TestSignatureDefinitions, 3472 TestSignatureDefinitions,
3461 TestGetClosureVars, TestUnwrap, TestMain, TestReload 3473 TestGetClosureVars, TestUnwrap, TestMain, TestReload
3462 ) 3474 )
3463 3475
3464 if __name__ == "__main__": 3476 if __name__ == "__main__":
3465 test_main() 3477 test_main()
OLDNEW
« no previous file with comments | « Lib/inspect.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+