Skip to content

Commit

Permalink
fix: make copy_function work with keyword-only defaults (#336)
Browse files Browse the repository at this point in the history
* fix: make copy_function work with keyword-only defaults

* move kwdefaults test to py3-only file

---------

Co-authored-by: Mahmoud Hashemi <mahmoud@hatnote.com>
  • Loading branch information
thorwhalen and mahmoud committed Apr 21, 2023
1 parent 0341ab3 commit c2b04b4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions boltons/funcutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ def copy_function(orig, copy_dict=True):
name=orig.__name__,
argdefs=getattr(orig, "__defaults__", None),
closure=getattr(orig, "__closure__", None))
if hasattr(orig, "__kwdefaults__"):
ret.__kwdefaults__ = orig.__kwdefaults__
if copy_dict:
ret.__dict__.update(orig.__dict__)
return ret
Expand Down
9 changes: 8 additions & 1 deletion tests/test_funcutils_fb_py3.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import pytest

from boltons.funcutils import wraps, FunctionBuilder, update_wrapper
from boltons.funcutils import wraps, FunctionBuilder, update_wrapper, copy_function
import boltons.funcutils as funcutils


Expand Down Expand Up @@ -53,6 +53,13 @@ def kwonly_non_roundtrippable_repr(*, x=lambda y: y + 1):
True, 'kwonly_non_roundtrippable_repr', 2)


def test_copy_function_kw_defaults_py3():
# test that the copy works with keyword-only defaults
f = lambda x, *, y=2: x * y
f_copy = copy_function(f)
assert f(21) == f_copy(21) == 42


@pytest.mark.parametrize('partial_kind', (functools, funcutils))
def test_update_wrapper_partial(partial_kind):
wrapper = partial_kind.partial(wrappable_varkw_func, b=1)
Expand Down

0 comments on commit c2b04b4

Please sign in to comment.