@@ -246,6 +246,8 @@ def create_function(func_signature, # type: Union[str, Signature]
246246 if isinstance (func_signature , str ):
247247 # transform the string into a Signature and make sure the string contains ":"
248248 func_name_from_str , func_signature , func_signature_str = get_signature_from_string (func_signature , evaldict )
249+ if '__signature__' in attrs :
250+ attrs ['__signature__' ] = func_signature
249251
250252 # if not explicitly overridden using `func_name`, the name in the string takes over
251253 if func_name_from_str is not None :
@@ -819,9 +821,11 @@ def wraps(wrapped_fun,
819821 `wrapped_fun`, so that the created function seems to be identical (except possiblyfor the signature).
820822 Note that all options in `with_signature` can still be overrided using parameters of `@wraps`.
821823
822- If the signature is *not* modified through `new_sig`, `remove_args`, `append_args` or `prepend_args`, the
823- additional `__wrapped__` attribute on the created function, to stay consistent with the `functools.wraps`
824- behaviour.
824+ The additional `__wrapped__` attribute is set on the created function, to stay consistent
825+ with the `functools.wraps` behaviour. If the signature is modified through `new_sig`,
826+ `remove_args`, `append_args` or `prepend_args`, the additional
827+ `__signature__` attribute will be set so that `inspect.signature` and related functionality
828+ works as expected. See PEP 362 for more detail on `__wrapped__` and `__signature__`.
825829
826830 See also [python documentation on @wraps](https://docs.python.org/3/library/functools.html#functools.wraps)
827831
@@ -960,15 +964,9 @@ def _get_args_for_wrapping(wrapped, new_sig, remove_args, prepend_args, append_a
960964
961965 # attributes: start from the wrapped dict, add '__wrapped__' if needed, and override with all attrs.
962966 all_attrs = copy (getattr_partial_aware (wrapped , '__dict__' ))
967+ all_attrs .setdefault ("__wrapped__" , wrapped )
963968 if has_new_sig :
964- # change of signature: delete the __wrapped__ attribute if any
965- try :
966- del all_attrs ['__wrapped__' ]
967- except KeyError :
968- pass
969- else :
970- # no change of signature: we can safely set the __wrapped__ attribute
971- all_attrs ['__wrapped__' ] = wrapped
969+ all_attrs ["__signature__" ] = func_sig
972970 all_attrs .update (attrs )
973971
974972 return func_name , func_sig , doc , qualname , co_name , module_name , all_attrs
0 commit comments