|
27 | 27 | "#|export\n", |
28 | 28 | "from __future__ import annotations\n", |
29 | 29 | "\n", |
30 | | - "import re,ast\n", |
| 30 | + "import re,ast,inspect\n", |
31 | 31 | "from tokenize import tokenize,COMMENT\n", |
32 | 32 | "from ast import parse,FunctionDef,AsyncFunctionDef,AnnAssign\n", |
33 | 33 | "from io import BytesIO\n", |
|
840 | 840 | "output_type": "stream", |
841 | 841 | "text": [ |
842 | 842 | "The sum of two numbers.\n", |
843 | | - " \n", |
| 843 | + "\n", |
844 | 844 | " Used to demonstrate numpy-style docstrings.\n", |
845 | 845 | "\n", |
846 | 846 | "Parameters\n", |
|
1019 | 1019 | "\n", |
1020 | 1020 | "@delegates(_a)\n", |
1021 | 1021 | "def _b(b:str, # Second\n", |
1022 | | - " **kwargs): \n", |
| 1022 | + " **kwargs\n", |
| 1023 | + " ): # Return nothing\n", |
1023 | 1024 | " return b, (_a(**kwargs)) \n", |
1024 | 1025 | "\n", |
1025 | 1026 | "docments(_b)" |
|
1073 | 1074 | "@delegates(_c)\n", |
1074 | 1075 | "def _d(c:int, # First\n", |
1075 | 1076 | " b:str, **kwargs\n", |
1076 | | - " )->int:\n", |
| 1077 | + " )->int: # Return an int\n", |
1077 | 1078 | " return c, _c(b, **kwargs)" |
1078 | 1079 | ] |
1079 | 1080 | }, |
|
1090 | 1091 | "test_eq(docments(_d, full=True).keys() & _argset, _argset) # _d has the args a,b,c and return" |
1091 | 1092 | ] |
1092 | 1093 | }, |
| 1094 | + { |
| 1095 | + "cell_type": "code", |
| 1096 | + "execution_count": null, |
| 1097 | + "metadata": {}, |
| 1098 | + "outputs": [], |
| 1099 | + "source": [ |
| 1100 | + "#| export\n", |
| 1101 | + "def sig2str(func):\n", |
| 1102 | + " \"Generate function signature with docments as comments\"\n", |
| 1103 | + " docs = docments(func, full=True)\n", |
| 1104 | + " params = []\n", |
| 1105 | + " for k,v in docs.items():\n", |
| 1106 | + " if k == 'return': continue\n", |
| 1107 | + " anno = getattr(v['anno'], '__name__', str(v['anno'])) if v['anno'] != inspect._empty else ''\n", |
| 1108 | + " if '|' in str(v['anno']): anno = str(v['anno'])\n", |
| 1109 | + " p = k + (f':{anno}' if anno and anno != 'inspect._empty' else '')\n", |
| 1110 | + " if v['default'] != inspect._empty:\n", |
| 1111 | + " d = getattr(v['default'], '__name__', v['default']) if hasattr(v['default'], '__name__') else v['default']\n", |
| 1112 | + " p += f'={d}' if d is not None else '=None'\n", |
| 1113 | + " if v['docment']: p += f' # {v[\"docment\"]}'\n", |
| 1114 | + " params.append(p)\n", |
| 1115 | + " \n", |
| 1116 | + " ret = docs.get('return', {})\n", |
| 1117 | + " ret_str = ':'\n", |
| 1118 | + " if ret and ret.get('anno')!=inspect._empty:\n", |
| 1119 | + " ret_str = f\"->{getattr(ret['anno'], '__name__', str(ret['anno']))}\" + (f': # {ret[\"docment\"]}' if ret.get('docment') else ':')\n", |
| 1120 | + " doc_str = f' \"{func.__doc__}\"' if func.__doc__ else ''\n", |
| 1121 | + " return f\"def {func.__name__}(\\n \" + \",\\n \".join(params) + f\"\\n){ret_str}\\n{doc_str}\"" |
| 1122 | + ] |
| 1123 | + }, |
| 1124 | + { |
| 1125 | + "cell_type": "code", |
| 1126 | + "execution_count": null, |
| 1127 | + "metadata": {}, |
| 1128 | + "outputs": [ |
| 1129 | + { |
| 1130 | + "name": "stdout", |
| 1131 | + "output_type": "stream", |
| 1132 | + "text": [ |
| 1133 | + "def _d(\n", |
| 1134 | + " b:str # Second,\n", |
| 1135 | + " a:int=2 # Third,\n", |
| 1136 | + " c:int # First\n", |
| 1137 | + ")->int: # Return an int\n", |
| 1138 | + "\n" |
| 1139 | + ] |
| 1140 | + } |
| 1141 | + ], |
| 1142 | + "source": [ |
| 1143 | + "print(sig2str(_d))" |
| 1144 | + ] |
| 1145 | + }, |
1093 | 1146 | { |
1094 | 1147 | "cell_type": "markdown", |
1095 | 1148 | "metadata": {}, |
|
1108 | 1161 | " params = [a.arg for a in node.args.args]\n", |
1109 | 1162 | " if node.args.vararg: params.append(f\"*{node.args.vararg.arg}\")\n", |
1110 | 1163 | " if node.args.kwarg: params.append(f\"**{node.args.kwarg.arg}\")\n", |
1111 | | - " return \", \".join(params)\n", |
1112 | | - "\n", |
| 1164 | + " return \", \".join(params)" |
| 1165 | + ] |
| 1166 | + }, |
| 1167 | + { |
| 1168 | + "cell_type": "code", |
| 1169 | + "execution_count": null, |
| 1170 | + "metadata": {}, |
| 1171 | + "outputs": [], |
| 1172 | + "source": [ |
| 1173 | + "#| export\n", |
1113 | 1174 | "class _DocstringExtractor(ast.NodeVisitor):\n", |
1114 | 1175 | " def __init__(self): self.docstrings,self.cls,self.cls_init = {},None,None\n", |
1115 | 1176 | "\n", |
|
0 commit comments