|
2111 | 2111 | " return get_name(obj) + sig.replace('<unrepresentable>', '...')" |
2112 | 2112 | ] |
2113 | 2113 | }, |
| 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 | + }, |
2114 | 2158 | { |
2115 | 2159 | "cell_type": "code", |
2116 | 2160 | "execution_count": null, |
|
2119 | 2163 | "outputs": [], |
2120 | 2164 | "source": [ |
2121 | 2165 | "#| 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", |
2122 | 2170 | "class DocmentText(_DocmentBase):\n", |
2123 | 2171 | " def __init__(self, obj, maxline=110, docstring=True):\n", |
2124 | 2172 | " super().__init__(obj)\n", |
2125 | 2173 | " self.maxline,self.docstring = maxline,docstring\n", |
2126 | 2174 | "\n", |
2127 | 2175 | " def _fmt_param(self, nm, p):\n", |
2128 | 2176 | " 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", |
2130 | 2178 | " \n", |
2131 | 2179 | " @property\n", |
2132 | 2180 | " def _ret_str(self):\n", |
|
2140 | 2188 | "\n", |
2141 | 2189 | " def __str__(self):\n", |
2142 | 2190 | " 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", |
2155 | 2192 | " docstr = f' \"{self.obj.__doc__}\"' if self.docstring and self.obj.__doc__ else ''\n", |
2156 | 2193 | " return f\"{sig_str}\\n{docstr}\"\n", |
2157 | 2194 | " \n", |
|
2462 | 2499 | "```python\n", |
2463 | 2500 | "\n", |
2464 | 2501 | "def _f(\n", |
2465 | | - " a, b:int, # param b\n", |
| 2502 | + " a, b:callable=print, # param b\n", |
2466 | 2503 | " c:str='foo', # param c\n", |
2467 | 2504 | ")->str: # Result of doing it\n", |
2468 | 2505 | "\n", |
|
2475 | 2512 | "```python\n", |
2476 | 2513 | "\n", |
2477 | 2514 | "def _f(\n", |
2478 | | - " a, b:int, # param b\n", |
| 2515 | + " a, b:callable=print, # param b\n", |
2479 | 2516 | " c:str='foo', # param c\n", |
2480 | 2517 | ")->str: # Result of doing it\n", |
2481 | 2518 | "\n", |
|
2492 | 2529 | ], |
2493 | 2530 | "source": [ |
2494 | 2531 | "def _f(a,\n", |
2495 | | - " b:int, #param b\n", |
| 2532 | + " b:callable=print, #param b\n", |
2496 | 2533 | " c:str='foo' #param c\n", |
2497 | 2534 | " )->str: # Result of doing it\n", |
2498 | 2535 | " \"Do a thing\"\n", |
|
0 commit comments