Skip to content

Commit 021ee5a

Browse files
committed
fixes #756
1 parent a61b630 commit 021ee5a

3 files changed

Lines changed: 76 additions & 29 deletions

File tree

fastcore/_modidx.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@
288288
'fastcore.docments._escape_markdown': ('docments.html#_escape_markdown', 'fastcore/docments.py'),
289289
'fastcore.docments._f_name': ('docments.html#_f_name', 'fastcore/docments.py'),
290290
'fastcore.docments._fmt_anno': ('docments.html#_fmt_anno', 'fastcore/docments.py'),
291+
'fastcore.docments._fmt_default': ('docments.html#_fmt_default', 'fastcore/docments.py'),
292+
'fastcore.docments._fmt_sig': ('docments.html#_fmt_sig', 'fastcore/docments.py'),
291293
'fastcore.docments._fullname': ('docments.html#_fullname', 'fastcore/docments.py'),
292294
'fastcore.docments._get_comment': ('docments.html#_get_comment', 'fastcore/docments.py'),
293295
'fastcore.docments._get_full': ('docments.html#_get_full', 'fastcore/docments.py'),

fastcore/docments.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -324,15 +324,34 @@ def _clean_text_sig(obj):
324324
sig = re.sub(r'\$\w+,?\s*', '', sig)
325325
return get_name(obj) + sig.replace('<unrepresentable>', '...')
326326

327+
# %% ../nbs/04_docments.ipynb #cfcc46bf
328+
def _fmt_sig(name, params, ret_str, maxline):
329+
"Format function signature with params and docment comments"
330+
lines,curr = [],[]
331+
for fmt,doc in params:
332+
comment = f' # {doc}' if doc else ''
333+
if curr and len(', '.join(curr))+len(fmt)+len(comment)>maxline:
334+
lines.append(', '.join(curr) + ',')
335+
curr = []
336+
curr.append(fmt)
337+
if doc: lines.append(', '.join(curr) + ',' + comment); curr = []
338+
if curr: lines.append(', '.join(curr))
339+
pstr = '\n '.join(lines)
340+
return f"def {name}(\n {pstr}\n{ret_str}"
341+
327342
# %% ../nbs/04_docments.ipynb #4550820f
343+
def _fmt_default(o):
344+
if o is empty: return ''
345+
return o.__name__ if hasattr(o, '__name__') else repr(o)
346+
328347
class DocmentText(_DocmentBase):
329348
def __init__(self, obj, maxline=110, docstring=True):
330349
super().__init__(obj)
331350
self.maxline,self.docstring = maxline,docstring
332351

333352
def _fmt_param(self, nm, p):
334353
anno,default = p.get('anno',empty), p.get('default',empty)
335-
return nm + (f':{_maybe_nm(anno)}' if anno != empty else '') + (f'={repr(default)}' if default != empty else '')
354+
return nm + (f':{_maybe_nm(anno)}' if anno != empty else '') + (f'={_fmt_default(default)}' if default != empty else '')
336355

337356
@property
338357
def _ret_str(self):
@@ -346,18 +365,7 @@ def params(self): return [(self._fmt_param(k,v), v.get('docment','')) for k,v in
346365

347366
def __str__(self):
348367
if (sig := _clean_text_sig(self.obj)) and not self.params: sig_str = f"def {sig}"
349-
else:
350-
lines,curr = [],[]
351-
for fmt,doc in self.params:
352-
comment = f' # {doc}' if doc else ''
353-
if curr and len(', '.join(curr))+len(fmt)+len(comment)>self.maxline:
354-
lines.append(', '.join(curr) + ',')
355-
curr = []
356-
curr.append(fmt)
357-
if doc: lines.append(', '.join(curr) + ',' + comment); curr = []
358-
if curr: lines.append(', '.join(curr))
359-
params = '\n '.join(lines)
360-
sig_str = f"def {get_name(self.obj)}(\n {params}\n{self._ret_str}"
368+
else: sig_str = _fmt_sig(get_name(self.obj), self.params, self._ret_str, self.maxline)
361369
docstr = f' "{self.obj.__doc__}"' if self.docstring and self.obj.__doc__ else ''
362370
return f"{sig_str}\n{docstr}"
363371

nbs/04_docments.ipynb

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,6 +2111,50 @@
21112111
" return get_name(obj) + sig.replace('<unrepresentable>', '...')"
21122112
]
21132113
},
2114+
{
2115+
"cell_type": "code",
2116+
"execution_count": null,
2117+
"id": "cfcc46bf",
2118+
"metadata": {},
2119+
"outputs": [],
2120+
"source": [
2121+
"#| export\n",
2122+
"def _fmt_sig(name, params, ret_str, maxline):\n",
2123+
" \"Format function signature with params and docment comments\"\n",
2124+
" lines,curr = [],[]\n",
2125+
" for fmt,doc in params:\n",
2126+
" comment = f' # {doc}' if doc else ''\n",
2127+
" if curr and len(', '.join(curr))+len(fmt)+len(comment)>maxline:\n",
2128+
" lines.append(', '.join(curr) + ',')\n",
2129+
" curr = []\n",
2130+
" curr.append(fmt)\n",
2131+
" if doc: lines.append(', '.join(curr) + ',' + comment); curr = []\n",
2132+
" if curr: lines.append(', '.join(curr))\n",
2133+
" pstr = '\\n '.join(lines)\n",
2134+
" return f\"def {name}(\\n {pstr}\\n{ret_str}\""
2135+
]
2136+
},
2137+
{
2138+
"cell_type": "code",
2139+
"execution_count": null,
2140+
"id": "fc88e424",
2141+
"metadata": {},
2142+
"outputs": [
2143+
{
2144+
"name": "stdout",
2145+
"output_type": "stream",
2146+
"text": [
2147+
"def foo(\n",
2148+
" a:int, # first\n",
2149+
" b:str, c, # third\n",
2150+
")->int:\n"
2151+
]
2152+
}
2153+
],
2154+
"source": [
2155+
"print(_fmt_sig('foo', [('a:int', 'first'), ('b:str', None), ('c', 'third')], ')->int:', 80))"
2156+
]
2157+
},
21142158
{
21152159
"cell_type": "code",
21162160
"execution_count": null,
@@ -2119,14 +2163,18 @@
21192163
"outputs": [],
21202164
"source": [
21212165
"#| export\n",
2166+
"def _fmt_default(o):\n",
2167+
" if o is empty: return ''\n",
2168+
" return o.__name__ if hasattr(o, '__name__') else repr(o)\n",
2169+
"\n",
21222170
"class DocmentText(_DocmentBase):\n",
21232171
" def __init__(self, obj, maxline=110, docstring=True):\n",
21242172
" super().__init__(obj)\n",
21252173
" self.maxline,self.docstring = maxline,docstring\n",
21262174
"\n",
21272175
" def _fmt_param(self, nm, p):\n",
21282176
" anno,default = p.get('anno',empty), p.get('default',empty)\n",
2129-
" return nm + (f':{_maybe_nm(anno)}' if anno != empty else '') + (f'={repr(default)}' if default != empty else '')\n",
2177+
" return nm + (f':{_maybe_nm(anno)}' if anno != empty else '') + (f'={_fmt_default(default)}' if default != empty else '')\n",
21302178
" \n",
21312179
" @property\n",
21322180
" def _ret_str(self):\n",
@@ -2140,18 +2188,7 @@
21402188
"\n",
21412189
" def __str__(self):\n",
21422190
" if (sig := _clean_text_sig(self.obj)) and not self.params: sig_str = f\"def {sig}\"\n",
2143-
" else:\n",
2144-
" lines,curr = [],[]\n",
2145-
" for fmt,doc in self.params:\n",
2146-
" comment = f' # {doc}' if doc else ''\n",
2147-
" if curr and len(', '.join(curr))+len(fmt)+len(comment)>self.maxline:\n",
2148-
" lines.append(', '.join(curr) + ',')\n",
2149-
" curr = []\n",
2150-
" curr.append(fmt)\n",
2151-
" if doc: lines.append(', '.join(curr) + ',' + comment); curr = []\n",
2152-
" if curr: lines.append(', '.join(curr))\n",
2153-
" params = '\\n '.join(lines)\n",
2154-
" sig_str = f\"def {get_name(self.obj)}(\\n {params}\\n{self._ret_str}\"\n",
2191+
" else: sig_str = _fmt_sig(get_name(self.obj), self.params, self._ret_str, self.maxline)\n",
21552192
" docstr = f' \"{self.obj.__doc__}\"' if self.docstring and self.obj.__doc__ else ''\n",
21562193
" return f\"{sig_str}\\n{docstr}\"\n",
21572194
" \n",
@@ -2462,7 +2499,7 @@
24622499
"```python\n",
24632500
"\n",
24642501
"def _f(\n",
2465-
" a, b:int, # param b\n",
2502+
" a, b:callable=print, # param b\n",
24662503
" c:str='foo', # param c\n",
24672504
")->str: # Result of doing it\n",
24682505
"\n",
@@ -2475,7 +2512,7 @@
24752512
"```python\n",
24762513
"\n",
24772514
"def _f(\n",
2478-
" a, b:int, # param b\n",
2515+
" a, b:callable=print, # param b\n",
24792516
" c:str='foo', # param c\n",
24802517
")->str: # Result of doing it\n",
24812518
"\n",
@@ -2492,7 +2529,7 @@
24922529
],
24932530
"source": [
24942531
"def _f(a,\n",
2495-
" b:int, #param b\n",
2532+
" b:callable=print, #param b\n",
24962533
" c:str='foo' #param c\n",
24972534
" )->str: # Result of doing it\n",
24982535
" \"Do a thing\"\n",

0 commit comments

Comments
 (0)