|
205 | 205 | "parse_docstring(add_np)" |
206 | 206 | ] |
207 | 207 | }, |
208 | | - { |
209 | | - "cell_type": "markdown", |
210 | | - "metadata": {}, |
211 | | - "source": [ |
212 | | - "## Usage" |
213 | | - ] |
214 | | - }, |
215 | 208 | { |
216 | 209 | "cell_type": "code", |
217 | 210 | "execution_count": null, |
|
398 | 391 | "assert qual_name(docscrape) == 'fastcore.docscrape'" |
399 | 392 | ] |
400 | 393 | }, |
| 394 | + { |
| 395 | + "cell_type": "markdown", |
| 396 | + "metadata": {}, |
| 397 | + "source": [ |
| 398 | + "## Docments" |
| 399 | + ] |
| 400 | + }, |
401 | 401 | { |
402 | 402 | "cell_type": "code", |
403 | 403 | "execution_count": null, |
|
441 | 441 | "\n", |
442 | 442 | " def _update_docments(f, r):\n", |
443 | 443 | " if hasattr(f, '__delwrap__'): _update_docments(f.__delwrap__, r)\n", |
444 | | - " r.update({k:v for k,v in _docments(f, **kwargs).items()\n", |
445 | | - " if k in params and (full or v.get('docment',None))})\n", |
| 444 | + " r.update({k:v for k,v in _docments(f, **kwargs).items() if k in params\n", |
| 445 | + " and (v.get('docment', None) or not nested_idx(r, k, 'docment'))})\n", |
446 | 446 | "\n", |
447 | 447 | " _update_docments(elt, r)\n", |
448 | 448 | " if not full: r = {k:v['docment'] for k,v in r.items()}\n", |
|
465 | 465 | "data": { |
466 | 466 | "text/markdown": [ |
467 | 467 | "```json\n", |
468 | | - "{ 'a': 'The first operand',\n", |
469 | | - " 'b': 'This is the second of the operands to the *addition* operator.\\n'\n", |
470 | | - " 'Note that passing a negative value here is the equivalent of the '\n", |
471 | | - " '*subtraction* operator.',\n", |
472 | | - " 'return': \"The result is calculated using Python's builtin `+` operator.\"}\n", |
| 468 | + "{ 'a': 'the 1st number to add',\n", |
| 469 | + " 'b': 'the 2nd number to add',\n", |
| 470 | + " 'return': 'the result of adding `a` to `b`'}\n", |
473 | 471 | "```" |
474 | 472 | ], |
475 | 473 | "text/plain": [ |
476 | | - "{'a': 'The first operand',\n", |
477 | | - " 'b': 'This is the second of the operands to the *addition* operator.\\nNote that passing a negative value here is the equivalent of the *subtraction* operator.',\n", |
478 | | - " 'return': \"The result is calculated using Python's builtin `+` operator.\"}" |
| 474 | + "{'a': 'the 1st number to add',\n", |
| 475 | + " 'b': 'the 2nd number to add',\n", |
| 476 | + " 'return': 'the result of adding `a` to `b`'}" |
479 | 477 | ] |
480 | 478 | }, |
481 | 479 | "execution_count": null, |
|
484 | 482 | } |
485 | 483 | ], |
486 | 484 | "source": [ |
| 485 | + "def add(\n", |
| 486 | + " a:int, # the 1st number to add\n", |
| 487 | + " b=0, # the 2nd number to add\n", |
| 488 | + ")->int: # the result of adding `a` to `b`\n", |
| 489 | + " \"The sum of two numbers.\"\n", |
| 490 | + " return a+b\n", |
| 491 | + "\n", |
487 | 492 | "docments(add)" |
488 | 493 | ] |
489 | 494 | }, |
|
505 | 510 | "```json\n", |
506 | 511 | "{ 'a': { 'anno': 'int',\n", |
507 | 512 | " 'default': <class 'inspect._empty'>,\n", |
508 | | - " 'docment': 'The first operand'},\n", |
509 | | - " 'b': { 'anno': 'int',\n", |
510 | | - " 'default': <class 'inspect._empty'>,\n", |
511 | | - " 'docment': 'This is the second of the operands to the *addition* '\n", |
512 | | - " 'operator.\\n'\n", |
513 | | - " 'Note that passing a negative value here is the equivalent '\n", |
514 | | - " 'of the *subtraction* operator.'},\n", |
| 513 | + " 'docment': 'the 1st number to add'},\n", |
| 514 | + " 'b': { 'anno': <class 'int'>,\n", |
| 515 | + " 'default': 0,\n", |
| 516 | + " 'docment': 'the 2nd number to add'},\n", |
515 | 517 | " 'return': { 'anno': 'int',\n", |
516 | 518 | " 'default': <class 'inspect._empty'>,\n", |
517 | | - " 'docment': \"The result is calculated using Python's builtin `+` \"\n", |
518 | | - " 'operator.'}}\n", |
| 519 | + " 'docment': 'the result of adding `a` to `b`'}}\n", |
519 | 520 | "```" |
520 | 521 | ], |
521 | 522 | "text/plain": [ |
522 | | - "{'a': {'docment': 'The first operand',\n", |
523 | | - " 'anno': 'int',\n", |
524 | | - " 'default': inspect._empty},\n", |
525 | | - " 'b': {'docment': 'This is the second of the operands to the *addition* operator.\\nNote that passing a negative value here is the equivalent of the *subtraction* operator.',\n", |
| 523 | + "{'a': {'docment': 'the 1st number to add',\n", |
526 | 524 | " 'anno': 'int',\n", |
527 | 525 | " 'default': inspect._empty},\n", |
528 | | - " 'return': {'docment': \"The result is calculated using Python's builtin `+` operator.\",\n", |
| 526 | + " 'b': {'docment': 'the 2nd number to add', 'anno': int, 'default': 0},\n", |
| 527 | + " 'return': {'docment': 'the result of adding `a` to `b`',\n", |
529 | 528 | " 'anno': 'int',\n", |
530 | 529 | " 'default': inspect._empty}}" |
531 | 530 | ] |
|
557 | 556 | "```json\n", |
558 | 557 | "{ 'anno': <class 'int'>,\n", |
559 | 558 | " 'default': <class 'inspect._empty'>,\n", |
560 | | - " 'docment': 'The first operand'}\n", |
| 559 | + " 'docment': 'the 1st number to add'}\n", |
561 | 560 | "```" |
562 | 561 | ], |
563 | 562 | "text/plain": [ |
564 | | - "{'docment': 'The first operand', 'anno': int, 'default': inspect._empty}" |
| 563 | + "{'docment': 'the 1st number to add', 'anno': int, 'default': inspect._empty}" |
565 | 564 | ] |
566 | 565 | }, |
567 | 566 | "execution_count": null, |
|
663 | 662 | "data": { |
664 | 663 | "text/markdown": [ |
665 | 664 | "```json\n", |
666 | | - "{'a': 'First operand', 'b': '2nd operand'}\n", |
| 665 | + "{'a': 'First operand', 'b': '2nd operand', 'return': None}\n", |
667 | 666 | "```" |
668 | 667 | ], |
669 | 668 | "text/plain": [ |
670 | | - "{'a': 'First operand', 'b': '2nd operand'}" |
| 669 | + "{'a': 'First operand', 'b': '2nd operand', 'return': None}" |
671 | 670 | ] |
672 | 671 | }, |
673 | 672 | "execution_count": null, |
|
688 | 687 | "data": { |
689 | 688 | "text/markdown": [ |
690 | 689 | "```json\n", |
691 | | - "{'return': 'Integral result of addition operator'}\n", |
| 690 | + "{'return': 'Integral result of addition operator', 'self': None}\n", |
692 | 691 | "```" |
693 | 692 | ], |
694 | 693 | "text/plain": [ |
695 | | - "{'return': 'Integral result of addition operator'}" |
| 694 | + "{'self': None, 'return': 'Integral result of addition operator'}" |
696 | 695 | ] |
697 | 696 | }, |
698 | 697 | "execution_count": null, |
|
855 | 854 | " foo:str, # docment for parameter foo\n", |
856 | 855 | " ):...\n", |
857 | 856 | " \n", |
858 | | - "test_eq(docments(_F.class_method), {'foo': 'docment for parameter foo'})" |
| 857 | + "test_eq(docments(_F.class_method), {'foo': 'docment for parameter foo', 'return': None})" |
859 | 858 | ] |
860 | 859 | }, |
861 | 860 | { |
|
874 | 873 | "from fastcore.meta import delegates" |
875 | 874 | ] |
876 | 875 | }, |
877 | | - { |
878 | | - "cell_type": "code", |
879 | | - "execution_count": null, |
880 | | - "metadata": {}, |
881 | | - "outputs": [], |
882 | | - "source": [ |
883 | | - "def _a(a:int=2): return a # First\n", |
884 | | - "\n", |
885 | | - "@delegates(_a)\n", |
886 | | - "def _b(b:str, **kwargs): return b, (_a(**kwargs)) # Second" |
887 | | - ] |
888 | | - }, |
889 | 876 | { |
890 | 877 | "cell_type": "code", |
891 | 878 | "execution_count": null, |
|
895 | 882 | "data": { |
896 | 883 | "text/markdown": [ |
897 | 884 | "```json\n", |
898 | | - "{'a': 'First', 'b': 'Second'}\n", |
| 885 | + "{'a': 'First', 'b': 'Second', 'return': None}\n", |
899 | 886 | "```" |
900 | 887 | ], |
901 | 888 | "text/plain": [ |
902 | | - "{'a': 'First', 'b': 'Second'}" |
| 889 | + "{'a': 'First', 'return': None, 'b': 'Second'}" |
903 | 890 | ] |
904 | 891 | }, |
905 | 892 | "execution_count": null, |
|
908 | 895 | } |
909 | 896 | ], |
910 | 897 | "source": [ |
| 898 | + "def _a(a:int=2): return a # First\n", |
| 899 | + "\n", |
| 900 | + "@delegates(_a)\n", |
| 901 | + "def _b(b:str, **kwargs): return b, (_a(**kwargs)) # Second\n", |
| 902 | + "\n", |
911 | 903 | "docments(_b)" |
912 | 904 | ] |
913 | 905 | }, |
|
923 | 915 | "\n", |
924 | 916 | "@delegates(_c)\n", |
925 | 917 | "def _d(c:int, # First\n", |
926 | | - " b:str, **kwargs): return c, _c(b, **kwargs)" |
| 918 | + " b:str, **kwargs\n", |
| 919 | + " )->int:\n", |
| 920 | + " return c, _c(b, **kwargs)" |
927 | 921 | ] |
928 | 922 | }, |
929 | 923 | { |
|
934 | 928 | "source": [ |
935 | 929 | "#|hide\n", |
936 | 930 | "test_eq(docments(_c, full=True)['b']['docment'],'Second')\n", |
937 | | - "test_eq(docments(_d, full=True)['b']['docment'],None)\n", |
| 931 | + "test_eq(docments(_d, full=True)['b']['docment'],'Second')\n", |
938 | 932 | "_argset = {'a', 'b', 'c', 'return'}\n", |
939 | 933 | "test_eq(docments(_d, full=True).keys() & _argset, _argset) # _d has the args a,b,c and return" |
940 | 934 | ] |
|
0 commit comments