Skip to content

Commit f8c3646

Browse files
committed
fix last fix
1 parent 19f2315 commit f8c3646

File tree

2 files changed

+24
-60
lines changed

2 files changed

+24
-60
lines changed

fastcore/dispatch.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ def anno_ret(func):
2525
cmp_instance = functools.cmp_to_key(lambda a,b: 0 if a==b else 1 if issubclass(a,b) else -1)
2626

2727
# Cell
28-
def _chk_defaults(f):
28+
def _chk_defaults(f, ann):
2929
try: # Some callables don't have signatures, so ignore those errors
30-
params = list(inspect.signature(f).parameters.values())[:2]
30+
params = list(inspect.signature(f).parameters.values())[:min(len(ann),2)]
3131
if any(p.default!=inspect.Parameter.empty for p in params):
3232
warn(f"{f.__name__} has default params. These will be ignored.")
3333
except ValueError: pass
@@ -37,7 +37,7 @@ def _p2_anno(f):
3737
"Get the 1st 2 annotations of `f`, defaulting to `object`"
3838
hints = type_hints(f)
3939
ann = [o for n,o in hints.items() if n!='return']
40-
if callable(f): _chk_defaults(f)
40+
if callable(f): _chk_defaults(f, ann)
4141
while len(ann)<2: ann.append(object)
4242
return ann[:2]
4343

nbs/03_dispatch.ipynb

Lines changed: 21 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,9 @@
217217
"outputs": [],
218218
"source": [
219219
"#export\n",
220-
"def _chk_defaults(f):\n",
220+
"def _chk_defaults(f, ann):\n",
221221
" try: # Some callables don't have signatures, so ignore those errors\n",
222-
" params = list(inspect.signature(f).parameters.values())[:2]\n",
222+
" params = list(inspect.signature(f).parameters.values())[:min(len(ann),2)]\n",
223223
" if any(p.default!=inspect.Parameter.empty for p in params):\n",
224224
" warn(f\"{f.__name__} has default params. These will be ignored.\")\n",
225225
" except ValueError: pass"
@@ -236,7 +236,7 @@
236236
" \"Get the 1st 2 annotations of `f`, defaulting to `object`\"\n",
237237
" hints = type_hints(f)\n",
238238
" ann = [o for n,o in hints.items() if n!='return']\n",
239-
" if callable(f): _chk_defaults(f)\n",
239+
" if callable(f): _chk_defaults(f, ann)\n",
240240
" while len(ann)<2: ann.append(object)\n",
241241
" return ann[:2]"
242242
]
@@ -264,19 +264,31 @@
264264
"test_eq(_p2_anno(_f), (int,int))\n",
265265
"def _f(a:int, b:str)->float: pass\n",
266266
"test_eq(_p2_anno(_f), (int,str))\n",
267-
"test_eq(_p2_anno(attrgetter('foo')), (object,object))\n",
268-
"_p2_anno(None)"
267+
"test_eq(_p2_anno(attrgetter('foo')), (object,object))"
269268
]
270269
},
271270
{
272271
"cell_type": "code",
273272
"execution_count": null,
274273
"metadata": {},
275-
"outputs": [],
274+
"outputs": [
275+
{
276+
"data": {
277+
"text/plain": [
278+
"([object, object], [int, object])"
279+
]
280+
},
281+
"execution_count": null,
282+
"metadata": {},
283+
"output_type": "execute_result"
284+
}
285+
],
276286
"source": [
277287
"#hide\n",
278288
"def _f(x:int,y:int=10): pass\n",
279-
"test_warns(lambda: _p2_anno(_f))"
289+
"test_warns(lambda: _p2_anno(_f))\n",
290+
"def _f(x:int,y=10): pass\n",
291+
"_p2_anno(None),_p2_anno(_f)"
280292
]
281293
},
282294
{
@@ -856,7 +868,7 @@
856868
{
857869
"data": {
858870
"text/markdown": [
859-
"<h4 id=\"TypeDispatch.__call__\" class=\"doc_header\"><code>TypeDispatch.__call__</code><a href=\"__main__.py#L33\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
871+
"<h4 id=\"TypeDispatch.__call__\" class=\"doc_header\"><code>TypeDispatch.__call__</code><a href=\"__main__.py#L32\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
860872
"\n",
861873
"> <code>TypeDispatch.__call__</code>(**\\*`args`**, **\\*\\*`kwargs`**)\n",
862874
"\n",
@@ -944,7 +956,7 @@
944956
{
945957
"data": {
946958
"text/markdown": [
947-
"<h4 id=\"TypeDispatch.returns\" class=\"doc_header\"><code>TypeDispatch.returns</code><a href=\"__main__.py#L21\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
959+
"<h4 id=\"TypeDispatch.returns\" class=\"doc_header\"><code>TypeDispatch.returns</code><a href=\"__main__.py#L20\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
948960
"\n",
949961
"> <code>TypeDispatch.returns</code>(**`x`**)\n",
950962
"\n",
@@ -1110,15 +1122,6 @@
11101122
"test_eq(f_td_test('a','b'), 'ab')"
11111123
]
11121124
},
1113-
{
1114-
"cell_type": "code",
1115-
"execution_count": null,
1116-
"metadata": {},
1117-
"outputs": [],
1118-
"source": [
1119-
"test_warns??"
1120-
]
1121-
},
11221125
{
11231126
"cell_type": "code",
11241127
"execution_count": null,
@@ -1139,45 +1142,6 @@
11391142
"test_warns(outer,True)"
11401143
]
11411144
},
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-
},
11811145
{
11821146
"cell_type": "markdown",
11831147
"metadata": {},

0 commit comments

Comments
 (0)