Skip to content
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

Fix ci parameter checking script #6864

Merged
merged 7 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 84 additions & 27 deletions ci_scripts/check_api_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,76 +56,128 @@ def parse_args():

def _check_params_in_description(rstfilename, paramstr):
flag = True
params_intitle = []
info = ""
params_in_title = []
if paramstr:
fake_func = ast.parse(f"def fake_func({paramstr}): pass")
for arg in fake_func.body[0].args.args:
params_intitle.append(arg.arg)
# 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)

funcdescnode = extract_params_desc_from_rst_file(rstfilename)
if funcdescnode:
items = funcdescnode.children[1].children[0].children
if len(items) != len(params_intitle):
list_pat = r"^<list_item>.*</list_item>$"
if not re.match(list_pat, str(items[0])):
flag = False
info = "Something wrong with the format of params list in description, check it please."
elif len(items) != len(params_in_title):
flag = False
if not items:
info = (
"Params section in description is empty, check it please."
)
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}")
else:
for i in range(len(items)):
pname_intitle = params_intitle[i].split("=")[0].strip()
mo = re.match(r"(\w+)\b.*", items[i].children[0].astext())
pname_in_title = params_in_title[i].split("=")[0].strip()
mo = re.match(
r"\*{0,2}(\w+)\b.*", items[i].children[0].astext()
)
if mo:
pname_indesc = mo.group(1)
if pname_indesc != pname_intitle:
if pname_indesc != pname_in_title:
flag = False
info = f"the following param in title does not match the param in description: {pname_in_title} != {pname_indesc}."
print(
f"check failed (parammeters description): {rstfilename}, {pname_indesc} != {pname_intitle}"
f"check failed (parammeters description): {rstfilename}, {pname_in_title} != {pname_indesc}"
)
else:
flag = False
info = f"param name '{pname_in_title}' not matched in description line{i+1}, check it please."
print(
f"check failed (parammeters description): {rstfilename}, param name not found in {i} paragraph."
)
else:
if params_intitle:
if params_in_title:
info = "params section not found in description, check it please."
print(
f"check failed (parameters description not found): {rstfilename}, {params_intitle}."
f"check failed (parameters description not found): {rstfilename}, {params_in_title}."
)
flag = False
return flag
return flag, info


def _check_params_in_description_with_fullargspec(rstfilename, funcname):
flag = True
info = ""
funcspec = inspect.getfullargspec(eval(funcname))
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}"
print(f"check failed (parammeters description): {rstfilename}")
else:
for i in range(len(items)):
pname_intitle = params_inspec[i]
mo = re.match(r"(\w+)\b.*", items[i].children[0].astext())
pname_in_title = params_inspec[i]
mo = re.match(
r"\*{0,2}(\w+)\b.*", items[i].children[0].astext()
)
if mo:
pname_indesc = mo.group(1)
if pname_indesc != pname_intitle:
if pname_indesc != pname_in_title:
flag = False
info = f"the following param in title does not match the param in description: {pname_in_title} != {pname_indesc}."
print(
f"check failed (parammeters description): {rstfilename}, {pname_indesc} != {pname_intitle}"
f"check failed (parammeters description): {rstfilename}, {pname_in_title} != {pname_indesc}"
)
else:
flag = False
info = f"param name '{pname_in_title}' not matched in description line{i+1}, check it please."
print(
f"check failed (parammeters description): {rstfilename}, param name not found in {i} paragraph."
)
else:
if funcspec.args:
info = "params section not found in description, check it please."
print(
f"check failed (parameters description not found): {rstfilename}, {funcspec.args}."
)
flag = False
return flag
return flag, info


def check_api_parameters(rstfiles, apiinfo):
Expand All @@ -138,11 +190,11 @@ def check_api_parameters(rstfiles, apiinfo):
2. Some COMPLICATED annotations may break the scripts.
"""
pat = re.compile(
r"^\.\.\s+py:(method|function|class)::\s+(\S+)\s*\(\s*(.*)\s*\)\s*$"
r"^\.\.\s+py:(method|function|class)::\s+([^\s(]+)\s*(?:\(\s*(.*)\s*\))?\s*$"
)
check_passed = []
check_failed = []
api_notfound = []
check_failed = {}
api_notfound = {}
for rstfile in rstfiles:
rstfilename = osp.join("../docs", rstfile)
print(f"checking : {rstfile}")
Expand Down Expand Up @@ -171,22 +223,24 @@ def check_api_parameters(rstfiles, apiinfo):
print(
f"check func:{funcname} in {rstfilename} with {paramstr}"
)
flag = _check_params_in_description(
flag, info = _check_params_in_description(
rstfilename, paramstr
)
else:
print(
f'check func:{funcname} in {rstfilename} with {paramstr}, but different with json\'s {apiobj["args"]}'
)
flag = _check_params_in_description(
flag, info = _check_params_in_description(
rstfilename, paramstr
)
else: # paddle.abs class_method does not have `args` in its json item.
print(
f"check func:{funcname} in {rstfilename} with its FullArgSpec"
)
flag = _check_params_in_description_with_fullargspec(
rstfilename, funcname
flag, info = (
_check_params_in_description_with_fullargspec(
rstfilename, funcname
)
)
break
if not func_found_in_json: # may be inner functions
Expand All @@ -200,11 +254,12 @@ def check_api_parameters(rstfiles, apiinfo):
check_passed.append(rstfile)
print(f"check success: {rstfile}")
else:
check_failed.append(rstfile)
check_failed[rstfile] = info
print(f"check failed: {rstfile}")
break
if not func_found:
api_notfound.append(rstfile)
info = 'funcname in title is not found, please check the format of ".. py:function::func()"'
api_notfound[rstfile] = info
print(f"check failed (object not found): {rstfile}")
print(f"checking done: {rstfile}")
return check_passed, check_failed, api_notfound
Expand All @@ -220,9 +275,11 @@ def check_api_parameters(rstfiles, apiinfo):
result = True
if check_failed:
result = False
print(f"check_api_parameters failed: {check_failed}")
for path, info in check_failed.items():
print(f"Checking failed file:{path}\nError:{info}\n")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的 path 是生成的 html 文件路径还是原始的 rst 文件路径?从提示角度来看,使用源文件 rst 是更推荐的

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path是rst文件的路径文件名,例如"api/paddle/unstack_cn.rst",是要改一下变量名吗?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path是rst文件的路径文件名,例如"api/paddle/unstack_cn.rst",是要改一下变量名吗?

这个倒不用,只是确认下

if api_notfound:
print(f"check_api_parameters funcname not found in: {api_notfound}")
for path, info in api_notfound.items():
print(f"Checking failed file:{path}\nError:{info}\n")
if result:
sys.exit(0)
else:
Expand Down
3 changes: 3 additions & 0 deletions ci_scripts/check_api_parameters.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ function filter_cn_api_files() {
local __resultvar=$2
local need_check_files=""
for file in `echo $git_files`;do
if [[ "$file" == *"Overview_cn.rst"* ]] || [[ "$file" == *__cn.rst ]]; then
continue
fi
echo "$file" | grep '.*\.rst$' > /dev/null
if [ $? -eq 0 ] ;then
need_check_files="${need_check_files} $file"
Expand Down
2 changes: 1 addition & 1 deletion ci_scripts/ci_start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ if [ "${BUILD_DOC}" = "true" ] && [ -x /usr/local/bin/sphinx-build ] ; then
fi
fi

check_parameters=OFF
check_parameters=ON
if [ "${check_parameters}" = "OFF" ] ; then
#echo "chinese api doc fileslist is empty, skip check."
echo "check_api_parameters is not stable, close it temporarily."
Expand Down
6 changes: 3 additions & 3 deletions docs/api/paddle/atan2_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ atan2
参数
:::::::::

- **x** (Tensor) - 输入的 Tensor,数据类型为:int32、int64、float16、float32、float64。
- **y** (Tensor) - 输入的 Tensor,数据类型为:int32、int64、float16、float32、float64。
- **name** (str,可选) - 操作的名称(可选,默认值为 None)。更多信息请参见 :ref:`api_guide_Name`。
- **x** (Tensor) - 输入的 Tensor,数据类型为:int32、int64、float16、float32、float64。
- **y** (Tensor) - 输入的 Tensor,数据类型为:int32、int64、float16、float32、float64。
- **name** (str,可选) - 操作的名称(可选,默认值为 None)。更多信息请参见 :ref:`api_guide_Name`。

返回
:::::::::
Expand Down
1 change: 1 addition & 0 deletions docs/api/paddle/bitwise_and_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ bitwise_and
- **x** (Tensor)- 输入的 N-D `Tensor`,数据类型为:bool,uint8,int8,int16,int32,int64。
- **y** (Tensor)- 输入的 N-D `Tensor`,数据类型为:bool,uint8,int8,int16,int32,int64。
- **out** (Tensor,可选)- 输出的结果 `Tensor`,是与输入数据类型相同的 N-D `Tensor`。默认值为 None,此时将创建新的 Tensor 来保存输出结果。
- **name** (str,可选) - 具体用法请参见 :ref:`api_guide_Name`,一般无需设置,默认值为 None。

返回
::::::::::::
Expand Down
1 change: 1 addition & 0 deletions docs/api/paddle/bitwise_not_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ bitwise_not

- **x** (Tensor)- 输入的 N-D `Tensor`,数据类型为:bool,uint8,int8,int16,int32,int64。
- **out** (Tensor,可选)- 输出的结果 `Tensor`,是与输入数据类型相同的 N-D `Tensor`。默认值为 None,此时将创建新的 Tensor 来保存输出结果。
- **name** (str,可选) - 具体用法请参见 :ref:`api_guide_Name`,一般无需设置,默认值为 None。

返回
::::::::::::
Expand Down
1 change: 1 addition & 0 deletions docs/api/paddle/bitwise_or_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ bitwise_or
- **x** (Tensor)- 输入的 N-D `Tensor`,数据类型为:bool,uint8,int8,int16,int32,int64。
- **y** (Tensor)- 输入的 N-D `Tensor`,数据类型为:bool,uint8,int8,int16,int32,int64。
- **out** (Tensor,可选)- 输出的结果 `Tensor`,是与输入数据类型相同的 N-D `Tensor`。默认值为 None,此时将创建新的 Tensor 来保存输出结果。
- **name** (str,可选) - 具体用法请参见 :ref:`api_guide_Name`,一般无需设置,默认值为 None。

返回
::::::::::::
Expand Down
1 change: 1 addition & 0 deletions docs/api/paddle/bitwise_xor_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ bitwise_xor
- **x** (Tensor)- 输入的 N-D `Tensor`,数据类型为:bool,uint8,int8,int16,int32,int64。
- **y** (Tensor)- 输入的 N-D `Tensor`,数据类型为:bool,uint8,int8,int16,int32,int64。
- **out** (Tensor,可选)- 输出的结果 `Tensor`,是与输入数据类型相同的 N-D `Tensor`。默认值为 None,此时将创建新的 Tensor 来保存输出结果。
- **name** (str,可选) - 具体用法请参见 :ref:`api_guide_Name`,一般无需设置,默认值为 None。

返回
::::::::::::
Expand Down
4 changes: 2 additions & 2 deletions docs/api/paddle/deg2rad_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ deg2rad
参数
:::::::::

- **x** (Tensor) - 输入的 Tensor,数据类型为:int32、int64、float32、float64。
- **name** (str,可选) - 操作的名称(可选,默认值为 None)。更多信息请参见 :ref:`api_guide_Name`。
- **x** (Tensor) - 输入的 Tensor,数据类型为:int32、int64、float32、float64。
- **name** (str,可选) - 操作的名称(可选,默认值为 None)。更多信息请参见 :ref:`api_guide_Name`。

返回
:::::::::
Expand Down
7 changes: 2 additions & 5 deletions docs/api/paddle/einsum_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,12 @@ Einsum 求和过程理论上等价于如下四步,但实现中实际执行的
参数
:::::


**equation** (str):求和标记

**operands** (Tensor, [Tensor, ...]):输入 Tensor
- **equation** (str):求和标记
- **operands** (Tensor, [Tensor, ...]):输入 Tensor

返回
:::::


Tensor:输出 Tensor

代码示例
Expand Down
4 changes: 2 additions & 2 deletions docs/api/paddle/erfinv_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ erfinv
参数
:::::::::

- **x** (Tensor) - 输入的 Tensor,数据类型为:float16、bfloat16、float32、float64。
- **name** (str,可选) - 操作的名称(可选,默认值为 None)。更多信息请参见 :ref:`api_guide_Name`。
- **x** (Tensor) - 输入的 Tensor,数据类型为:float16、bfloat16、float32、float64。
- **name** (str,可选) - 操作的名称(可选,默认值为 None)。更多信息请参见 :ref:`api_guide_Name`。

返回
:::::::::
Expand Down
4 changes: 2 additions & 2 deletions docs/api/paddle/expm1_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ expm1
参数
:::::::::

- **x** (Tensor) - 该 OP 的输入为多维 Tensor。数据类型为:int32、int64、float16、float32、float64、complex64、complex128。
- **name** (str,可选) - 具体用法请参见 :ref:`api_guide_Name`,一般无需设置,默认值为 None。
- **x** (Tensor) - 该 OP 的输入为多维 Tensor。数据类型为:int32、int64、float16、float32、float64、complex64、complex128。
- **name** (str,可选) - 具体用法请参见 :ref:`api_guide_Name`,一般无需设置,默认值为 None。

返回
:::::::::
Expand Down
6 changes: 3 additions & 3 deletions docs/api/paddle/gcd_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ gcd
参数
:::::::::

- **x** (Tensor) - 输入的 Tensor,数据类型为:int32,int64。
- **y** (Tensor) - 输入的 Tensor,数据类型为:int32,int64。
- **name** (str,可选) - 操作的名称(可选,默认值为 None)。更多信息请参见 :ref:`api_guide_Name`。
- **x** (Tensor) - 输入的 Tensor,数据类型为:int32,int64。
- **y** (Tensor) - 输入的 Tensor,数据类型为:int32,int64。
- **name** (str,可选) - 操作的名称(可选,默认值为 None)。更多信息请参见 :ref:`api_guide_Name`。

返回
:::::::::
Expand Down
6 changes: 0 additions & 6 deletions docs/api/paddle/get_cuda_rng_state_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ get_cuda_rng_state

获取 cuda 随机数生成器的状态信息。


参数
::::::::::::

无。

返回
::::::::::::

Expand Down
7 changes: 0 additions & 7 deletions docs/api/paddle/get_default_dtype_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ get_default_dtype

得到当前全局的 dtype。该值初始是 float32。


参数
::::::::::::



返回
::::::::::::
string,这个全局 dtype 仅支持 float16、float32、float64。
Expand Down
2 changes: 1 addition & 1 deletion docs/api/paddle/histogramdd_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ histogramdd
参数
::::::::::::

- **input** (Tensor) - 输入多维 Tensor 。
- **x** (Tensor) - 输入多维 Tensor 。
- **bins** (Tensor[]|int[]|int) - 如果为 Tensor 数组,则表示所有 bin 边界。如果为 int 数组,则表示每个维度中等宽 bin 的数量。如果为 int,则表示所有维度的等宽 bin 数量。默认值为 10 ,表示所有维度的等宽 bin 数量为 10 个。
- **ranges** (float[], 可选) - 表示每个维度中最左边和最右边的 bin 边界。如果为 None ,则将每个尺寸的最小值和最大值设置为最左边和最右边。默认值为 None ,表示自动根据最大值与最小值计算 bin 的边界。
- **density** (bool,可选) - 表示是否计算 density ,如果为 False,结果将包含每个 bin 中的计数(或权重)。如果为 True,则将每个计数(权重)除以总计数(总权重),然后再除以相关 bin 的宽度。默认值为 False ,表示不计算 density 。
Expand Down
Loading