|
245 | 245 | " if isinstance(defn, (FunctionDef, AsyncFunctionDef)):\n",
|
246 | 246 | " res = {arg.lineno:arg.arg for arg in defn.args.args}\n",
|
247 | 247 | " # Add *args if present\n",
|
248 |
| - " if defn.args.vararg and args_kwargs: res[defn.args.vararg.lineno] = defn.args.vararg.arg\n", |
| 248 | + " if defn.args.vararg: res[defn.args.vararg.lineno] = defn.args.vararg.arg\n", |
249 | 249 | " # Add keyword-only args\n",
|
250 |
| - " if args_kwargs: res.update({arg.lineno:arg.arg for arg in defn.args.kwonlyargs})\n", |
| 250 | + " res.update({arg.lineno:arg.arg for arg in defn.args.kwonlyargs})\n", |
251 | 251 | " # Add **kwargs if present\n",
|
252 | 252 | " if defn.args.kwarg and args_kwargs: res[defn.args.kwarg.lineno] = defn.args.kwarg.arg\n",
|
253 | 253 | " if returns and defn.returns: res[defn.returns.lineno] = 'return'\n",
|
|
258 | 258 | " return None"
|
259 | 259 | ]
|
260 | 260 | },
|
| 261 | + { |
| 262 | + "cell_type": "code", |
| 263 | + "execution_count": null, |
| 264 | + "metadata": {}, |
| 265 | + "outputs": [ |
| 266 | + { |
| 267 | + "data": { |
| 268 | + "text/plain": [ |
| 269 | + "{2: 'a', 3: 'b', 4: 'return'}" |
| 270 | + ] |
| 271 | + }, |
| 272 | + "execution_count": null, |
| 273 | + "metadata": {}, |
| 274 | + "output_type": "execute_result" |
| 275 | + } |
| 276 | + ], |
| 277 | + "source": [ |
| 278 | + "parms = _param_locs(add)\n", |
| 279 | + "parms" |
| 280 | + ] |
| 281 | + }, |
261 | 282 | {
|
262 | 283 | "cell_type": "code",
|
263 | 284 | "execution_count": null,
|
|
284 | 305 | " line -= 1\n",
|
285 | 306 | " return dedent('\\n'.join(reversed(res))) if res else None\n",
|
286 | 307 | "\n",
|
287 |
| - "def _get_full(anno, name, default, docs):\n", |
288 |
| - " if anno==empty and default!=empty: anno = type(default)\n", |
289 |
| - " return AttrDict(docment=docs.get(name), anno=anno, default=default)" |
| 308 | + "def _get_full(p, docs):\n", |
| 309 | + " anno = p.annotation\n", |
| 310 | + " if anno==empty:\n", |
| 311 | + " if p.default!=empty: anno = type(p.default)\n", |
| 312 | + " elif p.kind in (Parameter.VAR_POSITIONAL, Parameter.VAR_KEYWORD): anno = p.kind\n", |
| 313 | + " return AttrDict(docment=docs.get(p.name), anno=anno, default=p.default)" |
| 314 | + ] |
| 315 | + }, |
| 316 | + { |
| 317 | + "cell_type": "code", |
| 318 | + "execution_count": null, |
| 319 | + "metadata": {}, |
| 320 | + "outputs": [ |
| 321 | + { |
| 322 | + "data": { |
| 323 | + "text/plain": [ |
| 324 | + "'the 1st number to add'" |
| 325 | + ] |
| 326 | + }, |
| 327 | + "execution_count": null, |
| 328 | + "metadata": {}, |
| 329 | + "output_type": "execute_result" |
| 330 | + } |
| 331 | + ], |
| 332 | + "source": [ |
| 333 | + "_get_comment(2, 'a', {2: ' the 1st number to add'}, parms)" |
290 | 334 | ]
|
291 | 335 | },
|
292 | 336 | {
|
|
395 | 439 | " docs = {arg:_get_comment(line, arg, comments, parms) for line,arg in parms.items()}\n",
|
396 | 440 | "\n",
|
397 | 441 | " sig = signature_ex(s, True)\n",
|
398 |
| - " res = {arg:_get_full(p.annotation, p.name, p.default, docs) for arg,p in sig.parameters.items()}\n", |
399 |
| - " if returns: res['return'] = _get_full(sig.return_annotation, 'return', empty, docs)\n", |
| 442 | + " res = {name:_get_full(p, docs) for name,p in sig.parameters.items()}\n", |
| 443 | + " if returns: res['return'] = AttrDict(docment=docs.get('return'), anno=sig.return_annotation, default=empty)\n", |
400 | 444 | " res = _merge_docs(res, nps)\n",
|
401 | 445 | " if eval_str:\n",
|
402 | 446 | " hints = type_hints(s)\n",
|
|
413 | 457 | "source": [
|
414 | 458 | "#|export\n",
|
415 | 459 | "@delegates(_docments)\n",
|
416 |
| - "def docments(elt, full=False, **kwargs):\n", |
| 460 | + "def docments(elt, full=False, args_kwargs=False, **kwargs):\n", |
417 | 461 | " \"Generates a `docment`\"\n",
|
| 462 | + " if full: args_kwargs=True\n", |
418 | 463 | " r = {}\n",
|
419 | 464 | " params = set(signature(elt).parameters)\n",
|
420 | 465 | " params.add('return')\n",
|
|
472 | 517 | "docments(add)"
|
473 | 518 | ]
|
474 | 519 | },
|
| 520 | + { |
| 521 | + "cell_type": "markdown", |
| 522 | + "metadata": {}, |
| 523 | + "source": [ |
| 524 | + "`args_kwargs=True` adds args and kwargs docs too:" |
| 525 | + ] |
| 526 | + }, |
475 | 527 | {
|
476 | 528 | "cell_type": "code",
|
477 | 529 | "execution_count": null,
|
|
484 | 536 | "{ 'a': 'the 1st number to add',\n",
|
485 | 537 | " 'args': 'some args',\n",
|
486 | 538 | " 'b': 'the 2nd number to add',\n",
|
487 |
| - " 'kwargs': 'Passed to the `example` function',\n", |
| 539 | + " 'kwargs': None,\n", |
488 | 540 | " 'return': 'the result of adding `a` to `b`'}\n",
|
489 | 541 | "```"
|
490 | 542 | ],
|
491 | 543 | "text/plain": [
|
492 | 544 | "{'args': 'some args',\n",
|
493 | 545 | " 'a': 'the 1st number to add',\n",
|
494 | 546 | " 'b': 'the 2nd number to add',\n",
|
495 |
| - " 'kwargs': 'Passed to the `example` function',\n", |
| 547 | + " 'kwargs': None,\n", |
496 | 548 | " 'return': 'the result of adding `a` to `b`'}"
|
497 | 549 | ]
|
498 | 550 | },
|
|
517 | 569 | "cell_type": "markdown",
|
518 | 570 | "metadata": {},
|
519 | 571 | "source": [
|
520 |
| - "If you pass `full=True`, the values are `dict` of defaults, types, and docments as values. Note that the type annotation is inferred from the default value, if the annotation is empty and a default is supplied." |
| 572 | + "If you pass `full=True`, the values are `dict` of defaults, types, and docments as values. Note that the type annotation is inferred from the default value, if the annotation is empty and a default is supplied. (Note that for `full`, `args_kwargs=True` is always set too.)" |
521 | 573 | ]
|
522 | 574 | },
|
523 | 575 | {
|
|
532 | 584 | "{ 'a': { 'anno': <class 'int'>,\n",
|
533 | 585 | " 'default': <class 'inspect._empty'>,\n",
|
534 | 586 | " 'docment': 'the 1st number to add'},\n",
|
| 587 | + " 'args': { 'anno': <_ParameterKind.VAR_POSITIONAL: 2>,\n", |
| 588 | + " 'default': <class 'inspect._empty'>,\n", |
| 589 | + " 'docment': 'some args'},\n", |
535 | 590 | " 'b': { 'anno': <class 'int'>,\n",
|
536 | 591 | " 'default': 0,\n",
|
537 | 592 | " 'docment': 'the 2nd number to add'},\n",
|
| 593 | + " 'kwargs': { 'anno': <_ParameterKind.VAR_KEYWORD: 4>,\n", |
| 594 | + " 'default': <class 'inspect._empty'>,\n", |
| 595 | + " 'docment': None},\n", |
538 | 596 | " 'return': { 'anno': <class 'int'>,\n",
|
539 | 597 | " 'default': <class 'inspect._empty'>,\n",
|
540 | 598 | " 'docment': 'the result of adding `a` to `b`'}}\n",
|
541 | 599 | "```"
|
542 | 600 | ],
|
543 | 601 | "text/plain": [
|
544 |
| - "{'a': {'docment': 'the 1st number to add',\n", |
| 602 | + "{'args': {'docment': 'some args',\n", |
| 603 | + " 'anno': <_ParameterKind.VAR_POSITIONAL: 2>,\n", |
| 604 | + " 'default': inspect._empty},\n", |
| 605 | + " 'a': {'docment': 'the 1st number to add',\n", |
545 | 606 | " 'anno': int,\n",
|
546 | 607 | " 'default': inspect._empty},\n",
|
547 | 608 | " 'b': {'docment': 'the 2nd number to add', 'anno': int, 'default': 0},\n",
|
| 609 | + " 'kwargs': {'docment': None,\n", |
| 610 | + " 'anno': <_ParameterKind.VAR_KEYWORD: 4>,\n", |
| 611 | + " 'default': inspect._empty},\n", |
548 | 612 | " 'return': {'docment': 'the result of adding `a` to `b`',\n",
|
549 | 613 | " 'anno': int,\n",
|
550 | 614 | " 'default': inspect._empty}}"
|
|
968 | 1032 | "data": {
|
969 | 1033 | "text/markdown": [
|
970 | 1034 | "```json\n",
|
971 |
| - "{'a': 'First', 'b': 'Second', 'return': None}\n", |
| 1035 | + "{ 'a': {'anno': <class 'int'>, 'default': 2, 'docment': 'First'},\n", |
| 1036 | + " 'b': { 'anno': 'str',\n", |
| 1037 | + " 'default': <class 'inspect._empty'>,\n", |
| 1038 | + " 'docment': 'Second'},\n", |
| 1039 | + " 'return': { 'anno': <class 'inspect._empty'>,\n", |
| 1040 | + " 'default': <class 'inspect._empty'>,\n", |
| 1041 | + " 'docment': None}}\n", |
972 | 1042 | "```"
|
973 | 1043 | ],
|
974 | 1044 | "text/plain": [
|
975 |
| - "{'a': 'First', 'return': None, 'b': 'Second'}" |
| 1045 | + "{'a': {'docment': 'First', 'anno': int, 'default': 2},\n", |
| 1046 | + " 'return': {'docment': None,\n", |
| 1047 | + " 'anno': inspect._empty,\n", |
| 1048 | + " 'default': inspect._empty},\n", |
| 1049 | + " 'b': {'docment': 'Second', 'anno': 'str', 'default': inspect._empty}}" |
976 | 1050 | ]
|
977 | 1051 | },
|
978 | 1052 | "execution_count": null,
|
|
981 | 1055 | }
|
982 | 1056 | ],
|
983 | 1057 | "source": [
|
984 |
| - "def _a(a:int=2): return a # First\n", |
985 |
| - "\n", |
986 |
| - "@delegates(_a)\n", |
987 |
| - "def _b(b:str, # Second\n", |
988 |
| - " **kwargs): \n", |
989 |
| - " return b, (_a(**kwargs)) \n", |
990 |
| - "docments(_b)" |
| 1058 | + "docments(_b, full=True)" |
991 | 1059 | ]
|
992 | 1060 | },
|
993 | 1061 | {
|
|
1142 | 1210 | "#|hide\n",
|
1143 | 1211 | "import nbdev; nbdev.nbdev_export()"
|
1144 | 1212 | ]
|
| 1213 | + }, |
| 1214 | + { |
| 1215 | + "cell_type": "code", |
| 1216 | + "execution_count": null, |
| 1217 | + "metadata": {}, |
| 1218 | + "outputs": [], |
| 1219 | + "source": [] |
1145 | 1220 | }
|
1146 | 1221 | ],
|
1147 | 1222 | "metadata": {
|
|
0 commit comments