Given an arbitrary, possibly callable object, try to create a suitable signature object. Return a (reduced func, signature) tuple, or None.
(func, as_instance, eat_self)
| 91 | |
| 92 | |
| 93 | def _get_signature_object(func, as_instance, eat_self): |
| 94 | """ |
| 95 | Given an arbitrary, possibly callable object, try to create a suitable |
| 96 | signature object. |
| 97 | Return a (reduced func, signature) tuple, or None. |
| 98 | """ |
| 99 | if isinstance(func, type) and not as_instance: |
| 100 | # If it's a type and should be modelled as a type, use __init__. |
| 101 | func = func.__init__ |
| 102 | # Skip the `self` argument in __init__ |
| 103 | eat_self = True |
| 104 | elif isinstance(func, (classmethod, staticmethod)): |
| 105 | if isinstance(func, classmethod): |
| 106 | # Skip the `cls` argument of a class method |
| 107 | eat_self = True |
| 108 | # Use the original decorated method to extract the correct function signature |
| 109 | func = func.__func__ |
| 110 | elif not isinstance(func, FunctionTypes): |
| 111 | # If we really want to model an instance of the passed type, |
| 112 | # __call__ should be looked up, not __init__. |
| 113 | try: |
| 114 | func = func.__call__ |
| 115 | except AttributeError: |
| 116 | return None |
| 117 | if eat_self: |
| 118 | sig_func = partial(func, None) |
| 119 | else: |
| 120 | sig_func = func |
| 121 | try: |
| 122 | return func, inspect.signature(sig_func) |
| 123 | except ValueError: |
| 124 | # Certain callable types are not supported by inspect.signature() |
| 125 | return None |
| 126 | |
| 127 | |
| 128 | def _check_signature(func, mock, skipfirst, instance=False): |
no test coverage detected