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

[Don't merge]check new python syntax #60279

Closed
wants to merge 2 commits into from
Closed
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
80 changes: 80 additions & 0 deletions demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import paddle

all_dir = list(
set(
dir(paddle.Tensor)
+ dir(paddle.base.framework.Variable)
+ dir(paddle.base.libpaddle.pir.OpResult)
)
)
all_dir.sort()
output_text = """| math | 动态图Tensor | 老IR Varialbe | 新IR OpResult |
|:---------------------:|:--------------------------------------------------------:|:------------------------------------------------------:|:------------------------------------------------------:|
"""


print(len(all_dir))
for i in all_dir:
math_name = f"`{i}`" if "__" in i else i
temp_line = f"| {math_name} "
if i in dir(paddle.Tensor):
temp_line += "| ✅ "
else:
temp_line += "| ❌ "

if i in dir(paddle.base.framework.Variable):
temp_line += "| ✅ "
else:
temp_line += "| ❌ "

if i in dir(paddle.base.libpaddle.pir.OpResult):
# api 对应 pr 匹配规则
if i in paddle.tensor.tensor_method_func:
temp_line += "| #57857 |"
else:
match i:
case "place" | "ndimension" | "dim" | "ndim" | "item":
temp_line += "| #58042 |"
case "astype":
temp_line += "| #58026 |"
case "__div__" | "__truediv__" | "__rdiv__" | "__rtruediv__":
temp_line += "| #57997 |"
case "__rmul__" | "__mul__" | "__rsub__" | "__sub__" | "__radd__" | "__add__":
temp_line += "| #58106 |"
case "__pow__" | "__rpow__" | "__floordiv__" | "__mod__" | "__matmul__":
temp_line += "| #58219 |"
case "__ne__" | "__lt__" | "__le__" | "__gt__" | "__ge__" | "__and__" | "__or__" | "__xor__" | "__invert__":
temp_line += "| #58343 |"
case "clone":
temp_line += "| #59115 |"
case "append":
temp_line += "| #59220 |"
case "cpu" | "cuda":
temp_line += "| #59300 |"
case "__neg__":
temp_line += "| #60166 |"
case "__eq__":
temp_line += "| #58896 |"
case _:
temp_line += "| ✅ |"
else:
temp_line += "| ❌ |"

output_text += temp_line + "\n"

with open("./method_compare.md", "w") as file:
file.write(output_text)
7 changes: 6 additions & 1 deletion paddle/scripts/paddle_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4286,9 +4286,14 @@ function main() {
run_linux_cpu_test ${PYTHON_ABI:-""} ${PROC_RUN:-1}
;;
cicheck_sot)
check_run_sot_ci
export WITH_SHARED_PHI=ON
PYTHON_VERSIONS=(3.8 3.9 3.10 3.11)
# check python syntax
for PY_VERSION in ${PYTHON_VERSIONS[@]}; do
python$PY_VERSION tools/codestyle/check_new_python_syntax.py $(git diff --name-only upstream/develop | grep '.py$')
done
check_run_sot_ci

for PY_VERSION in ${PYTHON_VERSIONS[@]}; do
ln -sf $(which python${PY_VERSION}) /usr/local/bin/python
ln -sf $(which pip${PY_VERSION}) /usr/local/bin/pip
Expand Down
49 changes: 49 additions & 0 deletions tools/codestyle/check_new_python_syntax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import argparse
import ast
import os
from typing import Sequence


def check_file(filename: str) -> int:
try:
if not os.path.exists(filename):
return 0
with open(filename, 'rb') as f:
ast_obj = ast.parse(f.read(), filename=filename)
except SyntaxError:
print(f'{filename} - ast Parsing failed')
return 1

return 0


def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*', help='Filenames to run')
args = parser.parse_args(argv)

retv = 0
for filename in args.filenames:
print(f"check: {filename}")
retv |= check_file(filename)
return retv


if __name__ == '__main__':
raise SystemExit(main())