|
449 | 449 | "\n",
|
450 | 450 | " def add(self, f):\n",
|
451 | 451 | " \"Add type `t` and function `f`\"\n",
|
452 |
| - " a0,a1 = _p2_anno(f)\n", |
| 452 | + " if isinstance(f, staticmethod): a0,a1 = _p2_anno(f.__func__)\n", |
| 453 | + " else: a0,a1 = _p2_anno(f)\n", |
453 | 454 | " t = self.funcs.d.get(a0)\n",
|
454 | 455 | " if t is None:\n",
|
455 | 456 | " t = _TypeDict()\n",
|
|
473 | 474 | " ts = L(args).map(type)[:2]\n",
|
474 | 475 | " f = self[tuple(ts)]\n",
|
475 | 476 | " if not f: return args[0]\n",
|
476 |
| - " if self.inst is not None: f = MethodType(f, self.inst)\n", |
| 477 | + " if isinstance(f, staticmethod): f = f.__func__\n", |
| 478 | + " elif self.inst is not None: f = MethodType(f, self.inst)\n", |
477 | 479 | " elif self.owner is not None: f = MethodType(f, self.owner)\n",
|
478 | 480 | " return f(*args, **kwargs)\n",
|
479 | 481 | "\n",
|
|
943 | 945 | {
|
944 | 946 | "data": {
|
945 | 947 | "text/markdown": [
|
946 |
| - "<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", |
| 948 | + "<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", |
947 | 949 | "\n",
|
948 | 950 | "> <code>TypeDispatch.__call__</code>(**\\*`args`**, **\\*\\*`kwargs`**)\n",
|
949 | 951 | "\n",
|
|
1031 | 1033 | {
|
1032 | 1034 | "data": {
|
1033 | 1035 | "text/markdown": [
|
1034 |
| - "<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", |
| 1036 | + "<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", |
1035 | 1037 | "\n",
|
1036 | 1038 | "> <code>TypeDispatch.returns</code>(**`x`**)\n",
|
1037 | 1039 | "\n",
|
|
1198 | 1200 | " \"A global registry for `TypeDispatch` objects keyed by function name\"\n",
|
1199 | 1201 | " def __init__(self): self.d = defaultdict(TypeDispatch)\n",
|
1200 | 1202 | " def __call__(self, f):\n",
|
1201 |
| - " nm = f'{f.__qualname__}'\n", |
| 1203 | + " if isinstance(f, (classmethod, staticmethod)): nm = f'{f.__func__.__qualname__}'\n", |
| 1204 | + " else: nm = f'{f.__qualname__}'\n", |
| 1205 | + " if isinstance(f, classmethod): f=f.__func__\n", |
1202 | 1206 | " self.d[nm].add(f)\n",
|
1203 | 1207 | " return self.d[nm]\n",
|
1204 | 1208 | "\n",
|
|
1227 | 1231 | "test_eq(f_td_test('a','b'), 'ab')"
|
1228 | 1232 | ]
|
1229 | 1233 | },
|
| 1234 | + { |
| 1235 | + "cell_type": "markdown", |
| 1236 | + "metadata": {}, |
| 1237 | + "source": [ |
| 1238 | + "#### Using typedispatch With other decorators\n", |
| 1239 | + "\n", |
| 1240 | + "You can use `typedispatch` with `classmethod` and `staticmethod` decorator" |
| 1241 | + ] |
| 1242 | + }, |
| 1243 | + { |
| 1244 | + "cell_type": "code", |
| 1245 | + "execution_count": null, |
| 1246 | + "metadata": {}, |
| 1247 | + "outputs": [], |
| 1248 | + "source": [ |
| 1249 | + "class A:\n", |
| 1250 | + " @typedispatch\n", |
| 1251 | + " def f_td_test(self, x:numbers.Integral, y): return x+1\n", |
| 1252 | + " @typedispatch\n", |
| 1253 | + " @classmethod\n", |
| 1254 | + " def f_td_test(cls, x:int, y:float): return x+y\n", |
| 1255 | + " @typedispatch\n", |
| 1256 | + " @staticmethod\n", |
| 1257 | + " def f_td_test(x:int, y:int): return x*y\n", |
| 1258 | + " \n", |
| 1259 | + "test_eq(A.f_td_test(3,2), 6)\n", |
| 1260 | + "test_eq(A.f_td_test(3,2.0), 5)\n", |
| 1261 | + "test_eq(A().f_td_test(3,'2.0'), 4)" |
| 1262 | + ] |
| 1263 | + }, |
1230 | 1264 | {
|
1231 | 1265 | "cell_type": "markdown",
|
1232 | 1266 | "metadata": {},
|
|
0 commit comments