Skip to content

[CI] refine_func_args_parse #7257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
96 changes: 60 additions & 36 deletions ci_scripts/check_api_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import re
import sys

import paddle # noqa: F401


def add_path(path):
if path not in sys.path:
Expand All @@ -30,6 +32,7 @@ def add_path(path):
# Add docs/api to PYTHONPATH
add_path(osp.abspath(osp.join(this_dir, "..", "docs", "api")))
from extract_api_from_docs import extract_params_desc_from_rst_file
from gen_doc import gen_functions_args_str

arguments = [
# flags, dest, type, default, help
Expand Down Expand Up @@ -60,37 +63,13 @@ def _check_params_in_description(rstfilename, paramstr):
params_in_title = []
if paramstr:
fake_func = ast.parse(f"def fake_func({paramstr}): pass")
# Iterate over all in_title parameters
num_defaults = len(fake_func.body[0].args.defaults)
num_args = len(fake_func.body[0].args.args)
# args & defaults
for i, arg in enumerate(fake_func.body[0].args.args):
if i >= num_args - num_defaults:
default_value = fake_func.body[0].args.defaults[
i - (num_args - num_defaults)
]
params_in_title.append(f"{arg.arg}={default_value}")
else:
params_in_title.append(arg.arg)
# posonlyargs
for arg in fake_func.body[0].args.posonlyargs:
params_in_title.append(arg.arg)
# vararg(*args)
if fake_func.body[0].args.vararg:
params_in_title.append(fake_func.body[0].args.vararg.arg)
# kwonlyargs & kw_defaults
for i, arg in enumerate(fake_func.body[0].args.kwonlyargs):
if (
i < len(fake_func.body[0].args.kw_defaults)
and fake_func.body[0].args.kw_defaults[i] is not None
):
default_value = fake_func.body[0].args.kw_defaults[i]
params_in_title.append(f"{arg.arg}={default_value}")
else:
params_in_title.append(arg.arg)
# **kwargs
if fake_func.body[0].args.kwarg:
params_in_title.append(fake_func.body[0].args.kwarg.arg)
func_node = fake_func.body[0]
func_args_str = gen_functions_args_str(func_node)
params_in_title = func_args_str.split(", ")
if "/" in params_in_title:
params_in_title.remove("/")
if "*" in params_in_title:
params_in_title.remove("*")

funcdescnode = extract_params_desc_from_rst_file(rstfilename)
if funcdescnode:
Expand All @@ -107,7 +86,9 @@ def _check_params_in_description(rstfilename, paramstr):
)
else:
info = f"The number of params in title does not match the params in description: {len(params_in_title)} != {len(items)}."
print(f"check failed (parammeters description): {rstfilename}")
print(
f"check failed with different nums (parammeters description): {rstfilename}"
)
else:
for i in range(len(items)):
pname_in_title = params_in_title[i].split("=")[0].strip()
Expand Down Expand Up @@ -141,11 +122,25 @@ def _check_params_in_description(rstfilename, paramstr):
def _check_params_in_description_with_fullargspec(rstfilename, funcname):
flag = True
info = ""
funcspec = inspect.getfullargspec(eval(funcname))
try:
func = eval(funcname)
except AttributeError:
flag = False
info = f"function {funcname} in rst file {rstfilename} not found in paddle module, please check it."
return flag, info
source = inspect.getsource(func)

tree = ast.parse(source)
func_node = tree.body[0]
params_inspec = gen_functions_args_str(func_node).split(", ")
if "/" in params_inspec:
params_inspec.remove("/")
if "*" in params_inspec:
params_inspec.remove("*")

funcdescnode = extract_params_desc_from_rst_file(rstfilename)
if funcdescnode:
items = funcdescnode.children[1].children[0].children
params_inspec = funcspec.args
if len(items) != len(params_inspec):
flag = False
info = f"check_with_fullargspec failed (parammeters description): {rstfilename}"
Expand All @@ -171,10 +166,10 @@ def _check_params_in_description_with_fullargspec(rstfilename, funcname):
f"check failed (parammeters description): {rstfilename}, param name not found in {i} paragraph."
)
else:
if funcspec.args:
if params_inspec:
info = "params section not found in description, check it please."
print(
f"check failed (parameters description not found): {rstfilename}, {funcspec.args}."
f"check failed (parameters description not found): {rstfilename}, {params_inspec}."
)
flag = False
return flag, info
Expand All @@ -200,16 +195,45 @@ def check_api_parameters(rstfiles, apiinfo):
print(f"checking : {rstfile}")
with open(rstfilename, "r") as rst_fobj:
func_found = False
is_first_line = True
api_label = None
for line in rst_fobj:
if is_first_line:
api_label = (
line.strip()
.removeprefix(".. _cn_api_")
.removesuffix(":")
.removesuffix("__upper")
)
is_first_line = False
mo = pat.match(line)
if mo:
func_found = True
functype = mo.group(1)
if functype not in ("function", "method"):
# TODO: check class method
check_passed.append(rstfile)
continue
funcname = mo.group(2)
paramstr = mo.group(3)
func_to_label = funcname.replace(".", "_")

# check same as the api_label
if func_to_label != api_label:
# if funcname is a function, try to back to class
try:
obj = eval(funcname)
except AttributeError:
obj = None
if obj is not None and inspect.isfunction(obj):
class_name = ".".join(funcname.split(".")[:-1])
class_to_label = class_name.replace(".", "_")
if class_to_label != api_label:
flag = False
info = f"funcname in title is not same as the label name: {funcname} != {api_label}."
check_failed[rstfile] = info
continue

flag = False
func_found_in_json = False
for apiobj in apiinfo.values():
Expand Down
2 changes: 1 addition & 1 deletion ci_scripts/check_api_parameters.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ if [ "$need_check_files" = "" ]
then
echo "need check files is empty, skip api parameters check"
else
python check_api_parameters.py --rst-files "${need_check_files}" --api-info $2
python check_api_parameters_patch.py --rst-files "${need_check_files}" --api-info $2
if [ $? -ne 0 ];then
set +x
echo "************************************************************************************"
Expand Down
Loading