4
4
from __future__ import annotations
5
5
6
6
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' ]
8
9
9
10
# Cell
10
11
#nbdev_comment from __future__ import annotations
18
19
from inspect import getsource ,isfunction ,ismethod ,isclass ,signature ,Parameter
19
20
from dataclasses import dataclass , is_dataclass
20
21
from .utils import *
21
-
22
+ from . meta import delegates
22
23
from fastcore import docscrape
23
24
from inspect import isclass ,getdoc
24
25
@@ -108,7 +109,23 @@ def _merge_docs(dms, npdocs):
108
109
return params
109
110
110
111
# 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 ):
112
129
"`dict` of parameter names to 'docment-style' comments in function or string `s`"
113
130
nps = parse_docstring (s )
114
131
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):
125
142
hints = type_hints (s )
126
143
for k ,v in res .items ():
127
144
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
+
128
160
if not full : res = {k :v ['docment' ] for k ,v in res .items ()}
129
161
return AttrDict (res )
0 commit comments