@@ -29,7 +29,10 @@ def add_path(path):
29
29
this_dir = osp .dirname (__file__ )
30
30
# Add docs/api to PYTHONPATH
31
31
add_path (osp .abspath (osp .join (this_dir , ".." , "docs" , "api" )))
32
- from extract_api_from_docs import extract_params_desc_from_rst_file
32
+ from extract_api_from_docs import (
33
+ extract_params_desc_from_rst_file ,
34
+ gen_functions_args_str ,
35
+ )
33
36
34
37
arguments = [
35
38
# flags, dest, type, default, help
@@ -60,37 +63,11 @@ def _check_params_in_description(rstfilename, paramstr):
60
63
params_in_title = []
61
64
if paramstr :
62
65
fake_func = ast .parse (f"def fake_func({ paramstr } ): pass" )
63
- # Iterate over all in_title parameters
64
- num_defaults = len (fake_func .body [0 ].args .defaults )
65
- num_args = len (fake_func .body [0 ].args .args )
66
- # args & defaults
67
- for i , arg in enumerate (fake_func .body [0 ].args .args ):
68
- if i >= num_args - num_defaults :
69
- default_value = fake_func .body [0 ].args .defaults [
70
- i - (num_args - num_defaults )
71
- ]
72
- params_in_title .append (f"{ arg .arg } ={ default_value } " )
73
- else :
74
- params_in_title .append (arg .arg )
75
- # posonlyargs
76
- for arg in fake_func .body [0 ].args .posonlyargs :
77
- params_in_title .append (arg .arg )
78
- # vararg(*args)
79
- if fake_func .body [0 ].args .vararg :
80
- params_in_title .append (fake_func .body [0 ].args .vararg .arg )
81
- # kwonlyargs & kw_defaults
82
- for i , arg in enumerate (fake_func .body [0 ].args .kwonlyargs ):
83
- if (
84
- i < len (fake_func .body [0 ].args .kw_defaults )
85
- and fake_func .body [0 ].args .kw_defaults [i ] is not None
86
- ):
87
- default_value = fake_func .body [0 ].args .kw_defaults [i ]
88
- params_in_title .append (f"{ arg .arg } ={ default_value } " )
89
- else :
90
- params_in_title .append (arg .arg )
91
- # **kwargs
92
- if fake_func .body [0 ].args .kwarg :
93
- params_in_title .append (fake_func .body [0 ].args .kwarg .arg )
66
+ func_node = fake_func .body [0 ]
67
+ func_args_str = gen_functions_args_str (func_node )
68
+ params_in_title = func_args_str .split (", " )
69
+ params_in_title .remove ("/" )
70
+ params_in_title .remove ("*" )
94
71
95
72
funcdescnode = extract_params_desc_from_rst_file (rstfilename )
96
73
if funcdescnode :
@@ -141,11 +118,35 @@ def _check_params_in_description(rstfilename, paramstr):
141
118
def _check_params_in_description_with_fullargspec (rstfilename , funcname ):
142
119
flag = True
143
120
info = ""
144
- funcspec = inspect .getfullargspec (eval (funcname ))
121
+ try :
122
+ func = eval (funcname )
123
+ except NameError :
124
+ func = eval (funcname )
125
+ source = inspect .getsource (func )
126
+
127
+ class FunctionDefExtractor (ast .NodeTransformer ):
128
+ target_name = func .__name__
129
+
130
+ def visit_FunctionDef (self , node ):
131
+ if node .name == self .target_name :
132
+ node .decorator_list = []
133
+ node .body = [ast .Pass ()]
134
+ return node
135
+ return None
136
+
137
+ tree = ast .parse (source )
138
+ modified_tree = FunctionDefExtractor ().visit (tree )
139
+ modified_tree .body = [
140
+ node for node in modified_tree .body if node is not None
141
+ ]
142
+
143
+ func_node = modified_tree .body [0 ]
144
+ params_inspec = gen_functions_args_str (func_node ).split (", " )
145
+ params_inspec .remove ("/" )
146
+ params_inspec .remove ("*" )
145
147
funcdescnode = extract_params_desc_from_rst_file (rstfilename )
146
148
if funcdescnode :
147
149
items = funcdescnode .children [1 ].children [0 ].children
148
- params_inspec = funcspec .args
149
150
if len (items ) != len (params_inspec ):
150
151
flag = False
151
152
info = f"check_with_fullargspec failed (parammeters description): { rstfilename } "
@@ -171,10 +172,10 @@ def _check_params_in_description_with_fullargspec(rstfilename, funcname):
171
172
f"check failed (parammeters description): { rstfilename } , param name not found in { i } paragraph."
172
173
)
173
174
else :
174
- if funcspec . args :
175
+ if params_inspec :
175
176
info = "params section not found in description, check it please."
176
177
print (
177
- f"check failed (parameters description not found): { rstfilename } , { funcspec . args } ."
178
+ f"check failed (parameters description not found): { rstfilename } , { params_inspec } ."
178
179
)
179
180
flag = False
180
181
return flag , info
0 commit comments