MCPcopy Index your code
hub / github.com/python/cpython / _get_signature_object

Function _get_signature_object

Lib/unittest/mock.py:94–126  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Callers 4

_check_signatureFunction · 0.85
_set_signatureFunction · 0.85
_set_async_signatureFunction · 0.85
_mock_add_specMethod · 0.85

Calls 2

partialClass · 0.90
signatureMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…