|
217 | 217 | "outputs": [],
|
218 | 218 | "source": [
|
219 | 219 | "#export\n",
|
220 |
| - "def _chk_defaults(f):\n", |
| 220 | + "def _chk_defaults(f, ann):\n", |
221 | 221 | " 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", |
223 | 223 | " if any(p.default!=inspect.Parameter.empty for p in params):\n",
|
224 | 224 | " warn(f\"{f.__name__} has default params. These will be ignored.\")\n",
|
225 | 225 | " except ValueError: pass"
|
|
236 | 236 | " \"Get the 1st 2 annotations of `f`, defaulting to `object`\"\n",
|
237 | 237 | " hints = type_hints(f)\n",
|
238 | 238 | " 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", |
240 | 240 | " while len(ann)<2: ann.append(object)\n",
|
241 | 241 | " return ann[:2]"
|
242 | 242 | ]
|
|
264 | 264 | "test_eq(_p2_anno(_f), (int,int))\n",
|
265 | 265 | "def _f(a:int, b:str)->float: pass\n",
|
266 | 266 | "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))" |
269 | 268 | ]
|
270 | 269 | },
|
271 | 270 | {
|
272 | 271 | "cell_type": "code",
|
273 | 272 | "execution_count": null,
|
274 | 273 | "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 | + ], |
276 | 286 | "source": [
|
277 | 287 | "#hide\n",
|
278 | 288 | "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)" |
280 | 292 | ]
|
281 | 293 | },
|
282 | 294 | {
|
|
856 | 868 | {
|
857 | 869 | "data": {
|
858 | 870 | "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", |
860 | 872 | "\n",
|
861 | 873 | "> <code>TypeDispatch.__call__</code>(**\\*`args`**, **\\*\\*`kwargs`**)\n",
|
862 | 874 | "\n",
|
|
944 | 956 | {
|
945 | 957 | "data": {
|
946 | 958 | "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", |
948 | 960 | "\n",
|
949 | 961 | "> <code>TypeDispatch.returns</code>(**`x`**)\n",
|
950 | 962 | "\n",
|
|
1110 | 1122 | "test_eq(f_td_test('a','b'), 'ab')"
|
1111 | 1123 | ]
|
1112 | 1124 | },
|
1113 |
| - { |
1114 |
| - "cell_type": "code", |
1115 |
| - "execution_count": null, |
1116 |
| - "metadata": {}, |
1117 |
| - "outputs": [], |
1118 |
| - "source": [ |
1119 |
| - "test_warns??" |
1120 |
| - ] |
1121 |
| - }, |
1122 | 1125 | {
|
1123 | 1126 | "cell_type": "code",
|
1124 | 1127 | "execution_count": null,
|
|
1139 | 1142 | "test_warns(outer,True)"
|
1140 | 1143 | ]
|
1141 | 1144 | },
|
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 |
| - }, |
1181 | 1145 | {
|
1182 | 1146 | "cell_type": "markdown",
|
1183 | 1147 | "metadata": {},
|
|
0 commit comments