Skip to content

Commit 74bfbb5

Browse files
authored
Merge pull request #228 from tcapelle/cast
add cast to output
2 parents 9f13ba7 + 10c24c4 commit 74bfbb5

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
@@ -260,7 +260,8 @@ def argnames(f, frame=False):
260260
# Cell
261261
def with_cast(f):
262262
"Decorator which uses any parameter annotations as preprocessing functions"
263-
anno,params = annotations(f),argnames(f)
263+
anno, out_anno, params = annotations(f), anno_ret(f), argnames(f)
264+
c_out = ifnone(out_anno, noop)
264265
defaults = dict(zip(reversed(params), reversed(f.__defaults__ or {})))
265266
@functools.wraps(f)
266267
def _inner(*args, **kwargs):
@@ -271,7 +272,7 @@ def _inner(*args, **kwargs):
271272
if v in kwargs: kwargs[v] = c(kwargs[v])
272273
elif i<len(args): args[i] = c(args[i])
273274
elif v in defaults: kwargs[v] = c(defaults[v])
274-
return f(*args, **kwargs)
275+
return c_out(f(*args, **kwargs))
275276
return _inner
276277

277278
# 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
{
@@ -4851,7 +4859,7 @@
48514859
{
48524860
"data": {
48534861
"text/plain": [
4854-
"64"
4862+
"12"
48554863
]
48564864
},
48574865
"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)