|
25 | 25 | "outputs": [],
|
26 | 26 | "source": [
|
27 | 27 | "#export\n",
|
| 28 | + "from __future__ import annotations\n", |
28 | 29 | "from tokenize import tokenize,COMMENT\n",
|
29 | 30 | "from ast import parse,FunctionDef\n",
|
30 | 31 | "from io import BytesIO\n",
|
|
151 | 152 | " return res"
|
152 | 153 | ]
|
153 | 154 | },
|
| 155 | + { |
| 156 | + "cell_type": "code", |
| 157 | + "execution_count": null, |
| 158 | + "metadata": {}, |
| 159 | + "outputs": [], |
| 160 | + "source": [ |
| 161 | + "#export\n", |
| 162 | + "empty = Parameter.empty" |
| 163 | + ] |
| 164 | + }, |
154 | 165 | {
|
155 | 166 | "cell_type": "code",
|
156 | 167 | "execution_count": null,
|
|
168 | 179 | " return dedent('\\n'.join(reversed(res))) if res else None\n",
|
169 | 180 | "\n",
|
170 | 181 | "def _get_full(anno, name, default, docs):\n",
|
171 |
| - " if anno==Parameter.empty and default!=Parameter.empty: anno = type(default)\n", |
| 182 | + " if anno==empty and default!=empty: anno = type(default)\n", |
172 | 183 | " return AttrDict(docment=docs.get(name), anno=anno, default=default)"
|
173 | 184 | ]
|
174 | 185 | },
|
|
185 | 196 | " comments = {o.start[0]:_clean_comment(o.string) for o in _tokens(s) if o.type==COMMENT}\n",
|
186 | 197 | " parms = _param_locs(s, returns=returns)\n",
|
187 | 198 | " docs = {arg:_get_comment(line, arg, comments, parms) for line,arg in parms.items()}\n",
|
188 |
| - " if not full: return docs\n", |
| 199 | + " if not full: return AttrDict(docs)\n", |
189 | 200 | "\n",
|
190 | 201 | " if isinstance(s,str): s = eval(s)\n",
|
191 | 202 | " sig = signature(s)\n",
|
192 | 203 | " res = {arg:_get_full(p.annotation, p.name, p.default, docs) for arg,p in sig.parameters.items()}\n",
|
193 |
| - " if returns: res['return'] = _get_full(sig.return_annotation, 'return', Parameter.empty, docs)\n", |
| 204 | + " if returns: res['return'] = _get_full(sig.return_annotation, 'return', empty, docs)\n", |
194 | 205 | " if eval_str:\n",
|
195 | 206 | " hints = type_hints(s)\n",
|
196 | 207 | " for k,v in res.items():\n",
|
197 | 208 | " if k in hints: v['anno'] = hints.get(k)\n",
|
198 |
| - " return res" |
| 209 | + " return AttrDict(res)" |
199 | 210 | ]
|
200 | 211 | },
|
201 | 212 | {
|
|
212 | 223 | "outputs": [
|
213 | 224 | {
|
214 | 225 | "data": {
|
| 226 | + "text/markdown": [ |
| 227 | + "```json\n", |
| 228 | + "{ 'a': 'the 1st number to add',\n", |
| 229 | + " 'b': 'the 2nd number to add',\n", |
| 230 | + " 'return': 'the result of adding `a` to `b`'}\n", |
| 231 | + "```" |
| 232 | + ], |
215 | 233 | "text/plain": [
|
216 | 234 | "{'a': 'the 1st number to add',\n",
|
217 | 235 | " 'b': 'the 2nd number to add',\n",
|
|
242 | 260 | {
|
243 | 261 | "data": {
|
244 | 262 | "text/plain": [
|
245 |
| - "{'a': int, 'return': int}" |
| 263 | + "{'a': 'int', 'return': 'int'}" |
246 | 264 | ]
|
247 | 265 | },
|
248 | 266 | "execution_count": null,
|
|
269 | 287 | {
|
270 | 288 | "data": {
|
271 | 289 | "text/markdown": [
|
272 |
| - "- docment: the 1st number to add\n", |
273 |
| - "- anno: <class 'int'>\n", |
274 |
| - "- default: <class 'inspect._empty'>" |
| 290 | + "```json\n", |
| 291 | + "{ 'anno': 'int',\n", |
| 292 | + " 'default': <class 'inspect._empty'>,\n", |
| 293 | + " 'docment': 'the 1st number to add'}\n", |
| 294 | + "```" |
275 | 295 | ],
|
276 | 296 | "text/plain": [
|
277 |
| - "- docment: the 1st number to add\n", |
278 |
| - "- anno: <class 'int'>\n", |
279 |
| - "- default: <class 'inspect._empty'>" |
| 297 | + "{'docment': 'the 1st number to add', 'anno': 'int', 'default': inspect._empty}" |
280 | 298 | ]
|
281 | 299 | },
|
282 | 300 | "execution_count": null,
|
|
303 | 321 | {
|
304 | 322 | "data": {
|
305 | 323 | "text/markdown": [
|
306 |
| - "- docment: the 1st number to add\n", |
307 |
| - "- anno: <class 'int'>\n", |
308 |
| - "- default: <class 'inspect._empty'>" |
| 324 | + "```json\n", |
| 325 | + "{ 'anno': <class 'int'>,\n", |
| 326 | + " 'default': <class 'inspect._empty'>,\n", |
| 327 | + " 'docment': 'the 1st number to add'}\n", |
| 328 | + "```" |
309 | 329 | ],
|
310 | 330 | "text/plain": [
|
311 |
| - "- docment: the 1st number to add\n", |
312 |
| - "- anno: <class 'int'>\n", |
313 |
| - "- default: <class 'inspect._empty'>" |
| 331 | + "{'docment': 'the 1st number to add', 'anno': int, 'default': inspect._empty}" |
314 | 332 | ]
|
315 | 333 | },
|
316 | 334 | "execution_count": null,
|
|
353 | 371 | "outputs": [
|
354 | 372 | {
|
355 | 373 | "data": {
|
| 374 | + "text/markdown": [ |
| 375 | + "```json\n", |
| 376 | + "{ 'a': 'The first operand',\n", |
| 377 | + " 'b': 'This is the second of the operands to the *addition* operator.\\n'\n", |
| 378 | + " 'Note that passing a negative value here is the equivalent of the '\n", |
| 379 | + " '*subtraction* operator.',\n", |
| 380 | + " 'return': \"The result is calculated using Python's builtin `+` operator.\"}\n", |
| 381 | + "```" |
| 382 | + ], |
356 | 383 | "text/plain": [
|
357 | 384 | "{'a': 'The first operand',\n",
|
358 | 385 | " 'b': 'This is the second of the operands to the *addition* operator.\\nNote that passing a negative value here is the equivalent of the *subtraction* operator.',\n",
|
|
401 | 428 | "outputs": [
|
402 | 429 | {
|
403 | 430 | "data": {
|
| 431 | + "text/markdown": [ |
| 432 | + "```json\n", |
| 433 | + "{'a': 'First operand', 'b': '2nd operand', 'self': None}\n", |
| 434 | + "```" |
| 435 | + ], |
| 436 | + "text/plain": [ |
| 437 | + "{'self': None, 'a': 'First operand', 'b': '2nd operand'}" |
| 438 | + ] |
| 439 | + }, |
| 440 | + "execution_count": null, |
| 441 | + "metadata": {}, |
| 442 | + "output_type": "execute_result" |
| 443 | + } |
| 444 | + ], |
| 445 | + "source": [ |
| 446 | + "docments(Adder)" |
| 447 | + ] |
| 448 | + }, |
| 449 | + { |
| 450 | + "cell_type": "code", |
| 451 | + "execution_count": null, |
| 452 | + "metadata": {}, |
| 453 | + "outputs": [ |
| 454 | + { |
| 455 | + "data": { |
| 456 | + "text/markdown": [ |
| 457 | + "```json\n", |
| 458 | + "{'return': 'Integral result of addition operator', 'self': None}\n", |
| 459 | + "```" |
| 460 | + ], |
404 | 461 | "text/plain": [
|
405 |
| - "({'self': None, 'a': 'First operand', 'b': '2nd operand'},\n", |
406 |
| - " {'self': None, 'return': 'Integral result of addition operator'})" |
| 462 | + "{'self': None, 'return': 'Integral result of addition operator'}" |
407 | 463 | ]
|
408 | 464 | },
|
409 | 465 | "execution_count": null,
|
|
412 | 468 | }
|
413 | 469 | ],
|
414 | 470 | "source": [
|
415 |
| - "docments(Adder),docments(Adder.calculate)" |
| 471 | + "docments(Adder.calculate)" |
416 | 472 | ]
|
417 | 473 | },
|
418 | 474 | {
|
|
0 commit comments