Skip to content

Commit

Permalink
fixes #74
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Sep 6, 2020
1 parent c8e7887 commit 0b30c6e
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
10 changes: 9 additions & 1 deletion fastcore/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,20 @@ def anno_ret(func):
# Cell
cmp_instance = functools.cmp_to_key(lambda a,b: 0 if a==b else 1 if issubclass(a,b) else -1)

# Cell
def _chk_defaults(f):
try: # Some callables don't have signatures, so ignore those errors
params = list(inspect.signature(f).parameters.values())[:2]
if any(p.default!=inspect.Parameter.empty for p in params):
warn(f"{f.__name__} has default params. These will be ignored.")
except ValueError: pass

# Cell
def _p2_anno(f):
"Get the 1st 2 annotations of `f`, defaulting to `object`"
hints = type_hints(f)
ann = [o for n,o in hints.items() if n!='return']
if callable(f): _chk_defaults(f)
while len(ann)<2: ann.append(object)
return ann[:2]

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

def add(self, f):
"Add type `t` and function `f`"
if f and getattr(f,'__defaults__',None): warn(f"{f.__name__} has default params. These will be ignored.")
a0,a1 = _p2_anno(f)
t = self.funcs.d.get(a0)
if t is None:
Expand Down
79 changes: 77 additions & 2 deletions nbs/03_dispatch.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,21 @@
"test_eq(sorted(td, key=cmp_instance), [numbers.Number, numbers.Integral, int])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#export\n",
"def _chk_defaults(f):\n",
" try: # Some callables don't have signatures, so ignore those errors\n",
" params = list(inspect.signature(f).parameters.values())[:2]\n",
" if any(p.default!=inspect.Parameter.empty for p in params):\n",
" warn(f\"{f.__name__} has default params. These will be ignored.\")\n",
" except ValueError: pass"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -221,6 +236,7 @@
" \"Get the 1st 2 annotations of `f`, defaulting to `object`\"\n",
" hints = type_hints(f)\n",
" ann = [o for n,o in hints.items() if n!='return']\n",
" if callable(f): _chk_defaults(f)\n",
" while len(ann)<2: ann.append(object)\n",
" return ann[:2]"
]
Expand Down Expand Up @@ -248,7 +264,19 @@
"test_eq(_p2_anno(_f), (int,int))\n",
"def _f(a:int, b:str)->float: pass\n",
"test_eq(_p2_anno(_f), (int,str))\n",
"test_eq(_p2_anno(attrgetter('foo')), (object,object))"
"test_eq(_p2_anno(attrgetter('foo')), (object,object))\n",
"_p2_anno(None)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#hide\n",
"def _f(x:int,y:int=10): pass\n",
"test_warns(lambda: _p2_anno(_f))"
]
},
{
Expand Down Expand Up @@ -336,7 +364,6 @@
"\n",
" def add(self, f):\n",
" \"Add type `t` and function `f`\"\n",
" if f and getattr(f,'__defaults__',None): warn(f\"{f.__name__} has default params. These will be ignored.\")\n",
" a0,a1 = _p2_anno(f)\n",
" t = self.funcs.d.get(a0)\n",
" if t is None:\n",
Expand Down Expand Up @@ -1083,6 +1110,15 @@
"test_eq(f_td_test('a','b'), 'ab')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test_warns??"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -1103,6 +1139,45 @@
"test_warns(outer,True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/jhoward/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:12: UserWarning: _test has default params. These will be ignored.\n",
" if sys.path[0] == '':\n"
]
}
],
"source": [
"@typedispatch\n",
"def _test(x:int,y:int,z=10): return x*y*10"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"60"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"_test(3,2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down

0 comments on commit 0b30c6e

Please sign in to comment.