Skip to content

Commit 10c24c4

Browse files
author
Thomas Capelle
committed
add cast to output
1 parent 0b188e9 commit 10c24c4

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

fastcore/basics.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ def argnames(f, frame=False):
259259
# Cell
260260
def with_cast(f):
261261
"Decorator which uses any parameter annotations as preprocessing functions"
262-
anno,params = annotations(f),argnames(f)
262+
anno, out_anno, params = annotations(f), anno_ret(f), argnames(f)
263+
c_out = ifnone(out_anno, noop)
263264
defaults = dict(zip(reversed(params), reversed(f.__defaults__ or {})))
264265
@functools.wraps(f)
265266
def _inner(*args, **kwargs):
@@ -270,7 +271,7 @@ def _inner(*args, **kwargs):
270271
if v in kwargs: kwargs[v] = c(kwargs[v])
271272
elif i<len(args): args[i] = c(args[i])
272273
elif v in defaults: kwargs[v] = c(defaults[v])
273-
return f(*args, **kwargs)
274+
return c_out(f(*args, **kwargs))
274275
return _inner
275276

276277
# Cell

nbs/01_basics.ipynb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,7 +1517,8 @@
15171517
"#export\n",
15181518
"def with_cast(f):\n",
15191519
" \"Decorator which uses any parameter annotations as preprocessing functions\"\n",
1520-
" anno,params = annotations(f),argnames(f)\n",
1520+
" anno, out_anno, params = annotations(f), anno_ret(f), argnames(f)\n",
1521+
" c_out = ifnone(out_anno, noop)\n",
15211522
" defaults = dict(zip(reversed(params), reversed(f.__defaults__ or {})))\n",
15221523
" @functools.wraps(f)\n",
15231524
" def _inner(*args, **kwargs):\n",
@@ -1528,7 +1529,7 @@
15281529
" if v in kwargs: kwargs[v] = c(kwargs[v])\n",
15291530
" elif i<len(args): args[i] = c(args[i])\n",
15301531
" elif v in defaults: kwargs[v] = c(defaults[v])\n",
1531-
" return f(*args, **kwargs)\n",
1532+
" return c_out(f(*args, **kwargs))\n",
15321533
" return _inner"
15331534
]
15341535
},
@@ -1539,10 +1540,17 @@
15391540
"outputs": [],
15401541
"source": [
15411542
"@with_cast\n",
1542-
"def _f(a, b:Path, c:str='', d=0)->bool: return (a,b,c,d)\n",
1543+
"def _f(a, b:Path, c:str='', d=0): return (a,b,c,d)\n",
15431544
"\n",
15441545
"test_eq(_f(1, '.', 3), (1,Path('.'),'3',0))\n",
1545-
"test_eq(_f(1, '.'), (1,Path('.'),'',0))"
1546+
"test_eq(_f(1, '.'), (1,Path('.'),'',0))\n",
1547+
"\n",
1548+
"@with_cast\n",
1549+
"def _g(a:int=0)->str: return a\n",
1550+
"\n",
1551+
"test_eq(_g(4.0), '4')\n",
1552+
"test_eq(_g(4.4), '4')\n",
1553+
"test_eq(_g(2), '2')"
15461554
]
15471555
},
15481556
{
@@ -4285,7 +4293,7 @@
42854293
{
42864294
"data": {
42874295
"text/plain": [
4288-
"64"
4296+
"12"
42894297
]
42904298
},
42914299
"execution_count": null,

nbs/04_dispatch.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1446,5 +1446,5 @@
14461446
}
14471447
},
14481448
"nbformat": 4,
1449-
"nbformat_minor": 2
1449+
"nbformat_minor": 4
14501450
}

0 commit comments

Comments
 (0)