|
336 | 336 | "\n", |
337 | 337 | " def add(self, f):\n", |
338 | 338 | " \"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", |
339 | 340 | " a0,a1 = _p2_anno(f)\n", |
340 | 341 | " t = self.funcs.d.get(a0)\n", |
341 | 342 | " if t is None:\n", |
|
828 | 829 | { |
829 | 830 | "data": { |
830 | 831 | "text/markdown": [ |
831 | | - "<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", |
| 832 | + "<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", |
832 | 833 | "\n", |
833 | 834 | "> <code>TypeDispatch.__call__</code>(**\\*`args`**, **\\*\\*`kwargs`**)\n", |
834 | 835 | "\n", |
|
916 | 917 | { |
917 | 918 | "data": { |
918 | 919 | "text/markdown": [ |
919 | | - "<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", |
| 920 | + "<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", |
920 | 921 | "\n", |
921 | 922 | "> <code>TypeDispatch.returns</code>(**`x`**)\n", |
922 | 923 | "\n", |
|
979 | 980 | "source": [ |
980 | 981 | "def m_nin(self, x:(str,numbers.Integral)): return str(x)+'1'\n", |
981 | 982 | "def m_bll(self, x:bool): self.foo='a'\n", |
982 | | - "def m_num(self, x:numbers.Number): return x\n", |
| 983 | + "def m_num(self, x:numbers.Number): return x*2\n", |
983 | 984 | "\n", |
984 | 985 | "t = TypeDispatch([m_nin,m_num,m_bll])\n", |
985 | 986 | "class A: f = t # set class attribute `f` equal to a TypeDispatch instance\n", |
986 | 987 | " \n", |
987 | 988 | "a = A()\n", |
988 | 989 | "test_eq(a.f(1), '11') #dispatch to m_nin\n", |
989 | | - "test_eq(a.f(1.), 1.) #dispatch to m_num\n", |
| 990 | + "test_eq(a.f(1.), 2.) #dispatch to m_num\n", |
990 | 991 | "test_is(a.f.inst, a)\n", |
991 | 992 | "\n", |
992 | 993 | "a.f(False) # this triggers t.m_bll to run, which sets self.foo to 'a'\n", |
|
1028 | 1029 | "class A2: f = t2\n", |
1029 | 1030 | "a2 = A2()\n", |
1030 | 1031 | "test_eq(a2.f(1), '11')\n", |
1031 | | - "test_eq(a2.f(1.), 1.)\n", |
| 1032 | + "test_eq(a2.f(1.), 2.)\n", |
1032 | 1033 | "test_is(a2.f.inst, a2)\n", |
1033 | 1034 | "a2.f(False)\n", |
1034 | 1035 | "test_eq(a2.foo, 'a')\n", |
|
1072 | 1073 | "def f_td_test(x:numbers.Integral, y): return x+1\n", |
1073 | 1074 | "@typedispatch\n", |
1074 | 1075 | "def f_td_test(x:int, y:float): return x+y\n", |
| 1076 | + "@typedispatch\n", |
| 1077 | + "def f_td_test(x:int, y:int): return x*y\n", |
1075 | 1078 | "\n", |
1076 | 1079 | "test_eq(f_td_test(3,2.0), 5)\n", |
1077 | | - "\n", |
1078 | 1080 | "assert issubclass(int, numbers.Integral)\n", |
1079 | | - "test_eq(f_td_test(3,2), 4)\n", |
| 1081 | + "test_eq(f_td_test(3,2), 6)\n", |
1080 | 1082 | "\n", |
1081 | 1083 | "test_eq(f_td_test('a','b'), 'ab')" |
1082 | 1084 | ] |
1083 | 1085 | }, |
| 1086 | + { |
| 1087 | + "cell_type": "code", |
| 1088 | + "execution_count": null, |
| 1089 | + "metadata": {}, |
| 1090 | + "outputs": [ |
| 1091 | + { |
| 1092 | + "name": "stdout", |
| 1093 | + "output_type": "stream", |
| 1094 | + "text": [ |
| 1095 | + "<class 'UserWarning'>: f_td_test has default params. These will be ignored.\n" |
| 1096 | + ] |
| 1097 | + } |
| 1098 | + ], |
| 1099 | + "source": [ |
| 1100 | + "def outer():\n", |
| 1101 | + " @typedispatch\n", |
| 1102 | + " def f_td_test(x:int,y:int=10): return x*y\n", |
| 1103 | + "test_warns(outer,True)" |
| 1104 | + ] |
| 1105 | + }, |
1084 | 1106 | { |
1085 | 1107 | "cell_type": "markdown", |
1086 | 1108 | "metadata": {}, |
|
0 commit comments