Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.10/site-packages/django/utils/inspect.py: 18%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

40 statements  

1import functools 

2import inspect 

3 

4 

5@functools.lru_cache(maxsize=512) 

6def _get_func_parameters(func, remove_first): 

7 parameters = tuple(inspect.signature(func).parameters.values()) 

8 if remove_first: 

9 parameters = parameters[1:] 

10 return parameters 

11 

12 

13def _get_callable_parameters(meth_or_func): 

14 is_method = inspect.ismethod(meth_or_func) 

15 func = meth_or_func.__func__ if is_method else meth_or_func 

16 return _get_func_parameters(func, remove_first=is_method) 

17 

18 

19ARG_KINDS = frozenset( 

20 { 

21 inspect.Parameter.POSITIONAL_ONLY, 

22 inspect.Parameter.KEYWORD_ONLY, 

23 inspect.Parameter.POSITIONAL_OR_KEYWORD, 

24 } 

25) 

26 

27 

28def get_func_args(func): 

29 params = _get_callable_parameters(func) 

30 return [param.name for param in params if param.kind in ARG_KINDS] 

31 

32 

33def get_func_full_args(func): 

34 """ 

35 Return a list of (argument name, default value) tuples. If the argument 

36 does not have a default value, omit it in the tuple. Arguments such as 

37 *args and **kwargs are also included. 

38 """ 

39 params = _get_callable_parameters(func) 

40 args = [] 

41 for param in params: 

42 name = param.name 

43 # Ignore 'self' 

44 if name == "self": 

45 continue 

46 if param.kind == inspect.Parameter.VAR_POSITIONAL: 

47 name = "*" + name 

48 elif param.kind == inspect.Parameter.VAR_KEYWORD: 

49 name = "**" + name 

50 if param.default != inspect.Parameter.empty: 

51 args.append((name, param.default)) 

52 else: 

53 args.append((name,)) 

54 return args 

55 

56 

57def func_accepts_kwargs(func): 

58 """Return True if function 'func' accepts keyword arguments **kwargs.""" 

59 return any(p for p in _get_callable_parameters(func) if p.kind == p.VAR_KEYWORD) 

60 

61 

62def func_accepts_var_args(func): 

63 """ 

64 Return True if function 'func' accepts positional arguments *args. 

65 """ 

66 return any(p for p in _get_callable_parameters(func) if p.kind == p.VAR_POSITIONAL) 

67 

68 

69def method_has_no_args(meth): 

70 """Return True if a method only accepts 'self'.""" 

71 count = len([p for p in _get_callable_parameters(meth) if p.kind in ARG_KINDS]) 

72 return count == 0 if inspect.ismethod(meth) else count == 1 

73 

74 

75def func_supports_parameter(func, name): 

76 return any(param.name == name for param in _get_callable_parameters(func))