Skip to content

Commit 7ed8d0c

Browse files
authored
Merge pull request #134 from worc3131/kwdefaults
Fix copy_func to include required keyword defaults
2 parents 31e072d + 96b7e9b commit 7ed8d0c

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

fastcore/foundation.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def copy_func(f):
2222
"Copy a non-builtin function (NB `copy.copy` does not work for this)"
2323
if not isinstance(f,FunctionType): return copy(f)
2424
fn = FunctionType(f.__code__, f.__globals__, f.__name__, f.__defaults__, f.__closure__)
25+
fn.__kwdefaults__ = f.__kwdefaults__
2526
fn.__dict__.update(f.__dict__)
2627
return fn
2728

nbs/01_foundation.ipynb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
" \"Copy a non-builtin function (NB `copy.copy` does not work for this)\"\n",
7474
" if not isinstance(f,FunctionType): return copy(f)\n",
7575
" fn = FunctionType(f.__code__, f.__globals__, f.__name__, f.__defaults__, f.__closure__)\n",
76+
" fn.__kwdefaults__ = f.__kwdefaults__\n",
7677
" fn.__dict__.update(f.__dict__)\n",
7778
" return fn"
7879
]
@@ -118,6 +119,17 @@
118119
"assert c is not foo"
119120
]
120121
},
122+
{
123+
"cell_type": "code",
124+
"execution_count": null,
125+
"metadata": {},
126+
"outputs": [],
127+
"source": [
128+
"def g(x, *, y=3):\n",
129+
" return x+y\n",
130+
"test_eq(copy_func(g)(4), 7)"
131+
]
132+
},
121133
{
122134
"cell_type": "code",
123135
"execution_count": null,

0 commit comments

Comments
 (0)