-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_header_list.py
More file actions
49 lines (37 loc) · 1.57 KB
/
create_header_list.py
File metadata and controls
49 lines (37 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import ast
def get_type_name(annotation):
"""Extracting a complex type from annotation"""
if isinstance(annotation, ast.Name):
return annotation.id
elif isinstance(annotation, ast.Subscript):
value = get_type_name(annotation.value)
slice_ = get_type_name(annotation.slice)
return f"{value}[{slice_}]"
elif isinstance(annotation, ast.Tuple):
return f"({', '.join([get_type_name(elt) for elt in annotation.elts])})"
elif isinstance(annotation, ast.BinOp):
# for unions of types (for example: str | Figure)
left = get_type_name(annotation.left)
right = get_type_name(annotation.right)
return f"{left} | {right}"
return "Any"
def extract_function_headers_and_docs(input_file='utils.py'):
with open(input_file, 'r') as f:
code = f.read()
tree = ast.parse(code)
function_details = []
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
func_name = node.name
docstring = ast.get_docstring(node) or "No documentation"
args = []
for arg in node.args.args:
arg_type = None
if arg.annotation:
arg_type = get_type_name(arg.annotation)
args.append(f"{arg.arg}: {arg_type if arg_type else 'Any'}")
args_str = ",\n\t".join(args)
function_details.append(f"{func_name}({args_str})\n'''{docstring}'''\n")
#with open(output_file, 'w') as f:
# f.write("\n".join(function_details))
return "\n".join(function_details)