|
20 | 20 | import re
|
21 | 21 | import sys
|
22 | 22 |
|
| 23 | +import paddle # noqa: F401 |
| 24 | + |
23 | 25 |
|
24 | 26 | def add_path(path):
|
25 | 27 | if path not in sys.path:
|
@@ -70,6 +72,7 @@ def _check_params_in_description(rstfilename, paramstr):
|
70 | 72 | params_in_title.remove("/")
|
71 | 73 | if "*" in params_in_title:
|
72 | 74 | params_in_title.remove("*")
|
| 75 | + params_in_title = ", ".join(params_in_title) |
73 | 76 |
|
74 | 77 | funcdescnode = extract_params_desc_from_rst_file(rstfilename)
|
75 | 78 | if funcdescnode:
|
@@ -122,34 +125,20 @@ def _check_params_in_description_with_fullargspec(rstfilename, funcname):
|
122 | 125 | info = ""
|
123 | 126 | try:
|
124 | 127 | func = eval(funcname)
|
125 |
| - except NameError: |
126 |
| - import paddle # noqa: F401 |
127 |
| - |
128 |
| - func = eval(funcname) |
| 128 | + except AttributeError: |
| 129 | + flag = False |
| 130 | + info = f"function {funcname} in rst file {rstfilename} not found in paddle module, please check it." |
| 131 | + return flag, info |
129 | 132 | source = inspect.getsource(func)
|
130 | 133 |
|
131 |
| - class FunctionDefExtractor(ast.NodeTransformer): |
132 |
| - target_name = func.__name__ |
133 |
| - |
134 |
| - def visit_FunctionDef(self, node): |
135 |
| - if node.name == self.target_name: |
136 |
| - node.decorator_list = [] |
137 |
| - node.body = [ast.Pass()] |
138 |
| - return node |
139 |
| - return None |
140 |
| - |
141 | 134 | tree = ast.parse(source)
|
142 |
| - modified_tree = FunctionDefExtractor().visit(tree) |
143 |
| - modified_tree.body = [ |
144 |
| - node for node in modified_tree.body if node is not None |
145 |
| - ] |
146 |
| - |
147 |
| - func_node = modified_tree.body[0] |
| 135 | + func_node = tree.body[0] |
148 | 136 | params_inspec = gen_functions_args_str(func_node).split(", ")
|
149 | 137 | if "/" in params_inspec:
|
150 | 138 | params_inspec.remove("/")
|
151 | 139 | if "*" in params_inspec:
|
152 | 140 | params_inspec.remove("*")
|
| 141 | + params_inspec = ", ".join(params_inspec) |
153 | 142 | funcdescnode = extract_params_desc_from_rst_file(rstfilename)
|
154 | 143 | if funcdescnode:
|
155 | 144 | items = funcdescnode.children[1].children[0].children
|
@@ -207,16 +196,39 @@ def check_api_parameters(rstfiles, apiinfo):
|
207 | 196 | print(f"checking : {rstfile}")
|
208 | 197 | with open(rstfilename, "r") as rst_fobj:
|
209 | 198 | func_found = False
|
| 199 | + is_first_line = True |
| 200 | + api_label = None |
210 | 201 | for line in rst_fobj:
|
| 202 | + if is_first_line: |
| 203 | + api_label = ( |
| 204 | + line.strip() |
| 205 | + .removeprefix(".. _cn_api_") |
| 206 | + .replace("_", ".") |
| 207 | + .removesuffix("__upper") |
| 208 | + ) |
| 209 | + is_first_line = False |
211 | 210 | mo = pat.match(line)
|
212 | 211 | if mo:
|
213 | 212 | func_found = True
|
214 | 213 | functype = mo.group(1)
|
215 | 214 | if functype not in ("function", "method"):
|
| 215 | + # TODO: check class method |
216 | 216 | check_passed.append(rstfile)
|
217 | 217 | continue
|
218 | 218 | funcname = mo.group(2)
|
219 | 219 | paramstr = mo.group(3)
|
| 220 | + |
| 221 | + # check same as the api_label |
| 222 | + if funcname != api_label: |
| 223 | + # if funcname is a function, try to back to class |
| 224 | + obj = eval(funcname) |
| 225 | + if inspect.isfunction(obj): |
| 226 | + class_name = ".".join(funcname.split(".")[:-1]) |
| 227 | + if class_name != api_label: |
| 228 | + flag = False |
| 229 | + info = f"funcname in title is not same as the label name: {funcname} != {api_label}." |
| 230 | + return flag, info |
| 231 | + |
220 | 232 | flag = False
|
221 | 233 | func_found_in_json = False
|
222 | 234 | for apiobj in apiinfo.values():
|
|
0 commit comments