|
210 | 210 | "test_eq(sorted(td, key=cmp_instance), [numbers.Number, numbers.Integral, int])" |
211 | 211 | ] |
212 | 212 | }, |
| 213 | + { |
| 214 | + "cell_type": "code", |
| 215 | + "execution_count": null, |
| 216 | + "metadata": {}, |
| 217 | + "outputs": [], |
| 218 | + "source": [ |
| 219 | + "#export\n", |
| 220 | + "def _chk_defaults(f):\n", |
| 221 | + " try: # Some callables don't have signatures, so ignore those errors\n", |
| 222 | + " params = list(inspect.signature(f).parameters.values())[:2]\n", |
| 223 | + " if any(p.default!=inspect.Parameter.empty for p in params):\n", |
| 224 | + " warn(f\"{f.__name__} has default params. These will be ignored.\")\n", |
| 225 | + " except ValueError: pass" |
| 226 | + ] |
| 227 | + }, |
213 | 228 | { |
214 | 229 | "cell_type": "code", |
215 | 230 | "execution_count": null, |
|
221 | 236 | " \"Get the 1st 2 annotations of `f`, defaulting to `object`\"\n", |
222 | 237 | " hints = type_hints(f)\n", |
223 | 238 | " ann = [o for n,o in hints.items() if n!='return']\n", |
| 239 | + " if callable(f): _chk_defaults(f)\n", |
224 | 240 | " while len(ann)<2: ann.append(object)\n", |
225 | 241 | " return ann[:2]" |
226 | 242 | ] |
|
248 | 264 | "test_eq(_p2_anno(_f), (int,int))\n", |
249 | 265 | "def _f(a:int, b:str)->float: pass\n", |
250 | 266 | "test_eq(_p2_anno(_f), (int,str))\n", |
251 | | - "test_eq(_p2_anno(attrgetter('foo')), (object,object))" |
| 267 | + "test_eq(_p2_anno(attrgetter('foo')), (object,object))\n", |
| 268 | + "_p2_anno(None)" |
| 269 | + ] |
| 270 | + }, |
| 271 | + { |
| 272 | + "cell_type": "code", |
| 273 | + "execution_count": null, |
| 274 | + "metadata": {}, |
| 275 | + "outputs": [], |
| 276 | + "source": [ |
| 277 | + "#hide\n", |
| 278 | + "def _f(x:int,y:int=10): pass\n", |
| 279 | + "test_warns(lambda: _p2_anno(_f))" |
252 | 280 | ] |
253 | 281 | }, |
254 | 282 | { |
|
336 | 364 | "\n", |
337 | 365 | " def add(self, f):\n", |
338 | 366 | " \"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", |
340 | 367 | " a0,a1 = _p2_anno(f)\n", |
341 | 368 | " t = self.funcs.d.get(a0)\n", |
342 | 369 | " if t is None:\n", |
|
1083 | 1110 | "test_eq(f_td_test('a','b'), 'ab')" |
1084 | 1111 | ] |
1085 | 1112 | }, |
| 1113 | + { |
| 1114 | + "cell_type": "code", |
| 1115 | + "execution_count": null, |
| 1116 | + "metadata": {}, |
| 1117 | + "outputs": [], |
| 1118 | + "source": [ |
| 1119 | + "test_warns??" |
| 1120 | + ] |
| 1121 | + }, |
1086 | 1122 | { |
1087 | 1123 | "cell_type": "code", |
1088 | 1124 | "execution_count": null, |
|
1103 | 1139 | "test_warns(outer,True)" |
1104 | 1140 | ] |
1105 | 1141 | }, |
| 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 | + }, |
1106 | 1181 | { |
1107 | 1182 | "cell_type": "markdown", |
1108 | 1183 | "metadata": {}, |
|
0 commit comments