44from __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
1819from inspect import getsource ,isfunction ,ismethod ,isclass ,signature ,Parameter
1920from dataclasses import dataclass , is_dataclass
2021from .utils import *
21-
22+ from . meta import delegates
2223from fastcore import docscrape
2324from 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