Skip to content

Commit 8731b46

Browse files
committed
remove returns_none
1 parent f2ebe16 commit 8731b46

File tree

4 files changed

+153
-60
lines changed

4 files changed

+153
-60
lines changed

fastcore/dispatch.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ def __repr__(self): return self.d.__repr__()
7575
def first(self): return first(self.d.values())
7676

7777
# Cell
78-
@docs
7978
class TypeDispatch:
8079
"Dictionary-like object; `__getitem__` matches keys of types using `issubclass`"
8180
def __init__(self, funcs=(), bases=()):
@@ -94,11 +93,13 @@ def add(self, f):
9493
self.funcs.add(a0, t)
9594
t.add(a1, f)
9695

97-
def first(self): return self.funcs.first().first()
98-
def returns(self, x): return anno_ret(self[type(x)])
99-
def returns_none(self, x):
100-
r = anno_ret(self[type(x)])
101-
return None if r == NoneType else r
96+
def first(self):
97+
"Get first function in ordered dict of type:func."
98+
return self.funcs.first().first()
99+
100+
def returns(self, x):
101+
"Get the return type of annotation of `x`."
102+
return anno_ret(self[type(x)])
102103

103104
def _attname(self,k): return getattr(k,'__name__',str(k))
104105
def __repr__(self):
@@ -134,10 +135,6 @@ def __getitem__(self, k):
134135
if res is not None: return res
135136
return None
136137

137-
_docs = dict(first="Get first function in ordered dict of type:func.",
138-
returns="Get the return type of annotation of `x`.",
139-
returns_none="Returns `None` if return type annotation is `None` or `NoneType`.")
140-
141138
# Cell
142139
class DispatchReg:
143140
"A global registry for `TypeDispatch` objects keyed by function name"

fastcore/transform.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def _call(self, fn, x, split_idx=None, **kwargs):
8585
def _do_call(self, f, x, **kwargs):
8686
if not _is_tuple(x):
8787
if f is None: return x
88-
ret = f.returns_none(x) if hasattr(f,'returns_none') else None
88+
ret = f.returns(x) if hasattr(f,'returns') else None
8989
return retain_type(f(x, **kwargs), x, ret)
9090
res = tuple(self._do_call(f, x_, **kwargs) for x_ in x)
9191
return retain_type(res, x)

nbs/04_dispatch.ipynb

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,6 @@
298298
"outputs": [],
299299
"source": [
300300
"#export\n",
301-
"@docs\n",
302301
"class TypeDispatch:\n",
303302
" \"Dictionary-like object; `__getitem__` matches keys of types using `issubclass`\"\n",
304303
" def __init__(self, funcs=(), bases=()):\n",
@@ -317,11 +316,13 @@
317316
" self.funcs.add(a0, t)\n",
318317
" t.add(a1, f)\n",
319318
"\n",
320-
" def first(self): return self.funcs.first().first()\n",
321-
" def returns(self, x): return anno_ret(self[type(x)])\n",
322-
" def returns_none(self, x):\n",
323-
" r = anno_ret(self[type(x)])\n",
324-
" return None if r == NoneType else r\n",
319+
" def first(self):\n",
320+
" \"Get first function in ordered dict of type:func.\"\n",
321+
" return self.funcs.first().first()\n",
322+
" \n",
323+
" def returns(self, x):\n",
324+
" \"Get the return type of annotation of `x`.\"\n",
325+
" return anno_ret(self[type(x)])\n",
325326
"\n",
326327
" def _attname(self,k): return getattr(k,'__name__',str(k))\n",
327328
" def __repr__(self):\n",
@@ -355,36 +356,7 @@
355356
" for base in self.bases:\n",
356357
" res = base[k]\n",
357358
" if res is not None: return res\n",
358-
" return None\n",
359-
" \n",
360-
" _docs = dict(first=\"Get first function in ordered dict of type:func.\",\n",
361-
" returns=\"Get the return type of annotation of `x`.\",\n",
362-
" returns_none=\"Returns `None` if return type annotation is `None` or `NoneType`.\")"
363-
]
364-
},
365-
{
366-
"cell_type": "code",
367-
"execution_count": null,
368-
"metadata": {},
369-
"outputs": [
370-
{
371-
"data": {
372-
"text/plain": [
373-
"<function __main__.a(z: type)>"
374-
]
375-
},
376-
"execution_count": null,
377-
"metadata": {},
378-
"output_type": "execute_result"
379-
}
380-
],
381-
"source": [
382-
"def a(z:type): return 'a'\n",
383-
"def b(z:object): return 'b'\n",
384-
"\n",
385-
"t = TypeDispatch([a,b])\n",
386-
"\n",
387-
"t[TypeDispatch]"
359+
" return None"
388360
]
389361
},
390362
{
@@ -507,7 +479,7 @@
507479
{
508480
"data": {
509481
"text/markdown": [
510-
"<h4 id=\"TypeDispatch.add\" class=\"doc_header\"><code>TypeDispatch.add</code><a href=\"__main__.py#L11\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
482+
"<h4 id=\"TypeDispatch.add\" class=\"doc_header\"><code>TypeDispatch.add</code><a href=\"__main__.py#L10\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
511483
"\n",
512484
"> <code>TypeDispatch.add</code>(**`f`**)\n",
513485
"\n",
@@ -830,7 +802,7 @@
830802
{
831803
"data": {
832804
"text/markdown": [
833-
"<h4 id=\"TypeDispatch.__call__\" class=\"doc_header\"><code>TypeDispatch.__call__</code><a href=\"__main__.py#L34\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
805+
"<h4 id=\"TypeDispatch.__call__\" class=\"doc_header\"><code>TypeDispatch.__call__</code><a href=\"__main__.py#L35\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
834806
"\n",
835807
"> <code>TypeDispatch.__call__</code>(**\\*`args`**, **\\*\\*`kwargs`**)\n",
836808
"\n",
@@ -918,7 +890,7 @@
918890
{
919891
"data": {
920892
"text/markdown": [
921-
"<h4 id=\"TypeDispatch.returns\" class=\"doc_header\"><code>TypeDispatch.returns</code><a href=\"__main__.py#L22\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
893+
"<h4 id=\"TypeDispatch.returns\" class=\"doc_header\"><code>TypeDispatch.returns</code><a href=\"__main__.py#L24\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
922894
"\n",
923895
"> <code>TypeDispatch.returns</code>(**`x`**)\n",
924896
"\n",

nbs/05_transform.ipynb

Lines changed: 134 additions & 10 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)