Skip to content

Commit be8dcd6

Browse files
committed
fixes #478
1 parent 597f3a5 commit be8dcd6

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

fastcore/docments.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,15 @@ def _docments(s, returns=True, eval_str=False):
155155
@delegates(_docments)
156156
def docments(elt, full=False, **kwargs):
157157
"Generates a `docment`"
158+
r = {}
159+
params = set(signature(elt).parameters)
160+
params.add('return')
161+
158162
def _update_docments(f, r):
159163
if hasattr(f, '__delwrap__'): _update_docments(f.__delwrap__, r)
160164
r.update({k:v for k,v in _docments(f, **kwargs).items()
161-
if full or v.get('docment',None)})
165+
if k in params and (full or v.get('docment',None))})
162166

163-
r = {}
164167
_update_docments(elt, r)
165168
if not full: r = {k:v['docment'] for k,v in r.items()}
166169
return AttrDict(r)

nbs/06_docments.ipynb

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -435,12 +435,15 @@
435435
"@delegates(_docments)\n",
436436
"def docments(elt, full=False, **kwargs):\n",
437437
" \"Generates a `docment`\"\n",
438+
" r = {}\n",
439+
" params = set(signature(elt).parameters)\n",
440+
" params.add('return')\n",
441+
"\n",
438442
" def _update_docments(f, r):\n",
439443
" if hasattr(f, '__delwrap__'): _update_docments(f.__delwrap__, r)\n",
440444
" r.update({k:v for k,v in _docments(f, **kwargs).items()\n",
441-
" if full or v.get('docment',None)})\n",
445+
" if k in params and (full or v.get('docment',None))})\n",
442446
"\n",
443-
" r = {}\n",
444447
" _update_docments(elt, r)\n",
445448
" if not full: r = {k:v['docment'] for k,v in r.items()}\n",
446449
" return AttrDict(r)"
@@ -462,15 +465,17 @@
462465
"data": {
463466
"text/markdown": [
464467
"```json\n",
465-
"{ 'a': 'the 1st number to add',\n",
466-
" 'b': 'the 2nd number to add',\n",
467-
" 'return': 'the result of adding `a` to `b`'}\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",
468473
"```"
469474
],
470475
"text/plain": [
471-
"{'a': 'the 1st number to add',\n",
472-
" 'b': 'the 2nd number to add',\n",
473-
" 'return': 'the result of adding `a` to `b`'}"
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.\"}"
474479
]
475480
},
476481
"execution_count": null,
@@ -500,21 +505,27 @@
500505
"```json\n",
501506
"{ 'a': { 'anno': 'int',\n",
502507
" 'default': <class 'inspect._empty'>,\n",
503-
" 'docment': 'the 1st number to add'},\n",
504-
" 'b': { 'anno': <class 'int'>,\n",
505-
" 'default': 0,\n",
506-
" 'docment': 'the 2nd number to add'},\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",
507515
" 'return': { 'anno': 'int',\n",
508516
" 'default': <class 'inspect._empty'>,\n",
509-
" 'docment': 'the result of adding `a` to `b`'}}\n",
517+
" 'docment': \"The result is calculated using Python's builtin `+` \"\n",
518+
" 'operator.'}}\n",
510519
"```"
511520
],
512521
"text/plain": [
513-
"{'a': {'docment': 'the 1st number to add',\n",
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",
514526
" 'anno': 'int',\n",
515527
" 'default': inspect._empty},\n",
516-
" 'b': {'docment': 'the 2nd number to add', 'anno': int, 'default': 0},\n",
517-
" 'return': {'docment': 'the result of adding `a` to `b`',\n",
528+
" 'return': {'docment': \"The result is calculated using Python's builtin `+` operator.\",\n",
518529
" 'anno': 'int',\n",
519530
" 'default': inspect._empty}}"
520531
]
@@ -546,11 +557,11 @@
546557
"```json\n",
547558
"{ 'anno': <class 'int'>,\n",
548559
" 'default': <class 'inspect._empty'>,\n",
549-
" 'docment': 'the 1st number to add'}\n",
560+
" 'docment': 'The first operand'}\n",
550561
"```"
551562
],
552563
"text/plain": [
553-
"{'docment': 'the 1st number to add', 'anno': int, 'default': inspect._empty}"
564+
"{'docment': 'The first operand', 'anno': int, 'default': inspect._empty}"
554565
]
555566
},
556567
"execution_count": null,

0 commit comments

Comments
 (0)