|
237 | 237 | " res = _clean_re.findall(s)\n",
|
238 | 238 | " return res[0] if res else None\n",
|
239 | 239 | "\n",
|
240 |
| - "def _param_locs(s, returns=True):\n", |
| 240 | + "def _param_locs(s, returns=True, args_kwargs=False):\n", |
241 | 241 | " \"`dict` of parameter line numbers to names\"\n",
|
242 | 242 | " body = _parses(s).body\n",
|
243 |
| - " if len(body)==1: #or not isinstance(body[0], FunctionDef): return None\n", |
| 243 | + " if len(body)==1:\n", |
244 | 244 | " defn = body[0]\n",
|
245 | 245 | " if isinstance(defn, (FunctionDef, AsyncFunctionDef)):\n",
|
246 | 246 | " res = {arg.lineno:arg.arg for arg in defn.args.args}\n",
|
| 247 | + " # Add *args if present\n", |
| 248 | + " if defn.args.vararg and args_kwargs: res[defn.args.vararg.lineno] = defn.args.vararg.arg\n", |
| 249 | + " # Add keyword-only args\n", |
| 250 | + " if args_kwargs: res.update({arg.lineno:arg.arg for arg in defn.args.kwonlyargs})\n", |
| 251 | + " # Add **kwargs if present\n", |
| 252 | + " if defn.args.kwarg and args_kwargs: res[defn.args.kwarg.lineno] = defn.args.kwarg.arg\n", |
247 | 253 | " if returns and defn.returns: res[defn.returns.lineno] = 'return'\n",
|
248 | 254 | " return res\n",
|
249 | 255 | " elif isdataclass(s):\n",
|
|
380 | 386 | "outputs": [],
|
381 | 387 | "source": [
|
382 | 388 | "#|export\n",
|
383 |
| - "def _docments(s, returns=True, eval_str=False):\n", |
| 389 | + "def _docments(s, returns=True, eval_str=False, args_kwargs=False):\n", |
384 | 390 | " \"`dict` of parameter names to 'docment-style' comments in function or string `s`\"\n",
|
385 | 391 | " nps = parse_docstring(s)\n",
|
386 | 392 | " if isclass(s) and not is_dataclass(s): s = s.__init__ # Constructor for a class\n",
|
387 | 393 | " comments = {o.start[0]:_clean_comment(o.string) for o in _tokens(s) if o.type==COMMENT}\n",
|
388 |
| - " parms = _param_locs(s, returns=returns) or {}\n", |
| 394 | + " parms = _param_locs(s, returns=returns, args_kwargs=args_kwargs) or {}\n", |
389 | 395 | " docs = {arg:_get_comment(line, arg, comments, parms) for line,arg in parms.items()}\n",
|
390 | 396 | "\n",
|
391 | 397 | " sig = signature_ex(s, True)\n",
|
|
466 | 472 | "docments(add)"
|
467 | 473 | ]
|
468 | 474 | },
|
| 475 | + { |
| 476 | + "cell_type": "code", |
| 477 | + "execution_count": null, |
| 478 | + "metadata": {}, |
| 479 | + "outputs": [ |
| 480 | + { |
| 481 | + "data": { |
| 482 | + "text/markdown": [ |
| 483 | + "```json\n", |
| 484 | + "{ 'a': 'the 1st number to add',\n", |
| 485 | + " 'args': 'some args',\n", |
| 486 | + " 'b': 'the 2nd number to add',\n", |
| 487 | + " 'kwargs': 'Passed to the `example` function',\n", |
| 488 | + " 'return': 'the result of adding `a` to `b`'}\n", |
| 489 | + "```" |
| 490 | + ], |
| 491 | + "text/plain": [ |
| 492 | + "{'args': 'some args',\n", |
| 493 | + " 'a': 'the 1st number to add',\n", |
| 494 | + " 'b': 'the 2nd number to add',\n", |
| 495 | + " 'kwargs': 'Passed to the `example` function',\n", |
| 496 | + " 'return': 'the result of adding `a` to `b`'}" |
| 497 | + ] |
| 498 | + }, |
| 499 | + "execution_count": null, |
| 500 | + "metadata": {}, |
| 501 | + "output_type": "execute_result" |
| 502 | + } |
| 503 | + ], |
| 504 | + "source": [ |
| 505 | + "def add(*args, # some args\n", |
| 506 | + " a:int, # the 1st number to add\n", |
| 507 | + " b=0, # the 2nd number to add\n", |
| 508 | + " **kwargs, # Passed to the `example` function\n", |
| 509 | + ")->int: # the result of adding `a` to `b`\n", |
| 510 | + " \"The sum of two numbers.\"\n", |
| 511 | + " return a+b\n", |
| 512 | + "\n", |
| 513 | + "docments(add, args_kwargs=True)" |
| 514 | + ] |
| 515 | + }, |
469 | 516 | {
|
470 | 517 | "cell_type": "markdown",
|
471 | 518 | "metadata": {},
|
|
910 | 957 | "docments(_b)"
|
911 | 958 | ]
|
912 | 959 | },
|
| 960 | + { |
| 961 | + "cell_type": "code", |
| 962 | + "execution_count": null, |
| 963 | + "metadata": {}, |
| 964 | + "outputs": [ |
| 965 | + { |
| 966 | + "data": { |
| 967 | + "text/markdown": [ |
| 968 | + "```json\n", |
| 969 | + "{'a': 'First', 'b': 'Second', 'return': None}\n", |
| 970 | + "```" |
| 971 | + ], |
| 972 | + "text/plain": [ |
| 973 | + "{'a': 'First', 'return': None, 'b': 'Second'}" |
| 974 | + ] |
| 975 | + }, |
| 976 | + "execution_count": null, |
| 977 | + "metadata": {}, |
| 978 | + "output_type": "execute_result" |
| 979 | + } |
| 980 | + ], |
| 981 | + "source": [ |
| 982 | + "def _a(a:int=2): return a # First\n", |
| 983 | + "\n", |
| 984 | + "@delegates(_a)\n", |
| 985 | + "def _b(b:str, # Second\n", |
| 986 | + " **kwargs): \n", |
| 987 | + " return b, (_a(**kwargs)) \n", |
| 988 | + "docments(_b)" |
| 989 | + ] |
| 990 | + }, |
913 | 991 | {
|
914 | 992 | "cell_type": "code",
|
915 | 993 | "execution_count": null,
|
|
1062 | 1140 | "#|hide\n",
|
1063 | 1141 | "import nbdev; nbdev.nbdev_export()"
|
1064 | 1142 | ]
|
1065 |
| - }, |
1066 |
| - { |
1067 |
| - "cell_type": "code", |
1068 |
| - "execution_count": null, |
1069 |
| - "metadata": {}, |
1070 |
| - "outputs": [], |
1071 |
| - "source": [] |
1072 | 1143 | }
|
1073 | 1144 | ],
|
1074 | 1145 | "metadata": {
|
|
0 commit comments