|
119 | 119 | " return renderer(sym or show_doc, disp=disp)" |
120 | 120 | ] |
121 | 121 | }, |
| 122 | + { |
| 123 | + "cell_type": "markdown", |
| 124 | + "id": "a38843d9-4fa3-40f3-bb4d-949344b2bdee", |
| 125 | + "metadata": {}, |
| 126 | + "source": [ |
| 127 | + "You can use `show_doc` to document apis of functions, classes or methods:" |
| 128 | + ] |
| 129 | + }, |
122 | 130 | { |
123 | 131 | "cell_type": "code", |
124 | 132 | "execution_count": null, |
|
137 | 145 | "func docstring" |
138 | 146 | ], |
139 | 147 | "text/plain": [ |
140 | | - "<__main__.BasicMarkdownRenderer at 0x7f9bb840bee0>" |
| 148 | + "<__main__.BasicMarkdownRenderer at 0x7f9c5abb8340>" |
141 | 149 | ] |
142 | 150 | }, |
143 | 151 | "execution_count": null, |
|
153 | 161 | "show_doc(f)" |
154 | 162 | ] |
155 | 163 | }, |
| 164 | + { |
| 165 | + "cell_type": "code", |
| 166 | + "execution_count": null, |
| 167 | + "id": "1c28ec9d-29ff-497f-97b0-ca262f530cf6", |
| 168 | + "metadata": {}, |
| 169 | + "outputs": [ |
| 170 | + { |
| 171 | + "data": { |
| 172 | + "text/markdown": [ |
| 173 | + "---\n", |
| 174 | + "\n", |
| 175 | + "### Foo\n", |
| 176 | + "\n", |
| 177 | + "> **`Foo`**` (e: int)`\n", |
| 178 | + "\n", |
| 179 | + "This is the docstring for the __init__ method" |
| 180 | + ], |
| 181 | + "text/plain": [ |
| 182 | + "<__main__.BasicMarkdownRenderer at 0x7f9c48502880>" |
| 183 | + ] |
| 184 | + }, |
| 185 | + "execution_count": null, |
| 186 | + "metadata": {}, |
| 187 | + "output_type": "execute_result" |
| 188 | + } |
| 189 | + ], |
| 190 | + "source": [ |
| 191 | + "class Foo:\n", |
| 192 | + " def __init__(d:str,e:int):\n", |
| 193 | + " \"This is the docstring for the __init__ method\"\n", |
| 194 | + " ...\n", |
| 195 | + "\n", |
| 196 | + "show_doc(Foo)" |
| 197 | + ] |
| 198 | + }, |
| 199 | + { |
| 200 | + "cell_type": "code", |
| 201 | + "execution_count": null, |
| 202 | + "id": "3441e6c7-472b-411c-a179-b1e37dcbceac", |
| 203 | + "metadata": {}, |
| 204 | + "outputs": [ |
| 205 | + { |
| 206 | + "data": { |
| 207 | + "text/markdown": [ |
| 208 | + "---\n", |
| 209 | + "\n", |
| 210 | + "#### Foo.a_method\n", |
| 211 | + "\n", |
| 212 | + "> **`Foo.a_method`**` (a: list, b: dict, c)`\n", |
| 213 | + "\n", |
| 214 | + "This is a method" |
| 215 | + ], |
| 216 | + "text/plain": [ |
| 217 | + "<__main__.BasicMarkdownRenderer at 0x7f9c48502be0>" |
| 218 | + ] |
| 219 | + }, |
| 220 | + "execution_count": null, |
| 221 | + "metadata": {}, |
| 222 | + "output_type": "execute_result" |
| 223 | + } |
| 224 | + ], |
| 225 | + "source": [ |
| 226 | + "class Foo:\n", |
| 227 | + " def a_method(a:list,b:dict,c):\n", |
| 228 | + " \"This is a method\"\n", |
| 229 | + " ...\n", |
| 230 | + "\n", |
| 231 | + "show_doc(Foo.a_method)" |
| 232 | + ] |
| 233 | + }, |
156 | 234 | { |
157 | 235 | "cell_type": "code", |
158 | 236 | "execution_count": null, |
|
184 | 262 | "<blockquote><code>F(x: int = 1)</code></blockquote><p>class docstring</p>" |
185 | 263 | ], |
186 | 264 | "text/plain": [ |
187 | | - "<__main__.BasicHtmlRenderer at 0x7f9bb83fdd90>" |
| 265 | + "<__main__.BasicHtmlRenderer at 0x7f9c59b4d5b0>" |
188 | 266 | ] |
189 | 267 | }, |
190 | 268 | "execution_count": null, |
|
200 | 278 | "show_doc(F, renderer=BasicHtmlRenderer)" |
201 | 279 | ] |
202 | 280 | }, |
| 281 | + { |
| 282 | + "cell_type": "code", |
| 283 | + "execution_count": null, |
| 284 | + "id": "12c4cf13-56fa-4b47-a62e-7917ab61fa7c", |
| 285 | + "metadata": {}, |
| 286 | + "outputs": [], |
| 287 | + "source": [ |
| 288 | + "#|export\n", |
| 289 | + "def _is_patch(tree):\n", |
| 290 | + " decs = [d.id for d in getattr(tree, 'decorator_list', []) if hasattr(d, 'id')]\n", |
| 291 | + " return 'patch' in decs" |
| 292 | + ] |
| 293 | + }, |
| 294 | + { |
| 295 | + "cell_type": "code", |
| 296 | + "execution_count": null, |
| 297 | + "id": "35043aa7-6b60-4ddf-8949-39a78577f23d", |
| 298 | + "metadata": {}, |
| 299 | + "outputs": [], |
| 300 | + "source": [ |
| 301 | + "#|export\n", |
| 302 | + "def get_patch_name(tree):\n", |
| 303 | + " \"If a method is defined with @patch, get fully qualified name.\"\n", |
| 304 | + " first_arg = first(L(nested_attr(tree, 'args.args')))\n", |
| 305 | + " if _is_patch(tree) and first_arg.arg == 'self':\n", |
| 306 | + " annotation = nested_attr(first_arg, 'annotation.id')\n", |
| 307 | + " if annotation: return f'{annotation}.{tree.name}'\n", |
| 308 | + " else: return tree.name\n", |
| 309 | + " else: return tree.name" |
| 310 | + ] |
| 311 | + }, |
| 312 | + { |
| 313 | + "cell_type": "code", |
| 314 | + "execution_count": null, |
| 315 | + "id": "fe22731d-ad7d-4080-ac91-e0d24e8b681c", |
| 316 | + "metadata": {}, |
| 317 | + "outputs": [], |
| 318 | + "source": [ |
| 319 | + "#|hide\n", |
| 320 | + "import ast\n", |
| 321 | + "code=\"\"\"\n", |
| 322 | + "@bar\n", |
| 323 | + "@patch\n", |
| 324 | + "@foo\n", |
| 325 | + "def a_method(self:Foo, a:list,b:dict,c):\n", |
| 326 | + " \"This is a method\"\n", |
| 327 | + " ...\n", |
| 328 | + "\"\"\"\n", |
| 329 | + "\n", |
| 330 | + "code2=\"\"\"\n", |
| 331 | + "@bar\n", |
| 332 | + "@foo\n", |
| 333 | + "def a_method(self:Foo, a:list,b:dict,c):\n", |
| 334 | + " \"This is a method\"\n", |
| 335 | + " ...\n", |
| 336 | + "\"\"\"\n", |
| 337 | + "\n", |
| 338 | + "_tree = ast.parse(code).body[0]\n", |
| 339 | + "assert _is_patch(_tree)\n", |
| 340 | + "test_eq(get_patch_name(_tree), 'Foo.a_method')\n", |
| 341 | + "\n", |
| 342 | + "_tree2 = ast.parse(code2).body[0]\n", |
| 343 | + "test_eq(get_patch_name(_tree2), 'a_method')" |
| 344 | + ] |
| 345 | + }, |
203 | 346 | { |
204 | 347 | "cell_type": "markdown", |
205 | 348 | "id": "ec7a2f01", |
|
0 commit comments