|
131 | 131 | {
|
132 | 132 | "data": {
|
133 | 133 | "text/plain": [
|
134 |
| - "datetime.datetime(2024, 10, 16, 14, 0)" |
| 134 | + "datetime.datetime(2024, 10, 19, 14, 0)" |
135 | 135 | ]
|
136 | 136 | },
|
137 | 137 | "execution_count": null,
|
|
280 | 280 | "outputs": [],
|
281 | 281 | "source": [
|
282 | 282 | "#| export\n",
|
283 |
| - "def _mk_list(t, v): return [t(o) for o in v]" |
| 283 | + "def _mk_list(t, v): return [t(o) for o in listify(v)]" |
284 | 284 | ]
|
285 | 285 | },
|
286 | 286 | {
|
|
310 | 310 | "outputs": [],
|
311 | 311 | "source": [
|
312 | 312 | "#| export\n",
|
313 |
| - "def _fix_anno(t):\n", |
| 313 | + "def _fix_anno(t, o):\n", |
314 | 314 | " \"Create appropriate callable type for casting a `str` to type `t` (or first type in `t` if union)\"\n",
|
315 | 315 | " origin = get_origin(t)\n",
|
316 | 316 | " if origin is Union or origin is UnionType or origin in (list,List):\n",
|
317 | 317 | " t = first(o for o in get_args(t) if o!=type(None))\n",
|
318 | 318 | " d = {bool: str2bool, int: str2int, date: str2date, UploadFile: noop}\n",
|
319 | 319 | " res = d.get(t, t)\n",
|
320 |
| - " if origin in (list,List): return partial(_mk_list, res)\n", |
321 |
| - " return lambda o: res(o[-1]) if isinstance(o,(list,tuple)) else res(o)" |
| 320 | + " if origin in (list,List): return _mk_list(res, o)\n", |
| 321 | + " if not isinstance(o, (str,list,tuple)): return o\n", |
| 322 | + " return res(o[-1]) if isinstance(o,(list,tuple)) else res(o)" |
322 | 323 | ]
|
323 | 324 | },
|
324 | 325 | {
|
|
328 | 329 | "metadata": {},
|
329 | 330 | "outputs": [],
|
330 | 331 | "source": [
|
331 |
| - "test_eq(_fix_anno(Union[str,None])('a'), 'a')\n", |
332 |
| - "test_eq(_fix_anno(float)(0.9), 0.9)\n", |
333 |
| - "test_eq(_fix_anno(int)('1'), 1)\n", |
334 |
| - "test_eq(_fix_anno(int)(['1','2']), 2)\n", |
335 |
| - "test_eq(_fix_anno(list[int])(['1','2']), [1,2])\n", |
336 |
| - "test_eq(_fix_anno(list[int])('1'), [1])" |
| 332 | + "test_eq(_fix_anno(Union[str,None], 'a'), 'a')\n", |
| 333 | + "test_eq(_fix_anno(float, 0.9), 0.9)\n", |
| 334 | + "test_eq(_fix_anno(int, '1'), 1)\n", |
| 335 | + "test_eq(_fix_anno(int, ['1','2']), 2)\n", |
| 336 | + "test_eq(_fix_anno(list[int], ['1','2']), [1,2])\n", |
| 337 | + "test_eq(_fix_anno(list[int], '1'), [1])" |
337 | 338 | ]
|
338 | 339 | },
|
339 | 340 | {
|
|
351 | 352 | " # This is the type we want to cast `v` to\n",
|
352 | 353 | " anno = d.get(k, None)\n",
|
353 | 354 | " if not anno: return v\n",
|
354 |
| - " return _fix_anno(anno)(v)" |
| 355 | + " return _fix_anno(anno, v)" |
355 | 356 | ]
|
356 | 357 | },
|
357 | 358 | {
|
|
651 | 652 | " # If we have a default, return that if we have no value\n",
|
652 | 653 | " if res in (empty,None): res = p.default\n",
|
653 | 654 | " # We can cast str and list[str] to types; otherwise just return what we have\n",
|
654 |
| - " if not isinstance(res, (list,str)) or anno is empty: return res\n", |
655 |
| - " anno = _fix_anno(anno)\n", |
656 |
| - " try: return anno(res)\n", |
| 655 | + " if anno is empty: return res\n", |
| 656 | + " try: return _fix_anno(anno, res)\n", |
657 | 657 | " except ValueError: raise HTTPException(404, req.url.path) from None\n",
|
658 | 658 | "\n",
|
659 | 659 | "async def _wrap_req(req, params):\n",
|
|
805 | 805 | " if res is empty or res is None: res = p.default\n",
|
806 | 806 | " # We can cast str and list[str] to types; otherwise just return what we have\n",
|
807 | 807 | " if not isinstance(res, (list,str)) or anno is empty: return res\n",
|
808 |
| - " anno = _fix_anno(anno)\n", |
809 |
| - " return [anno(o) for o in res] if isinstance(res,list) else anno(res)\n", |
| 808 | + " return [_fix_anno(anno, o) for o in res] if isinstance(res,list) else _fix_anno(anno, res)\n", |
810 | 809 | "\n",
|
811 | 810 | "def _wrap_ws(ws, data, params):\n",
|
812 | 811 | " hdrs = {k.lower().replace('-','_'):v for k,v in data.pop('HEADERS', {}).items()}\n",
|
|
2233 | 2232 | "print(res.text)"
|
2234 | 2233 | ]
|
2235 | 2234 | },
|
| 2235 | + { |
| 2236 | + "cell_type": "code", |
| 2237 | + "execution_count": null, |
| 2238 | + "id": "28a99667", |
| 2239 | + "metadata": {}, |
| 2240 | + "outputs": [ |
| 2241 | + { |
| 2242 | + "name": "stdout", |
| 2243 | + "output_type": "stream", |
| 2244 | + "text": [ |
| 2245 | + "200\n", |
| 2246 | + "content1\n" |
| 2247 | + ] |
| 2248 | + } |
| 2249 | + ], |
| 2250 | + "source": [ |
| 2251 | + "res = cli.post('/uploads', files=[files[0]])\n", |
| 2252 | + "print(res.status_code)\n", |
| 2253 | + "print(res.text)" |
| 2254 | + ] |
| 2255 | + }, |
2236 | 2256 | {
|
2237 | 2257 | "cell_type": "code",
|
2238 | 2258 | "execution_count": null,
|
|
2243 | 2263 | "name": "stdout",
|
2244 | 2264 | "output_type": "stream",
|
2245 | 2265 | "text": [
|
2246 |
| - "Set to 2024-10-16 15:38:25.588198\n" |
| 2266 | + "Set to 2024-10-19 15:14:43.250650\n" |
2247 | 2267 | ]
|
2248 | 2268 | },
|
2249 | 2269 | {
|
2250 | 2270 | "data": {
|
2251 | 2271 | "text/plain": [
|
2252 |
| - "'Session time: 2024-10-16 15:38:25.588198'" |
| 2272 | + "'Session time: 2024-10-19 15:14:43.250650'" |
2253 | 2273 | ]
|
2254 | 2274 | },
|
2255 | 2275 | "execution_count": null,
|
|
2480 | 2500 | {
|
2481 | 2501 | "data": {
|
2482 | 2502 | "text/plain": [
|
2483 |
| - "'Cookie was set at time 15:38:25.962118'" |
| 2503 | + "'Cookie was set at time 15:14:43.683602'" |
2484 | 2504 | ]
|
2485 | 2505 | },
|
2486 | 2506 | "execution_count": null,
|
|
0 commit comments