Skip to content

Commit 101c4f6

Browse files
authored
Merge pull request #464 from seeM/delegates-docments-verbose
exclude `passed to {func}` from docments by passing `verbose=False` to `delegates`
2 parents 881151d + db13d78 commit 101c4f6

File tree

4 files changed

+136
-131
lines changed

4 files changed

+136
-131
lines changed

fastcore/docments.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,12 @@ def docments(elt, full=False, **kwargs):
155155
res = _docments(elt, **kwargs)
156156
if hasattr(elt, "__delwrap__"): #for delegates
157157
delwrap_dict = _docments(elt.__delwrap__, **kwargs)
158+
verbose = getattr(elt,'__delopts__',{}).get('verbose',True)
158159
for k,v in res.items():
159160
if k in delwrap_dict and v["docment"] is None and k != "return":
160161
if delwrap_dict[k]["docment"] is not None:
161-
v["docment"] = delwrap_dict[k]["docment"] + f" passed to `{qual_name(elt.__delwrap__)}`"
162+
v["docment"] = delwrap_dict[k]["docment"]
163+
if verbose: v["docment"]+=f" passed to `{qual_name(elt.__delwrap__)}`"
162164
else: v['docment'] = f"Argument passed to `{qual_name(elt.__delwrap__)}`"
163165

164166
if not full: res = {k:v['docment'] for k,v in res.items()}

fastcore/meta.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ def _f(f):
106106
return _f
107107

108108
# %% ../nbs/07_meta.ipynb 68
109-
def delegates(to=None, keep=False, but=None):
109+
def delegates(to:FunctionType=None, # Delegatee
110+
keep=False, # Keep `kwargs` in decorated function?
111+
but:list=None, # Exclude these parameters from signature
112+
verbose=True): # Include `to` in docments?
110113
"Decorator: replace `**kwargs` in signature with params from `to`"
111114
if but is None: but = []
112115
def _f(f):
@@ -123,6 +126,7 @@ def _f(f):
123126
sigd.update(s2)
124127
if keep: sigd['kwargs'] = k
125128
else: from_f.__delwrap__ = to_f
129+
from_f.__delopts__ = dict(verbose=verbose)
126130
from_f.__signature__ = sig.replace(parameters=sigd.values())
127131
return f
128132
return _f

nbs/06_docments.ipynb

Lines changed: 89 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,12 @@
414414
" res = _docments(elt, **kwargs)\n",
415415
" if hasattr(elt, \"__delwrap__\"): #for delegates\n",
416416
" delwrap_dict = _docments(elt.__delwrap__, **kwargs)\n",
417+
" verbose = getattr(elt,'__delopts__',{}).get('verbose',True)\n",
417418
" for k,v in res.items():\n",
418419
" if k in delwrap_dict and v[\"docment\"] is None and k != \"return\":\n",
419420
" if delwrap_dict[k][\"docment\"] is not None:\n",
420-
" v[\"docment\"] = delwrap_dict[k][\"docment\"] + f\" passed to `{qual_name(elt.__delwrap__)}`\"\n",
421+
" v[\"docment\"] = delwrap_dict[k][\"docment\"]\n",
422+
" if verbose: v[\"docment\"]+=f\" passed to `{qual_name(elt.__delwrap__)}`\"\n",
421423
" else: v['docment'] = f\"Argument passed to `{qual_name(elt.__delwrap__)}`\"\n",
422424
" \n",
423425
" if not full: res = {k:v['docment'] for k,v in res.items()}\n",
@@ -906,7 +908,7 @@
906908
"cell_type": "markdown",
907909
"metadata": {},
908910
"source": [
909-
"Docments even works with `@delegates`:"
911+
"Docments even works with `delegates`:"
910912
]
911913
},
912914
{
@@ -915,34 +917,30 @@
915917
"metadata": {},
916918
"outputs": [],
917919
"source": [
918-
"#|hide\n",
919-
"\n",
920-
"# Test delegates\n",
921-
"from fastcore.meta import delegates\n",
922-
"def _a(\n",
923-
" a:int=2, # First\n",
924-
"):\n",
925-
" return a\n",
920+
"from fastcore.meta import delegates"
921+
]
922+
},
923+
{
924+
"cell_type": "code",
925+
"execution_count": null,
926+
"metadata": {},
927+
"outputs": [],
928+
"source": [
929+
"def _a(a:int=2): return a # First\n",
926930
"\n",
927931
"@delegates(_a)\n",
928-
"def _b(\n",
929-
" b:str, # Second\n",
930-
" **kwargs\n",
931-
"):\n",
932-
" return b, (_a(**kwargs))\n",
933-
"\n",
934-
"def _c(\n",
935-
" b:str, # Second\n",
936-
" a:int=2, # Blah\n",
937-
"):\n",
938-
" return b, a\n",
939-
"@delegates(_c)\n",
940-
"def _d(\n",
941-
" c:int, # First\n",
942-
" b:str,\n",
943-
" **kwargs\n",
944-
"):\n",
945-
" return c, _c(b, **kwargs)"
932+
"def _b(b:str, **kwargs): return b, (_a(**kwargs)) # Second"
933+
]
934+
},
935+
{
936+
"cell_type": "code",
937+
"execution_count": null,
938+
"metadata": {},
939+
"outputs": [],
940+
"source": [
941+
"#|hide\n",
942+
"test_eq(docments(_a, full=True)['a']['docment'],'First')\n",
943+
"test_eq(docments(_b, full=True)['a']['docment'],'First passed to `_a`')"
946944
]
947945
},
948946
{
@@ -954,17 +952,11 @@
954952
"data": {
955953
"text/markdown": [
956954
"```json\n",
957-
"{ 'a': {'anno': 'int', 'default': 2, 'docment': 'First'},\n",
958-
" 'return': { 'anno': <class 'inspect._empty'>,\n",
959-
" 'default': <class 'inspect._empty'>,\n",
960-
" 'docment': None}}\n",
955+
"{'a': 'First passed to `_a`', 'b': 'Second', 'return': None}\n",
961956
"```"
962957
],
963958
"text/plain": [
964-
"{'a': {'docment': 'First', 'anno': 'int', 'default': 2},\n",
965-
" 'return': {'docment': None,\n",
966-
" 'anno': inspect._empty,\n",
967-
" 'default': inspect._empty}}"
959+
"{'b': 'Second', 'a': 'First passed to `_a`', 'return': None}"
968960
]
969961
},
970962
"execution_count": null,
@@ -973,7 +965,63 @@
973965
}
974966
],
975967
"source": [
976-
"docments(_a, full=True)"
968+
"docments(_b)"
969+
]
970+
},
971+
{
972+
"cell_type": "code",
973+
"execution_count": null,
974+
"metadata": {},
975+
"outputs": [],
976+
"source": [
977+
"#|hide\n",
978+
"def _c(b:str, # Second\n",
979+
" a:int=2): return b, a # Third\n",
980+
"\n",
981+
"@delegates(_c)\n",
982+
"def _d(c:int, # First\n",
983+
" b:str, **kwargs): return c, _c(b, **kwargs)"
984+
]
985+
},
986+
{
987+
"cell_type": "code",
988+
"execution_count": null,
989+
"metadata": {},
990+
"outputs": [],
991+
"source": [
992+
"#|hide\n",
993+
"test_eq(docments(_c, full=True)['b']['docment'],'Second')\n",
994+
"test_eq(docments(_d, full=True)['b']['docment'],'Second passed to `_c`')\n",
995+
"_argset = {'a', 'b', 'c', 'return'}\n",
996+
"test_eq(docments(_d, full=True).keys() & _argset, _argset) # _d has the args a,b,c and return"
997+
]
998+
},
999+
{
1000+
"cell_type": "markdown",
1001+
"metadata": {},
1002+
"source": [
1003+
"If you pass `verbose=False` to `delegates`, the `'passed to `{func}`'` part will be excluded from docments:"
1004+
]
1005+
},
1006+
{
1007+
"cell_type": "code",
1008+
"execution_count": null,
1009+
"metadata": {},
1010+
"outputs": [],
1011+
"source": [
1012+
"@delegates(_a, verbose=False)\n",
1013+
"def _e(b:str, **kwargs): return b, (_a(**kwargs)) # Second, not passed"
1014+
]
1015+
},
1016+
{
1017+
"cell_type": "code",
1018+
"execution_count": null,
1019+
"metadata": {},
1020+
"outputs": [],
1021+
"source": [
1022+
"#|hide\n",
1023+
"test_eq(docments(_e, full=True)['a']['docment'],'First')\n",
1024+
"test_eq(docments(_e, full=True)['b']['docment'],'Second, not passed')"
9771025
]
9781026
},
9791027
{
@@ -985,25 +1033,11 @@
9851033
"data": {
9861034
"text/markdown": [
9871035
"```json\n",
988-
"{ 'a': {'anno': 'int', 'default': 2, 'docment': 'Blah passed to `_c`'},\n",
989-
" 'b': { 'anno': 'str',\n",
990-
" 'default': <class 'inspect._empty'>,\n",
991-
" 'docment': 'Second passed to `_c`'},\n",
992-
" 'c': {'anno': 'int', 'default': <class 'inspect._empty'>, 'docment': 'First'},\n",
993-
" 'return': { 'anno': <class 'inspect._empty'>,\n",
994-
" 'default': <class 'inspect._empty'>,\n",
995-
" 'docment': None}}\n",
1036+
"{'a': 'First', 'b': 'Second, not passed', 'return': None}\n",
9961037
"```"
9971038
],
9981039
"text/plain": [
999-
"{'c': {'docment': 'First', 'anno': 'int', 'default': inspect._empty},\n",
1000-
" 'b': {'docment': 'Second passed to `_c`',\n",
1001-
" 'anno': 'str',\n",
1002-
" 'default': inspect._empty},\n",
1003-
" 'a': {'docment': 'Blah passed to `_c`', 'anno': 'int', 'default': 2},\n",
1004-
" 'return': {'docment': None,\n",
1005-
" 'anno': inspect._empty,\n",
1006-
" 'default': inspect._empty}}"
1040+
"{'b': 'Second, not passed', 'a': 'First', 'return': None}"
10071041
]
10081042
},
10091043
"execution_count": null,
@@ -1012,20 +1046,7 @@
10121046
}
10131047
],
10141048
"source": [
1015-
"docments(_d, full=True)"
1016-
]
1017-
},
1018-
{
1019-
"cell_type": "code",
1020-
"execution_count": null,
1021-
"metadata": {},
1022-
"outputs": [],
1023-
"source": [
1024-
"test_eq(docments(_b, full=True)['a']['docment'],'First passed to `_a`')\n",
1025-
"test_eq(docments(_c, full=True)['b']['docment'],'Second')\n",
1026-
"test_eq(docments(_d, full=True)['b']['docment'], 'Second passed to `_c`')\n",
1027-
"_argset = {'a', 'b', 'c', 'return'}\n",
1028-
"test_eq(set(docments(_d, full=True).keys()).intersection(_argset), _argset) # _d has the args a,b,c and return"
1049+
"docments(_e)"
10291050
]
10301051
},
10311052
{
@@ -1039,27 +1060,7 @@
10391060
"cell_type": "code",
10401061
"execution_count": null,
10411062
"metadata": {},
1042-
"outputs": [
1043-
{
1044-
"name": "stdout",
1045-
"output_type": "stream",
1046-
"text": [
1047-
"Converted 00_test.ipynb.\n",
1048-
"Converted 01_basics.ipynb.\n",
1049-
"Converted 02_foundation.ipynb.\n",
1050-
"Converted 03_xtras.ipynb.\n",
1051-
"Converted 03a_parallel.ipynb.\n",
1052-
"Converted 03b_net.ipynb.\n",
1053-
"Converted 04_dispatch.ipynb.\n",
1054-
"Converted 05_transform.ipynb.\n",
1055-
"Converted 06_docments.ipynb.\n",
1056-
"Converted 07_meta.ipynb.\n",
1057-
"Converted 08_script.ipynb.\n",
1058-
"Converted index.ipynb.\n",
1059-
"Converted parallel_win.ipynb.\n"
1060-
]
1061-
}
1062-
],
1063+
"outputs": [],
10631064
"source": [
10641065
"#|hide\n",
10651066
"#|eval: false\n",

0 commit comments

Comments
 (0)