Skip to content

Commit 0b30c6e

Browse files
committed
fixes #74
1 parent c8e7887 commit 0b30c6e

File tree

2 files changed

+86
-3
lines changed

2 files changed

+86
-3
lines changed

fastcore/dispatch.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,20 @@ def anno_ret(func):
2424
# Cell
2525
cmp_instance = functools.cmp_to_key(lambda a,b: 0 if a==b else 1 if issubclass(a,b) else -1)
2626

27+
# Cell
28+
def _chk_defaults(f):
29+
try: # Some callables don't have signatures, so ignore those errors
30+
params = list(inspect.signature(f).parameters.values())[:2]
31+
if any(p.default!=inspect.Parameter.empty for p in params):
32+
warn(f"{f.__name__} has default params. These will be ignored.")
33+
except ValueError: pass
34+
2735
# Cell
2836
def _p2_anno(f):
2937
"Get the 1st 2 annotations of `f`, defaulting to `object`"
3038
hints = type_hints(f)
3139
ann = [o for n,o in hints.items() if n!='return']
40+
if callable(f): _chk_defaults(f)
3241
while len(ann)<2: ann.append(object)
3342
return ann[:2]
3443

@@ -72,7 +81,6 @@ def __init__(self, funcs=(), bases=()):
7281

7382
def add(self, f):
7483
"Add type `t` and function `f`"
75-
if f and getattr(f,'__defaults__',None): warn(f"{f.__name__} has default params. These will be ignored.")
7684
a0,a1 = _p2_anno(f)
7785
t = self.funcs.d.get(a0)
7886
if t is None:

nbs/03_dispatch.ipynb

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,21 @@
210210
"test_eq(sorted(td, key=cmp_instance), [numbers.Number, numbers.Integral, int])"
211211
]
212212
},
213+
{
214+
"cell_type": "code",
215+
"execution_count": null,
216+
"metadata": {},
217+
"outputs": [],
218+
"source": [
219+
"#export\n",
220+
"def _chk_defaults(f):\n",
221+
" try: # Some callables don't have signatures, so ignore those errors\n",
222+
" params = list(inspect.signature(f).parameters.values())[:2]\n",
223+
" if any(p.default!=inspect.Parameter.empty for p in params):\n",
224+
" warn(f\"{f.__name__} has default params. These will be ignored.\")\n",
225+
" except ValueError: pass"
226+
]
227+
},
213228
{
214229
"cell_type": "code",
215230
"execution_count": null,
@@ -221,6 +236,7 @@
221236
" \"Get the 1st 2 annotations of `f`, defaulting to `object`\"\n",
222237
" hints = type_hints(f)\n",
223238
" ann = [o for n,o in hints.items() if n!='return']\n",
239+
" if callable(f): _chk_defaults(f)\n",
224240
" while len(ann)<2: ann.append(object)\n",
225241
" return ann[:2]"
226242
]
@@ -248,7 +264,19 @@
248264
"test_eq(_p2_anno(_f), (int,int))\n",
249265
"def _f(a:int, b:str)->float: pass\n",
250266
"test_eq(_p2_anno(_f), (int,str))\n",
251-
"test_eq(_p2_anno(attrgetter('foo')), (object,object))"
267+
"test_eq(_p2_anno(attrgetter('foo')), (object,object))\n",
268+
"_p2_anno(None)"
269+
]
270+
},
271+
{
272+
"cell_type": "code",
273+
"execution_count": null,
274+
"metadata": {},
275+
"outputs": [],
276+
"source": [
277+
"#hide\n",
278+
"def _f(x:int,y:int=10): pass\n",
279+
"test_warns(lambda: _p2_anno(_f))"
252280
]
253281
},
254282
{
@@ -336,7 +364,6 @@
336364
"\n",
337365
" def add(self, f):\n",
338366
" \"Add type `t` and function `f`\"\n",
339-
" if f and getattr(f,'__defaults__',None): warn(f\"{f.__name__} has default params. These will be ignored.\")\n",
340367
" a0,a1 = _p2_anno(f)\n",
341368
" t = self.funcs.d.get(a0)\n",
342369
" if t is None:\n",
@@ -1083,6 +1110,15 @@
10831110
"test_eq(f_td_test('a','b'), 'ab')"
10841111
]
10851112
},
1113+
{
1114+
"cell_type": "code",
1115+
"execution_count": null,
1116+
"metadata": {},
1117+
"outputs": [],
1118+
"source": [
1119+
"test_warns??"
1120+
]
1121+
},
10861122
{
10871123
"cell_type": "code",
10881124
"execution_count": null,
@@ -1103,6 +1139,45 @@
11031139
"test_warns(outer,True)"
11041140
]
11051141
},
1142+
{
1143+
"cell_type": "code",
1144+
"execution_count": null,
1145+
"metadata": {},
1146+
"outputs": [
1147+
{
1148+
"name": "stderr",
1149+
"output_type": "stream",
1150+
"text": [
1151+
"/home/jhoward/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:12: UserWarning: _test has default params. These will be ignored.\n",
1152+
" if sys.path[0] == '':\n"
1153+
]
1154+
}
1155+
],
1156+
"source": [
1157+
"@typedispatch\n",
1158+
"def _test(x:int,y:int,z=10): return x*y*10"
1159+
]
1160+
},
1161+
{
1162+
"cell_type": "code",
1163+
"execution_count": null,
1164+
"metadata": {},
1165+
"outputs": [
1166+
{
1167+
"data": {
1168+
"text/plain": [
1169+
"60"
1170+
]
1171+
},
1172+
"execution_count": null,
1173+
"metadata": {},
1174+
"output_type": "execute_result"
1175+
}
1176+
],
1177+
"source": [
1178+
"_test(3,2)"
1179+
]
1180+
},
11061181
{
11071182
"cell_type": "markdown",
11081183
"metadata": {},

0 commit comments

Comments
 (0)