Skip to content

Commit 319e05b

Browse files
committed
fixes #797
1 parent 71e3523 commit 319e05b

3 files changed

Lines changed: 26 additions & 6 deletions

File tree

fastcore/_modidx.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@
316316
'fastcore.docments._parses': ('docments.html#_parses', 'fastcore/docments.py'),
317317
'fastcore.docments._show_param': ('docments.html#_show_param', 'fastcore/docments.py'),
318318
'fastcore.docments._tokens': ('docments.html#_tokens', 'fastcore/docments.py'),
319+
'fastcore.docments._unwrap_sym': ('docments.html#_unwrap_sym', 'fastcore/docments.py'),
320+
'fastcore.docments.can_render': ('docments.html#can_render', 'fastcore/docments.py'),
319321
'fastcore.docments.docments': ('docments.html#docments', 'fastcore/docments.py'),
320322
'fastcore.docments.docstring': ('docments.html#docstring', 'fastcore/docments.py'),
321323
'fastcore.docments.extract_docstrings': ('docments.html#extract_docstrings', 'fastcore/docments.py'),

fastcore/docments.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# %% auto #0
66
__all__ = ['empty', 'docstring', 'parse_docstring', 'isdataclass', 'get_dataclass_source', 'get_source', 'get_name', 'qual_name',
77
'docments', 'sig_source', 'extract_docstrings', 'DocmentTbl', 'DocmentList', 'DocmentText', 'sig2str',
8-
'ShowDocRenderer', 'MarkdownRenderer']
8+
'can_render', 'ShowDocRenderer', 'MarkdownRenderer']
99

1010
# %% ../nbs/04_docments.ipynb #4c662acc
1111
import re,ast,inspect
@@ -391,6 +391,16 @@ def _docstring(sym):
391391
npdoc = parse_docstring(sym)
392392
return '\n\n'.join([npdoc['Summary'], npdoc['Extended']]).strip()
393393

394+
def _unwrap_sym(sym):
395+
if not hasattr(sym, '__signature__'): sym = getattr(sym, '__wrapped__', sym)
396+
return getattr(sym, 'fget', None) or getattr(sym, 'fset', None) or sym
397+
398+
def can_render(sym):
399+
"Check if `sym` has a renderable signature"
400+
sym = _unwrap_sym(sym)
401+
try: signature(sym, eval_str=True); return True
402+
except (ValueError, TypeError): return False
403+
394404
# %% ../nbs/04_docments.ipynb #9de89cb6
395405
def _fullname(o):
396406
module,name = getattr(o, "__module__", None),qual_name(o)
@@ -399,8 +409,7 @@ def _fullname(o):
399409
class ShowDocRenderer:
400410
def __init__(self, sym, name:str|None=None, title_level:int=3, maxline:int=110):
401411
"Show documentation for `sym`"
402-
if not hasattr(sym, '__signature__'): sym = getattr(sym, '__wrapped__', sym)
403-
sym = getattr(sym, 'fget', None) or getattr(sym, 'fset', None) or sym
412+
sym = _unwrap_sym(sym)
404413
store_attr()
405414
self.nm = name or qual_name(sym)
406415
self.isfunc = inspect.isfunction(sym)

nbs/04_docments.ipynb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,7 +2469,17 @@
24692469
"#| export\n",
24702470
"def _docstring(sym):\n",
24712471
" npdoc = parse_docstring(sym)\n",
2472-
" return '\\n\\n'.join([npdoc['Summary'], npdoc['Extended']]).strip()"
2472+
" return '\\n\\n'.join([npdoc['Summary'], npdoc['Extended']]).strip()\n",
2473+
"\n",
2474+
"def _unwrap_sym(sym):\n",
2475+
" if not hasattr(sym, '__signature__'): sym = getattr(sym, '__wrapped__', sym)\n",
2476+
" return getattr(sym, 'fget', None) or getattr(sym, 'fset', None) or sym\n",
2477+
"\n",
2478+
"def can_render(sym):\n",
2479+
" \"Check if `sym` has a renderable signature\"\n",
2480+
" sym = _unwrap_sym(sym)\n",
2481+
" try: signature(sym, eval_str=True); return True\n",
2482+
" except (ValueError, TypeError): return False"
24732483
]
24742484
},
24752485
{
@@ -2487,8 +2497,7 @@
24872497
"class ShowDocRenderer:\n",
24882498
" def __init__(self, sym, name:str|None=None, title_level:int=3, maxline:int=110):\n",
24892499
" \"Show documentation for `sym`\"\n",
2490-
" if not hasattr(sym, '__signature__'): sym = getattr(sym, '__wrapped__', sym)\n",
2491-
" sym = getattr(sym, 'fget', None) or getattr(sym, 'fset', None) or sym\n",
2500+
" sym = _unwrap_sym(sym)\n",
24922501
" store_attr()\n",
24932502
" self.nm = name or qual_name(sym)\n",
24942503
" self.isfunc = inspect.isfunction(sym)\n",

0 commit comments

Comments
 (0)