Skip to content

Commit

Permalink
Fixed partial so that when no argument is provided, it is equivalen…
Browse files Browse the repository at this point in the history
…t to `wraps(f)(f)`. That is, the `__wrapped__` attribute is set. Fixed #73
  • Loading branch information
Sylvain MARIE committed Oct 8, 2021
1 parent 2fed106 commit 6191e02
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/makefun/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,10 @@ def partial(f, # type: Callable

# (1) remove/change all preset arguments from the signature
orig_sig = signature(f)
new_sig = gen_partial_sig(orig_sig, preset_pos_args, preset_kwargs, f)
if preset_pos_args or preset_kwargs:
new_sig = gen_partial_sig(orig_sig, preset_pos_args, preset_kwargs, f)
else:
new_sig = None

if _is_generator_func(f):
if sys.version_info >= (3, 3):
Expand All @@ -1125,7 +1128,8 @@ def partial_f(*args, **kwargs):

# update the doc.
# Note that partial_f is generated above with a proper __name__ and __doc__ identical to the wrapped ones
partial_f.__doc__ = gen_partial_doc(partial_f.__name__, partial_f.__doc__, orig_sig, new_sig, preset_pos_args)
if new_sig is not None:
partial_f.__doc__ = gen_partial_doc(partial_f.__name__, partial_f.__doc__, orig_sig, new_sig, preset_pos_args)

return partial_f

Expand Down
16 changes: 16 additions & 0 deletions tests/test_partial_and_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,19 @@ def f(a, b, c, **d):
makefun.partial(f, b=0)

# assert str(signature(fp_ref)) == str(signature(fp))


def test_simple_partial_copy():
"""Test that when not providing any argument to partial, it is equivalent to wraps with new sig = None"""
def f1(a):
return a + 1

f2 = makefun.partial(f1)

# make sure that this is the same as wraps
assert f2.__wrapped__ == f1

f3 = makefun.wraps(f1)(f1)
assert f3.__wrapped__ == f1

assert f2(1) == f3(1) == 2

0 comments on commit 6191e02

Please sign in to comment.