Skip to content

Commit f93d2b4

Browse files
committed
Fix test for #85, and update test for #66.
I am not sure the fix for #66 was correct given that __wrapped__ is used by the typing module in get_type_hints, and setting __signature__ seems to fix all other tests.
1 parent 20beb4a commit f93d2b4

2 files changed

Lines changed: 11 additions & 14 deletions

File tree

src/makefun/main.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tests/test_issues.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,10 @@ def wrapper(foo):
181181
def second_wrapper(foo, bar):
182182
return wrapper(foo) + bar
183183

184+
assert second_wrapper.__wrapped__ is a
185+
assert "bar" in inspect.signature(second_wrapper).parameters
184186
assert second_wrapper(1, -1) == 0
185187

186-
with pytest.raises(AttributeError):
187-
second_wrapper.__wrapped__
188-
189188

190189
def test_issue_pr_67():
191190
"""Test handcrafted for https://github.com/smarie/python-makefun/pull/67"""

0 commit comments

Comments
 (0)