@@ -128,25 +128,39 @@ def wrapper%(wrapper_args)s:
128128
129129
130130def get_wrapped (func , responses ):
131- # Preserve the argspec for the wrapped function so that testing
132- # tools such as pytest can continue to use their fixture injection.
133- is_bound_method = hasattr (func , "__self__" )
134-
135131 if six .PY2 :
136132 args , a , kw , defaults = inspect .getargspec (func )
137133 wrapper_args = inspect .formatargspec (args , a , kw , defaults )
138- if is_bound_method :
134+
135+ # Preserve the argspec for the wrapped function so that testing
136+ # tools such as pytest can continue to use their fixture injection.
137+ if hasattr (func , "__self__" ):
139138 args = args [1 :] # Omit 'self'
140139 func_args = inspect .formatargspec (args , a , kw , None )
141140 else :
142141 signature = inspect .signature (func )
143- wrapper_args = str (signature )
144- if is_bound_method :
145- new_params = list (signature .parameters .values ())[1 :] # omit 'self'
146- signature = signature .replace (parameters = new_params )
142+ signature = signature .replace (return_annotation = inspect .Signature .empty )
143+ # If the function is wrapped, switch to *args, **kwargs for the parameters
144+ # as we can't rely on the signature to give us the arguments the function will
145+ # be called with. For example unittest.mock.patch uses required args that are
146+ # not actually passed to the function when invoked.
147+ if hasattr (func , "__wrapped__" ):
148+ wrapper_params = [
149+ inspect .Parameter ("args" , inspect .Parameter .VAR_POSITIONAL ),
150+ inspect .Parameter ("kwargs" , inspect .Parameter .VAR_KEYWORD ),
151+ ]
152+ else :
153+ wrapper_params = [
154+ param .replace (annotation = inspect .Parameter .empty )
155+ for param in signature .parameters .values ()
156+ ]
157+ signature = signature .replace (parameters = wrapper_params )
147158
159+ wrapper_args = str (signature )
148160 params_without_defaults = [
149- param .replace (default = inspect .Parameter .empty )
161+ param .replace (
162+ annotation = inspect .Parameter .empty , default = inspect .Parameter .empty
163+ )
150164 for param in signature .parameters .values ()
151165 ]
152166 signature = signature .replace (parameters = params_without_defaults )
@@ -158,10 +172,7 @@ def get_wrapped(func, responses):
158172 evaldict ,
159173 )
160174 wrapper = evaldict ["wrapper" ]
161-
162175 update_wrapper (wrapper , func )
163- if is_bound_method :
164- wrapper = wrapper .__get__ (func .__self__ , type (func .__self__ ))
165176 return wrapper
166177
167178
0 commit comments