Skip to content

Commit 1d60a29

Browse files
committed
fixes #671
1 parent ed195ee commit 1d60a29

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

fastcore/docments.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,18 @@ def get_dataclass_source(s):
4848
# %% ../nbs/06_docments.ipynb
4949
def get_source(s):
5050
"Get source code for string, function object or dataclass `s`"
51-
return getsource(s) if isfunction(s) or ismethod(s) else get_dataclass_source(s) if isdataclass(s) else s
51+
if isinstance(s,str): return s
52+
return getsource(s) if isfunction(s) or ismethod(s) else get_dataclass_source(s) if isdataclass(s) else None
5253

5354
# %% ../nbs/06_docments.ipynb
5455
def _parses(s):
5556
"Parse Python code in string, function object or dataclass `s`"
56-
return parse(dedent(get_source(s)))
57+
return parse(dedent(get_source(s) or ''))
5758

5859
def _tokens(s):
5960
"Tokenize Python code in string or function object `s`"
6061
s = get_source(s)
62+
if not s: return []
6163
return tokenize(BytesIO(s.encode('utf-8')).readline)
6264

6365
_clean_re = re.compile(r'^\s*#(.*)\s*$')

nbs/06_docments.ipynb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@
213213
"#|export\n",
214214
"def get_source(s):\n",
215215
" \"Get source code for string, function object or dataclass `s`\"\n",
216-
" return getsource(s) if isfunction(s) or ismethod(s) else get_dataclass_source(s) if isdataclass(s) else s"
216+
" if isinstance(s,str): return s\n",
217+
" return getsource(s) if isfunction(s) or ismethod(s) else get_dataclass_source(s) if isdataclass(s) else None"
217218
]
218219
},
219220
{
@@ -225,11 +226,12 @@
225226
"#|export\n",
226227
"def _parses(s):\n",
227228
" \"Parse Python code in string, function object or dataclass `s`\"\n",
228-
" return parse(dedent(get_source(s)))\n",
229+
" return parse(dedent(get_source(s) or ''))\n",
229230
"\n",
230231
"def _tokens(s):\n",
231232
" \"Tokenize Python code in string or function object `s`\"\n",
232233
" s = get_source(s)\n",
234+
" if not s: return []\n",
233235
" return tokenize(BytesIO(s.encode('utf-8')).readline)\n",
234236
"\n",
235237
"_clean_re = re.compile(r'^\\s*#(.*)\\s*$')\n",

0 commit comments

Comments
 (0)