|
5 | 5 | import subprocess |
6 | 6 |
|
7 | 7 | ELLIPSIS_NODE = ast.parse("...").body |
8 | | -# TODO: Preserve docstrings in method/function definitions |
9 | 8 | # TODO: Remove unused imports |
10 | 9 |
|
11 | 10 | # Load the AST for the base object definitions |
|
54 | 53 | # Replace all methods with type stubs |
55 | 54 | for subnode_index, subnode in enumerate(base_node.body): |
56 | 55 | if isinstance(subnode, ast.FunctionDef): |
57 | | - subnode.body = ELLIPSIS_NODE |
| 56 | + comment = [subnode.body[0]] if isinstance(subnode.body[0], ast.Expr) else [] |
| 57 | + subnode.body = comment + ELLIPSIS_NODE |
58 | 58 | elif isinstance(subnode, ast.AsyncFunctionDef): |
| 59 | + comment = [subnode.body[0]] if isinstance(subnode.body[0], ast.Expr) else [] |
59 | 60 | base_node.body[subnode_index] = ast.FunctionDef( |
60 | 61 | name=subnode.name, |
61 | 62 | args=subnode.args, |
62 | | - body=ELLIPSIS_NODE, |
| 63 | + body=comment + ELLIPSIS_NODE, |
63 | 64 | decorator_list=subnode.decorator_list, |
64 | 65 | returns=subnode.returns, |
65 | 66 | type_comment=subnode.type_comment, |
|
83 | 84 | or isinstance(base_node, ast.AsyncFunctionDef) |
84 | 85 | ) and base_node.name == node.name: |
85 | 86 | # Replace the function definition |
| 87 | + comment = [base_node.body[0]] if isinstance(base_node.body[0], ast.Expr) else [] |
86 | 88 | tree.body[node_index] = ast.FunctionDef( |
87 | 89 | name=base_node.name, |
88 | 90 | args=base_node.args, |
89 | | - body=ELLIPSIS_NODE, |
| 91 | + body=comment+ELLIPSIS_NODE, |
90 | 92 | decorator_list=base_node.decorator_list, |
91 | 93 | returns=base_node.returns, |
92 | 94 | type_comment=base_node.type_comment, |
|
0 commit comments