Skip to content

Commit 3d4b83e

Browse files
Preserve docstrings
1 parent 234278e commit 3d4b83e

File tree

2 files changed

+598
-68
lines changed

2 files changed

+598
-68
lines changed

ci/gen_stubs.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import subprocess
66

77
ELLIPSIS_NODE = ast.parse("...").body
8-
# TODO: Preserve docstrings in method/function definitions
98
# TODO: Remove unused imports
109

1110
# Load the AST for the base object definitions
@@ -54,12 +53,14 @@
5453
# Replace all methods with type stubs
5554
for subnode_index, subnode in enumerate(base_node.body):
5655
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
5858
elif isinstance(subnode, ast.AsyncFunctionDef):
59+
comment = [subnode.body[0]] if isinstance(subnode.body[0], ast.Expr) else []
5960
base_node.body[subnode_index] = ast.FunctionDef(
6061
name=subnode.name,
6162
args=subnode.args,
62-
body=ELLIPSIS_NODE,
63+
body=comment + ELLIPSIS_NODE,
6364
decorator_list=subnode.decorator_list,
6465
returns=subnode.returns,
6566
type_comment=subnode.type_comment,
@@ -83,10 +84,11 @@
8384
or isinstance(base_node, ast.AsyncFunctionDef)
8485
) and base_node.name == node.name:
8586
# Replace the function definition
87+
comment = [base_node.body[0]] if isinstance(base_node.body[0], ast.Expr) else []
8688
tree.body[node_index] = ast.FunctionDef(
8789
name=base_node.name,
8890
args=base_node.args,
89-
body=ELLIPSIS_NODE,
91+
body=comment+ELLIPSIS_NODE,
9092
decorator_list=base_node.decorator_list,
9193
returns=base_node.returns,
9294
type_comment=base_node.type_comment,

0 commit comments

Comments
 (0)