Skip to content

Commit 60544b2

Browse files
committed
fixes #750
1 parent 6609767 commit 60544b2

File tree

3 files changed

+94
-25
lines changed

3 files changed

+94
-25
lines changed

fastcore/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@
283283
'fastcore/docments.py'),
284284
'fastcore.docments._bold': ('docments.html#_bold', 'fastcore/docments.py'),
285285
'fastcore.docments._clean_comment': ('docments.html#_clean_comment', 'fastcore/docments.py'),
286+
'fastcore.docments._clean_text_sig': ('docments.html#_clean_text_sig', 'fastcore/docments.py'),
286287
'fastcore.docments._docstring': ('docments.html#_docstring', 'fastcore/docments.py'),
287288
'fastcore.docments._escape_markdown': ('docments.html#_escape_markdown', 'fastcore/docments.py'),
288289
'fastcore.docments._f_name': ('docments.html#_f_name', 'fastcore/docments.py'),

fastcore/docments.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,12 @@ def _fmt(self, nm, p):
318318
def _repr_markdown_(self): return '\n'.join(self._fmt(k,v) for k,v in self.dm.items())
319319
__repr__=__str__=_repr_markdown_
320320

321+
# %% ../nbs/04_docments.ipynb #6355b569
322+
def _clean_text_sig(obj):
323+
if not (sig := getattr(obj, '__text_signature__', None)): return None
324+
sig = re.sub(r'\$\w+,?\s*', '', sig)
325+
return get_name(obj) + sig.replace('<unrepresentable>', '...')
326+
321327
# %% ../nbs/04_docments.ipynb #4550820f
322328
class DocmentText(_DocmentBase):
323329
def __init__(self, obj, maxline=110, docstring=True):
@@ -339,18 +345,20 @@ def _ret_str(self):
339345
def params(self): return [(self._fmt_param(k,v), v.get('docment','')) for k,v in self.dm.items() if k != 'return']
340346

341347
def __str__(self):
342-
lines,curr = [],[]
343-
for fmt,doc in self.params:
344-
comment = f' # {doc}' if doc else ''
345-
if curr and len(', '.join(curr))+len(fmt)+len(comment)>self.maxline:
346-
lines.append(', '.join(curr) + ',')
347-
curr = []
348-
curr.append(fmt)
349-
if doc: lines.append(', '.join(curr) + ',' + comment); curr = []
350-
if curr: lines.append(', '.join(curr))
351-
body = '\n '.join(lines)
348+
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+
sig_str = f"def {get_name(self.obj)}(\n {'\n '.join(lines)}\n{self._ret_str}"
352360
docstr = f' "{self.obj.__doc__}"' if self.docstring and self.obj.__doc__ else ''
353-
return f"def {get_name(self.obj)}(\n {body}\n{self._ret_str}\n{docstr}"
361+
return f"{sig_str}\n{docstr}"
354362

355363
__repr__ = __str__
356364
def _repr_markdown_(self): return f"```python\n{self}\n```"

nbs/04_docments.ipynb

Lines changed: 74 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,7 +1847,7 @@
18471847
"| **Returns** | **int** | | |"
18481848
],
18491849
"text/plain": [
1850-
"DocmentTbl(obj=<function _f at 0x135058540>, dm={'a': {'docment': 'description of param a', 'anno': <class 'inspect._empty'>, 'default': <class 'inspect._empty'>}, 'b': {'docment': 'description of param b', 'anno': <class 'bool'>, 'default': True}, 'c': {'docment': <class 'inspect._empty'>, 'anno': <class 'str'>, 'default': None}, 'return': {'docment': <class 'inspect._empty'>, 'anno': <class 'int'>, 'default': <class 'inspect._empty'>}}, verbose=True, returns=True, params=['a', 'b', 'c'])"
1850+
"DocmentTbl(obj=<function _f at 0x135fa8c20>, dm={'a': {'docment': 'description of param a', 'anno': <class 'inspect._empty'>, 'default': <class 'inspect._empty'>}, 'b': {'docment': 'description of param b', 'anno': <class 'bool'>, 'default': True}, 'c': {'docment': <class 'inspect._empty'>, 'anno': <class 'str'>, 'default': None}, 'return': {'docment': <class 'inspect._empty'>, 'anno': <class 'int'>, 'default': <class 'inspect._empty'>}}, verbose=True, returns=True, params=['a', 'b', 'c'])"
18511851
]
18521852
},
18531853
"execution_count": null,
@@ -1925,7 +1925,7 @@
19251925
"| **Returns** | **str** | | **Result of doing it** |"
19261926
],
19271927
"text/plain": [
1928-
"DocmentTbl(obj=<function _f at 0x134ed3a60>, dm={'a': {'docment': <class 'inspect._empty'>, 'anno': <class 'inspect._empty'>, 'default': <class 'inspect._empty'>}, 'b': {'docment': 'param b', 'anno': <class 'int'>, 'default': <class 'inspect._empty'>}, 'c': {'docment': 'param c', 'anno': <class 'str'>, 'default': 'foo'}, 'return': {'docment': 'Result of doing it', 'anno': <class 'str'>, 'default': <class 'inspect._empty'>}}, verbose=True, returns=True, params=['a', 'b', 'c'])"
1928+
"DocmentTbl(obj=<function _f at 0x136032840>, dm={'a': {'docment': <class 'inspect._empty'>, 'anno': <class 'inspect._empty'>, 'default': <class 'inspect._empty'>}, 'b': {'docment': 'param b', 'anno': <class 'int'>, 'default': <class 'inspect._empty'>}, 'c': {'docment': 'param c', 'anno': <class 'str'>, 'default': 'foo'}, 'return': {'docment': 'Result of doing it', 'anno': <class 'str'>, 'default': <class 'inspect._empty'>}}, verbose=True, returns=True, params=['a', 'b', 'c'])"
19291929
]
19301930
},
19311931
"execution_count": null,
@@ -2018,7 +2018,7 @@
20182018
"| d | bool | True | description of param d |"
20192019
],
20202020
"text/plain": [
2021-
"DocmentTbl(obj=<function _Test.foo at 0x13517e8e0>, dm={'c': {'docment': 'description of param c', 'anno': <class 'int'>, 'default': <class 'inspect._empty'>}, 'd': {'docment': 'description of param d', 'anno': <class 'bool'>, 'default': True}, 'return': {'docment': <class 'inspect._empty'>, 'anno': <class 'inspect._empty'>, 'default': <class 'inspect._empty'>}}, verbose=True, returns=True, params=['self', 'c', 'd'])"
2021+
"DocmentTbl(obj=<function _Test.foo at 0x135ffdbc0>, dm={'c': {'docment': 'description of param c', 'anno': <class 'int'>, 'default': <class 'inspect._empty'>}, 'd': {'docment': 'description of param d', 'anno': <class 'bool'>, 'default': True}, 'return': {'docment': <class 'inspect._empty'>, 'anno': <class 'inspect._empty'>, 'default': <class 'inspect._empty'>}}, verbose=True, returns=True, params=['self', 'c', 'd'])"
20222022
]
20232023
},
20242024
"execution_count": null,
@@ -2097,6 +2097,20 @@
20972097
"DocmentList(_f)"
20982098
]
20992099
},
2100+
{
2101+
"cell_type": "code",
2102+
"execution_count": null,
2103+
"id": "6355b569",
2104+
"metadata": {},
2105+
"outputs": [],
2106+
"source": [
2107+
"#| export\n",
2108+
"def _clean_text_sig(obj):\n",
2109+
" if not (sig := getattr(obj, '__text_signature__', None)): return None\n",
2110+
" sig = re.sub(r'\\$\\w+,?\\s*', '', sig)\n",
2111+
" return get_name(obj) + sig.replace('<unrepresentable>', '...')"
2112+
]
2113+
},
21002114
{
21012115
"cell_type": "code",
21022116
"execution_count": null,
@@ -2125,18 +2139,20 @@
21252139
" def params(self): return [(self._fmt_param(k,v), v.get('docment','')) for k,v in self.dm.items() if k != 'return']\n",
21262140
"\n",
21272141
" def __str__(self):\n",
2128-
" lines,curr = [],[]\n",
2129-
" for fmt,doc in self.params:\n",
2130-
" comment = f' # {doc}' if doc else ''\n",
2131-
" if curr and len(', '.join(curr))+len(fmt)+len(comment)>self.maxline:\n",
2132-
" lines.append(', '.join(curr) + ',')\n",
2133-
" curr = []\n",
2134-
" curr.append(fmt)\n",
2135-
" if doc: lines.append(', '.join(curr) + ',' + comment); curr = []\n",
2136-
" if curr: lines.append(', '.join(curr))\n",
2137-
" body = '\\n '.join(lines)\n",
2142+
" 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+
" sig_str = f\"def {get_name(self.obj)}(\\n {'\\n '.join(lines)}\\n{self._ret_str}\"\n",
21382154
" docstr = f' \"{self.obj.__doc__}\"' if self.docstring and self.obj.__doc__ else ''\n",
2139-
" return f\"def {get_name(self.obj)}(\\n {body}\\n{self._ret_str}\\n{docstr}\"\n",
2155+
" return f\"{sig_str}\\n{docstr}\"\n",
21402156
" \n",
21412157
" __repr__ = __str__\n",
21422158
" def _repr_markdown_(self): return f\"```python\\n{self}\\n```\""
@@ -2541,6 +2557,50 @@
25412557
"MarkdownRenderer(g)"
25422558
]
25432559
},
2560+
{
2561+
"cell_type": "code",
2562+
"execution_count": null,
2563+
"id": "7114d616",
2564+
"metadata": {},
2565+
"outputs": [
2566+
{
2567+
"data": {
2568+
"text/markdown": [
2569+
"```python\n",
2570+
"\n",
2571+
"def next(iterator, default=..., /)\n",
2572+
"\n",
2573+
"\n",
2574+
"```\n",
2575+
"\n",
2576+
"*Return the next item from the iterator.*\n",
2577+
"\n",
2578+
"If default is given and the iterator is exhausted,\n",
2579+
"it is returned instead of raising StopIteration."
2580+
],
2581+
"text/plain": [
2582+
"```python\n",
2583+
"\n",
2584+
"def next(iterator, default=..., /)\n",
2585+
"\n",
2586+
"\n",
2587+
"```\n",
2588+
"\n",
2589+
"*Return the next item from the iterator.*\n",
2590+
"\n",
2591+
"If default is given and the iterator is exhausted,\n",
2592+
"it is returned instead of raising StopIteration."
2593+
]
2594+
},
2595+
"execution_count": null,
2596+
"metadata": {},
2597+
"output_type": "execute_result"
2598+
}
2599+
],
2600+
"source": [
2601+
"MarkdownRenderer(next)"
2602+
]
2603+
},
25442604
{
25452605
"cell_type": "markdown",
25462606
"id": "51a04f95",

0 commit comments

Comments
 (0)