@@ -278,11 +278,6 @@ def create_function(func_signature, # type: Union[str, Signature]
278
278
else :
279
279
raise TypeError ("Invalid type for `func_signature`: %s" % type (func_signature ))
280
280
281
- if isinstance (attrs .get ('__signature__' ), str ):
282
- # __signature__ must be a Signature object, so if it is a string,
283
- # we need to evaluate it.
284
- attrs ['__signature__' ] = get_signature_from_string (attrs ['__signature__' ], evaldict )[1 ]
285
-
286
281
# extract all information needed from the `Signature`
287
282
params_to_kw_assignment_mode = get_signature_params (func_signature )
288
283
params_names = list (params_to_kw_assignment_mode .keys ())
@@ -970,7 +965,17 @@ def _get_args_for_wrapping(wrapped, new_sig, remove_args, prepend_args, append_a
970
965
# PEP362: always set `__wrapped__`, and if signature was changed, set `__signature__` too
971
966
all_attrs ["__wrapped__" ] = wrapped
972
967
if has_new_sig :
973
- all_attrs ["__signature__" ] = func_sig
968
+ if isinstance (func_sig , Signature ):
969
+ all_attrs ["__signature__" ] = func_sig
970
+ else :
971
+ # __signature__ must be a Signature object, so if it is a string we need to evaluate it.
972
+ frame = _get_callerframe (offset = 1 )
973
+ evaldict , _ = extract_module_and_evaldict (frame )
974
+ # Here we could wish to directly override `func_name` and `func_sig` so that this does not have to be done
975
+ # again by `create_function` later... Would this be risky ?
976
+ _func_name , func_sig_as_sig , _ = get_signature_from_string (func_sig , evaldict )
977
+ all_attrs ["__signature__" ] = func_sig_as_sig
978
+
974
979
all_attrs .update (attrs )
975
980
976
981
return func_name , func_sig , doc , qualname , co_name , module_name , all_attrs
0 commit comments