Skip to content

Commit 4f8e825

Browse files
authored
Merge pull request #418 from fastai/delegates-in-docments
make `docments` handle delegates
2 parents 8598aa0 + 1091c88 commit 4f8e825

File tree

3 files changed

+303
-47
lines changed

3 files changed

+303
-47
lines changed

fastcore/_nbdev.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@
253253
"get_dataclass_source": "06_docments.ipynb",
254254
"get_source": "06_docments.ipynb",
255255
"empty": "06_docments.ipynb",
256+
"get_name": "06_docments.ipynb",
257+
"qual_name": "06_docments.ipynb",
256258
"docments": "06_docments.ipynb",
257259
"test_sig": "07_meta.ipynb",
258260
"FixSigMeta": "07_meta.ipynb",

fastcore/docments.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
from __future__ import annotations
55

66

7-
__all__ = ['docstring', 'parse_docstring', 'isdataclass', 'get_dataclass_source', 'get_source', 'empty', 'docments']
7+
__all__ = ['docstring', 'parse_docstring', 'isdataclass', 'get_dataclass_source', 'get_source', 'empty', 'get_name',
8+
'qual_name', 'docments']
89

910
# Cell
1011
#nbdev_comment from __future__ import annotations
@@ -18,7 +19,7 @@
1819
from inspect import getsource,isfunction,ismethod,isclass,signature,Parameter
1920
from dataclasses import dataclass, is_dataclass
2021
from .utils import *
21-
22+
from .meta import delegates
2223
from fastcore import docscrape
2324
from inspect import isclass,getdoc
2425

@@ -108,7 +109,23 @@ def _merge_docs(dms, npdocs):
108109
return params
109110

110111
# Cell
111-
def docments(s, full=False, returns=True, eval_str=False):
112+
def get_name(obj):
113+
"Get the name of `obj`"
114+
if hasattr(obj, '__name__'): return obj.__name__
115+
elif getattr(obj, '_name', False): return obj._name
116+
elif hasattr(obj,'__origin__'): return str(obj.__origin__).split('.')[-1] #for types
117+
elif type(obj)==property: return _get_property_name(obj)
118+
else: return str(obj).split('.')[-1]
119+
120+
# Cell
121+
def qual_name(obj):
122+
"Get the qualified name of `obj`"
123+
if hasattr(obj,'__qualname__'): return obj.__qualname__
124+
if ismethod(obj): return f"{get_name(obj.__self__)}.{get_name(fn)}"
125+
return get_name(obj)
126+
127+
# Cell
128+
def _docments(s, returns=True, eval_str=False):
112129
"`dict` of parameter names to 'docment-style' comments in function or string `s`"
113130
nps = parse_docstring(s)
114131
if isclass(s) and not is_dataclass(s): s = s.__init__ # Constructor for a class
@@ -125,5 +142,20 @@ def docments(s, full=False, returns=True, eval_str=False):
125142
hints = type_hints(s)
126143
for k,v in res.items():
127144
if k in hints: v['anno'] = hints.get(k)
145+
return res
146+
147+
# Cell
148+
@delegates(_docments)
149+
def docments(elt, full=False, **kwargs):
150+
"Generates a `docment`"
151+
res = _docments(elt, **kwargs)
152+
if hasattr(elt, "__delwrap__"): #for delegates
153+
delwrap_dict = _docments(elt.__delwrap__, **kwargs)
154+
for k,v in res.items():
155+
if k in delwrap_dict and v["docment"] is None and k != "return":
156+
if delwrap_dict[k]["docment"] is not None:
157+
v["docment"] = delwrap_dict[k]["docment"] + f" passed to `{qual_name(elt.__delwrap__)}`"
158+
else: v['docment'] = f"Argument passed to `{qual_name(elt.__delwrap__)}`"
159+
128160
if not full: res = {k:v['docment'] for k,v in res.items()}
129161
return AttrDict(res)

0 commit comments

Comments
 (0)