Skip to content

Commit 177944d

Browse files
authored
[CodeStyle] Replace black with ruff format and format all python and jupyter files (#6470)
* [CodeStyle] Replace `black` with `ruff format` and format all python and jupyter files * clean black config * fix a format
1 parent 93ef32d commit 177944d

File tree

62 files changed

+6821
-4856
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+6821
-4856
lines changed

.pre-commit-config.yaml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,11 @@ repos:
3333
language: system
3434
files: .+README(\.cn)?\.md$
3535
# For Python files
36-
- repo: https://github.com/psf/black.git
37-
rev: 23.3.0
38-
hooks:
39-
- id: black
40-
files: \.py$
4136
- repo: https://github.com/astral-sh/ruff-pre-commit
42-
rev: v0.1.5
37+
rev: v0.1.13
4338
hooks:
4439
- id: ruff
4540
args: [--fix, --exit-non-zero-on-fix, --no-cache]
41+
types_or: [ python, pyi ]
42+
- id: ruff-format
43+
types_or: [ python, pyi, jupyter ]

.pre-commit-hooks/convert_markdown_into_html.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373

7474
def convert_markdown_into_html(argv=None):
7575
parser = argparse.ArgumentParser()
76-
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
76+
parser.add_argument("filenames", nargs="*", help="Filenames to fix")
7777
args = parser.parse_args(argv)
7878

7979
retv = 0
@@ -91,5 +91,5 @@ def convert_markdown_into_html(argv=None):
9191
return retv
9292

9393

94-
if __name__ == '__main__':
94+
if __name__ == "__main__":
9595
sys.exit(convert_markdown_into_html())

ci_scripts/CAPItools/main.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# 解析所有的函数, 类, 枚举, 返回一个字典
1717
# 多线程使用并不安全, 请不要使用多线程
1818
def analysis_file(path):
19-
header = CppHeaderParser.CppHeader(path, encoding='utf8')
19+
header = CppHeaderParser.CppHeader(path, encoding="utf8")
2020
data = json.loads(header.toJSON())
2121
return data
2222

@@ -36,8 +36,8 @@ def generate_docs(
3636
func_name = item["name"].replace("/", "")
3737

3838
# Note: 操作符仅不生成rst,实际上在Overview列表依然会呈现以提示存在此操作符
39-
if func_name.startswith('operator'):
40-
checkwords = func_name.replace('operator', '', 1)
39+
if func_name.startswith("operator"):
40+
checkwords = func_name.replace("operator", "", 1)
4141
if re.search(r"\w", checkwords) is None:
4242
continue # 跳过操作符声明
4343
rst_dir = os.path.join(save_dir, LANGUAGE, path, func_name + ".rst")
@@ -47,7 +47,7 @@ def generate_docs(
4747
helper.create_and_write_file(rst_dir, LANGUAGE)
4848
except:
4949
print(traceback.format_exc())
50-
print('FAULT GENERATE:' + rst_dir)
50+
print("FAULT GENERATE:" + rst_dir)
5151

5252
for item in all_class:
5353
path = item["filename"].replace("../", "").replace(".h", "")
@@ -62,7 +62,7 @@ def generate_docs(
6262
helper.create_and_write_file(rst_dir, LANGUAGE)
6363
except:
6464
print(traceback.format_exc())
65-
print('FAULT GENERATE:' + rst_dir)
65+
print("FAULT GENERATE:" + rst_dir)
6666

6767

6868
# cpp 对应 python api
@@ -80,13 +80,13 @@ def cpp2py(data: dict):
8080
# 2. 提取待生成文档的PADDLE_API list
8181
# 3. 生成文档
8282
if __name__ == "__main__":
83-
root_dir = ''
84-
save_dir = '.' # 默认保存在当前目录
83+
root_dir = ""
84+
save_dir = "." # 默认保存在当前目录
8585
if len(sys.argv) == 3:
8686
root_dir = sys.argv[1]
8787
save_dir = sys.argv[2]
8888

89-
if root_dir == '':
89+
if root_dir == "":
9090
try:
9191
import inspect
9292

@@ -95,8 +95,8 @@ def cpp2py(data: dict):
9595
root_dir = os.path.dirname(inspect.getsourcefile(paddle))
9696
except:
9797
# for simple run
98-
root_dir = '../paddle'
99-
save_dir = '.' # 默认保存在当前目录
98+
root_dir = "../paddle"
99+
save_dir = "." # 默认保存在当前目录
100100

101101
all_funcs = []
102102
all_class = []
@@ -115,8 +115,8 @@ def cpp2py(data: dict):
115115
cpp2py_api_list = cpp2py(cpp2py_data).copy()
116116

117117
# 跳过文件中未包含PADDLE_API
118-
with open(file_path, encoding='utf-8') as f:
119-
if 'PADDLE_API ' not in f.read():
118+
with open(file_path, encoding="utf-8") as f:
119+
if "PADDLE_API " not in f.read():
120120
continue
121121

122122
print("Parsing: ", file_path)
@@ -131,9 +131,9 @@ def cpp2py(data: dict):
131131
all_class.extend(current_class)
132132
overview_list.append(
133133
{
134-
'h_file': file_path,
135-
'class': current_class,
136-
'function': current_func,
134+
"h_file": file_path,
135+
"class": current_class,
136+
"function": current_func,
137137
}
138138
)
139139

ci_scripts/CAPItools/utils.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
def get_PADDLE_API_func(data: dict):
55
result = []
66
for i in data["functions"]:
7-
if 'PADDLE_API' in i['debug']:
7+
if "PADDLE_API" in i["debug"]:
88
result.append(i)
99
return result
1010

@@ -32,7 +32,7 @@ def get_parameters(parameters):
3232
# parameter_api = "" # 这里解析是给api使用的 (暂时不用)
3333
parameter_dict = {}
3434
for i in parameters:
35-
parameter_type_tmp = i['type'].replace(" &", "").replace(" *", "")
35+
parameter_type_tmp = i["type"].replace(" &", "").replace(" *", "")
3636
# * 和 & 情况
3737
# parameter_api += parameter_type_tmp
3838

@@ -41,18 +41,18 @@ def get_parameters(parameters):
4141
if i["pointer"] == 1:
4242
# parameter_api += "*"
4343
parameter_type_tmp += "*"
44-
if i["constant"] == 1 and not parameter_type_tmp.startswith('const'):
44+
if i["constant"] == 1 and not parameter_type_tmp.startswith("const"):
4545
parameter_type_tmp = "const " + parameter_type_tmp
4646
# parameter_api += f" {i['name']}, "
47-
desc = i.get('desc', '').replace(' ', '')
47+
desc = i.get("desc", "").replace(" ", "")
4848

4949
# special progress for none parameter name case
50-
if i['name'] == '&':
50+
if i["name"] == "&":
5151
continue
5252
else:
53-
parameter_dict[i['name']] = {
54-
'type': parameter_type_tmp,
55-
'intro': desc,
53+
parameter_dict[i["name"]] = {
54+
"type": parameter_type_tmp,
55+
"intro": desc,
5656
}
5757
# parameter += f"\t- **{i['name']}** ({parameter_type_tmp}) - {desc}\n"
5858
# 去掉末尾的逗号
@@ -67,27 +67,27 @@ def get_parameters(parameters):
6767
# 解析后分别将对应关键字后的内容放入字典对应关键字后
6868
def parse_doxygen(doxygen):
6969
doxygen_dict = {
70-
'intro': '',
71-
'returns': '',
72-
'param_intro': {},
73-
'note': '',
70+
"intro": "",
71+
"returns": "",
72+
"param_intro": {},
73+
"note": "",
7474
}
7575

76-
if '@' in doxygen:
77-
doxygen = doxygen[doxygen.find('@') :]
78-
for doxygen_part in doxygen.split('@'):
79-
if doxygen_part.startswith('brief '):
80-
doxygen_dict['intro'] = doxygen_part.replace('brief ', '', 1)
81-
elif doxygen_part.startswith('return '):
82-
doxygen_dict['returns'] = doxygen_part.replace('return ', '', 1)
83-
elif doxygen_part.startswith('param '):
84-
param_intro = doxygen_part.replace('param ', '', 1)
85-
param_name = param_intro[: param_intro.find(' ')]
86-
doxygen_dict['param_intro'][param_name] = param_intro[
87-
param_intro.find(' ') + 1 :
76+
if "@" in doxygen:
77+
doxygen = doxygen[doxygen.find("@") :]
78+
for doxygen_part in doxygen.split("@"):
79+
if doxygen_part.startswith("brief "):
80+
doxygen_dict["intro"] = doxygen_part.replace("brief ", "", 1)
81+
elif doxygen_part.startswith("return "):
82+
doxygen_dict["returns"] = doxygen_part.replace("return ", "", 1)
83+
elif doxygen_part.startswith("param "):
84+
param_intro = doxygen_part.replace("param ", "", 1)
85+
param_name = param_intro[: param_intro.find(" ")]
86+
doxygen_dict["param_intro"][param_name] = param_intro[
87+
param_intro.find(" ") + 1 :
8888
]
89-
elif doxygen_part.startswith('note '):
90-
doxygen_dict['note'] = doxygen_part.replace('note ', '', 1)
89+
elif doxygen_part.startswith("note "):
90+
doxygen_dict["note"] = doxygen_part.replace("note ", "", 1)
9191
else:
9292
pass
9393

0 commit comments

Comments
 (0)