Skip to content

Commit 77d1929

Browse files
committed
getdoc
1 parent 85c5a7f commit 77d1929

File tree

3 files changed

+7
-41
lines changed

3 files changed

+7
-41
lines changed

fastcore/docments.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
from .utils import *
2020

2121
from fastcore import docscrape
22-
from inspect import isclass
22+
from inspect import isclass,getdoc
2323

2424
# Cell
2525
def docstring(sym):
2626
"Get docstring for `sym` for functions ad classes"
2727
if isinstance(sym, str): return sym
28-
res = getattr(sym, "__doc__", None)
29-
if not res and isclass(sym): res = nested_attr(sym, "__init__.__doc__")
28+
res = getdoc(sym)
29+
if not res and isclass(sym): res = getdoc(sym.__init__)
3030
return res or ""
3131

3232
# Cell

fastcore/docscrape.py

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,7 @@ def _parse_summary(self):
178178
summary = self._doc.read_to_next_empty_line()
179179
summary_str = " ".join([s.strip() for s in summary]).strip()
180180
compiled = re.compile(r'^([\w., ]+=)?\s*[\w\.]+\(.*\)$')
181-
if compiled.match(summary_str):
182-
if not self._is_at_section(): continue
181+
if compiled.match(summary_str) and not self._is_at_section(): continue
183182
break
184183

185184
if summary is not None: self['Summary'] = summary
@@ -222,42 +221,9 @@ def _obj(self):
222221
return None
223222

224223
def _error_location(self, msg, error=True):
225-
if self._obj is not None:
226-
# Make UserWarning more descriptive via object introspection.
227-
# Skip if introspection fails
228-
name = getattr(self._obj, '__name__', None)
229-
if name is None:
230-
name = getattr(getattr(self._obj, '__class__', None), '__name__', None)
231-
if name is not None: msg += f" in the docstring of {name}"
232224
if error: raise ValueError(msg)
233225
else: warn(msg)
234226

235-
# string conversion routines
236-
237-
def _str_header(self, name, symbol='-'): return [name, len(name)*symbol]
238-
def _str_indent(self, doc, indent=4): return [' '*indent + line for line in doc]
239-
240-
def _str_summary(self):
241-
if self['Summary']: return self['Summary'] + ['']
242-
return []
243-
244-
def _str_extended_summary(self):
245-
if self['Extended']: return self['Extended'] + ['']
246-
return []
247-
248-
def _str_param_list(self, name):
249-
out = []
250-
if self[name]:
251-
out += self._str_header(name)
252-
for param in self[name]:
253-
parts = []
254-
if param.name: parts.append(param.name)
255-
if param.type: parts.append(param.type)
256-
out += [' : '.join(parts)]
257-
if param.desc and ''.join(param.desc).strip(): out += self._str_indent(param.desc)
258-
out += ['']
259-
return out
260-
261227

262228
def dedent_lines(lines, split=True):
263229
"""Deindent a list of lines maximally"""

nbs/06_docments.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"from fastcore.utils import *\n",
3838
"\n",
3939
"from fastcore import docscrape\n",
40-
"from inspect import isclass"
40+
"from inspect import isclass,getdoc"
4141
]
4242
},
4343
{
@@ -143,8 +143,8 @@
143143
"def docstring(sym):\n",
144144
" \"Get docstring for `sym` for functions ad classes\"\n",
145145
" if isinstance(sym, str): return sym\n",
146-
" res = getattr(sym, \"__doc__\", None)\n",
147-
" if not res and isclass(sym): res = nested_attr(sym, \"__init__.__doc__\")\n",
146+
" res = getdoc(sym)\n",
147+
" if not res and isclass(sym): res = getdoc(sym.__init__)\n",
148148
" return res or \"\""
149149
]
150150
},

0 commit comments

Comments
 (0)