已合并
Revert "修复ruff-check检查出来的规范性问题" #126
吴铭泾创建于 3月10日
Revert "修复ruff-check检查出来的规范性问题" #126
已合并
从已删除 :dev合入到Ascend/MindIE-Motor-CPPdev
Pull Request已成功合入, 合并人@吴铭泾
(感谢 吴铭泾 的贡献)ascend-robot
3月10日 评论:
3月10日 评论:
Thanks for your pull-request.
The full list of commands accepted by me can be found at here。
You can get sig-info at here
PR Approval Progress
⚠️ This PR does not yet meet the following requirements:lgtm (requires ≥ 2 person(s) per module)、approve (requires ≥ 1 person(s) per module)
Module Approval Details
| module | lgtm status | approve status |
|---|---|---|
| repo-Ascend/MindIE-Motor | ❌ (0/2)(You can also ask: ella07, dljzx, guanxinghua, hu-xinyi_555, pengxiao) | ❌ (0/1)(You can also ask: zk123, guanxinghua, towncharlie, yjy_ac, whuguozili) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)


3月10日 添加了label:ascend-cla/yes
ascend-robot
3月10日 评论:
3月10日 评论:
3月10日 合入了pull request
ascend-robot
3月10日 评论:
3月10日 评论:
问题/功能描述
本次PR是一系列代码维护和优化工作的集合,核心目标是统一代码风格、提升健壮性和可维护性。主要解决了日志和错误信息格式化方式不一致的问题,优化了多个模块的导入依赖和异常处理逻辑,并修复了测试脚本中的语法错误和潜在缺陷。此外,对服务监控脚本进行了功能增强,引入了更精确的服务状态判断指标。
修改方案描述
修改方案涉及多个文件和模块,主要包括三个方面:1)代码风格统一:在日志模块、配置管理、故障处理、监控脚本等多个文件中,将大量使用字符串拼接或%格式化的日志输出和错误信息统一改为使用f-string格式化,提升了代码的一致性和可读性。2)代码健壮性增强:优化了多个模块的导入语句(如补充json、ssl、sys等),修复了异常捕获中未使用异常对象的问题,并移除了冗余的__all__声明和未使用的导入。在服务监控脚本中,新增了对num_requests_running指标的监控,使服务状态判断更精确。3)问题修复:修复了测试用例中未定义的变量引用、重复导入以及文件操作语法错误等问题,确保了测试的准确性和脚本的稳定运行。


mindie_motor/python/mindie_motor/node_manager/common/logging.py
@@ -119,10 +119,10 @@ def _create_log_file(log_file):
119119 else:
120120 clean_path = os.path.normpath(log_file)
121121 if os.path.islink(clean_path):
122- err_msg = "Check log file path failed because it's a symbolic."
代码可读性与一致性: 第122行将错误消息从普通字符串改为f-string,但该字符串中没有需要格式化的变量。这种改动增加了不必要的复杂性,与第125行的改动形成了不一致的处理方式。虽然第125行使用f-string是合理的(因为包含了变量clean_path),但第122行没有变量需要插入,使用普通字符串更简洁。这种不一致性会影响代码的可读性和维护性。
问题类型: 代码可读性与一致性
文件路径:
mindie_motor/python/mindie_motor/node_manager/common/logging.py行号: 122
问题代码:
err_msg = f"Check log file path failed because it's a symbolic."
修改建议:
将第122行改回普通字符串格式:err_msg = "Check log file path failed because it's a symbolic.",以保持代码简洁性和一致性。或者,如果团队有统一使用f-string的编码规范,那么第125行也应该相应地调整格式,确保两行代码风格一致。
---
此评论由代码审查工具自动生成
此评论由代码审查工具自动生成


mindie_motor/python/mindie_motor/node_manager/common/logging.py
@@ -123,3 +122,3 @@
122+ err_msg = f"Check log file path failed because it's a symbolic."
123123 raise ValueError(err_msg)
124124 if len(clean_path) > 1024:
125- err_msg = "Path of log file is too long, it should not exceed 1024 character."
错误信息准确性: 第125行的错误消息中使用了单数形式的'character',但实际检查的是路径长度是否超过1024个字符。当路径长度超过限制时,应该使用复数形式'characters'更符合英语语法习惯。虽然这是一个小问题,但在国际化或面向英语用户的项目中,这种细节会影响专业性。
问题类型: 错误信息准确性
文件路径:
mindie_motor/python/mindie_motor/node_manager/common/logging.py行号: 125
问题代码:
err_msg = f"Path of log file is too long, it should not exceed 1024 character."
修改建议:
将错误消息中的'character'改为复数形式'characters':err_msg = f"Path of log file is too long, it should not exceed 1024 characters."
---
此评论由代码审查工具自动生成
此评论由代码审查工具自动生成


mindie_motor/python/mindie_motor/node_manager/core/config.py
@@ -48,2 +48,3 @@
48+ raise KeyError(f"Management Port is Missing in Server Engine Config File")
4849 if "distDPServerEnabled" not in server_config or not isinstance(server_config["distDPServerEnabled"], bool):
49- raise KeyError("distDPServerEnabled is Missing or Invalid in Server Engine Config File")
50+ raise KeyError(f"distDPServerEnabled is Missing or Invalid in Server Engine Config File")
代码可读性: 异常消息使用f-string但未包含变量,与前两行问题相同。原字符串'distDPServerEnabled is Missing or Invalid in Server Engine Config File'已经足够清晰。
问题类型: 代码可读性
文件路径:
mindie_motor/python/mindie_motor/node_manager/core/config.py行号: 50
问题代码:
raise KeyError(f"distDPServerEnabled is Missing or Invalid in Server Engine Config File")
修改建议:
移除f-string前缀:raise KeyError("distDPServerEnabled is Missing or Invalid in Server Engine Config File")---
此评论由代码审查工具自动生成
此评论由代码审查工具自动生成


mindie_motor/python/mindie_motor/node_manager/core/config.py
@@ -56,3 +57,3 @@
5657 address = ipaddress.ip_address(pod_ip)
5758 if address.version != IPV4 and address.version != IPV6:
58- raise Exception('[Config] POD_IP is not ipv4 or ipv6.')
59+ raise Exception(f'[Config] POD_IP is not ipv4 or ipv6.')
安全编码规范: 在异常消息中使用了f-string,但消息内容为静态字符串,没有使用任何变量插值。虽然这里使用了单引号字符串,但同样存在不必要的f-string前缀问题。
问题类型: 安全编码规范
文件路径:
mindie_motor/python/mindie_motor/node_manager/core/config.py行号: 59
问题代码:
raise Exception(f'[Config] POD_IP is not ipv4 or ipv6.')
修改建议:
移除不必要的f-string前缀,改为普通字符串:raise Exception('[Config] POD_IP is not ipv4 or ipv6.')---
此评论由代码审查工具自动生成
此评论由代码审查工具自动生成


mindie_motor/python/mindie_motor/om_adapter/backends/log_collect/log_collector.py
@@ -76,3 +75,3 @@
75+ err_msg = f"[OM Adapter] Init log monitor failed, the collect_path is empty from config.json"
7676 self.logger.error(err_msg)
7777 raise Exception(err_msg)
78- self.logger.info("[OM Adapter] Log monitor path is %s" % collect_path)
代码风格与一致性: 第79行是f-string与%格式化的混合使用,与第45、51、64行是同类问题。这种模式在代码中重复出现,表明需要统一的修复方案。不一致的字符串格式化方式会影响代码的可读性和维护成本。
问题类型: 代码风格与一致性
文件路径:
mindie_motor/python/mindie_motor/om_adapter/backends/log_collect/log_collector.py行号: 78
问题代码:
self.logger.info(f"[OM Adapter] Log monitor path is %s" % collect_path)
修改建议:
改为纯f-string格式:`self.logger.info(f"[OM Adapter] Log monitor path is {collect_path}")`。同时建议检查整个文件中所有字符串格式化的地方,确保风格统一。---
此评论由代码审查工具自动生成
此评论由代码审查工具自动生成


mindie_motor/src/example/deploy_scripts/boot_helper/update_mindie_server_config.py
@@ -80,4 +80,3 @@
8080
8181 base_dir = base_dir if base_dir[-1] == "/" else base_dir + '/'
8282 if not cls.is_base_dir_path(base_dir, real_file_path):
83- err_msg = 'the file path is not in base dir'
代码逻辑和结构: 在错误消息字符串中不必要地使用了f-string,但字符串中没有包含任何变量插值。
问题类型: 代码逻辑和结构
文件路径:
mindie_motor/src/example/deploy_scripts/boot_helper/update_mindie_server_config.py行号: 83
问题代码:
err_msg = f'the file path is not in base dir'
修改建议:
将f-string改回普通字符串:err_msg = 'the file path is not in base dir'
---
此评论由代码审查工具自动生成
此评论由代码审查工具自动生成


mindie_motor/src/example/deploy_scripts/boot_helper/update_mindie_server_config.py
@@ -66,30 +66,29 @@
6666
6767 if len(file_path) > 1024:
68- err_msg = "The file path exceeds the maximum length."
68+ err_msg = f"The file path exceeds the maximum length."
6969 return False, err_msg, None
7070
7171 if not allow_symlink and os.path.islink(file_path):
72- err_msg = "The file path is a link."
72+ err_msg = f"The file path is a link."
7373 return False, err_msg, None
7474
7575 try:
7676 real_file_path = os.path.realpath(file_path)
77- except Exception:
78- err_msg = "Realpath parsing failed"
77+ except Exception as e:
78+ err_msg = f"Realpath parsing failed"
7979 return False, err_msg, None
8080
8181 base_dir = base_dir if base_dir[-1] == "/" else base_dir + '/'
8282 if not cls.is_base_dir_path(base_dir, real_file_path):
83- err_msg = 'the file path is not in base dir'
83+ err_msg = f'the file path is not in base dir'
8484 return False, err_msg, None
8585
8686 return True, None, real_file_path
@@ -101,7 +101,7 @@ class _FileUtils:
101101 """
102102 # Check if the file exists
103103 if not cls.check_file_exists(file_path):
104- err_msg = "Error: File not found."
代码逻辑和结构: 在错误消息字符串中不必要地使用了f-string,但字符串中没有包含任何变量插值。
问题类型: 代码逻辑和结构
文件路径:
mindie_motor/src/example/deploy_scripts/boot_helper/update_mindie_server_config.py行号: 104
问题代码:
err_msg = f"Error: File not found."
修改建议:
将f-string改回普通字符串:err_msg = "Error: File not found."
---
此评论由代码审查工具自动生成
此评论由代码审查工具自动生成


mindie_motor/src/example/deploy_scripts/boot_helper/update_mindie_server_config.py
@@ -73,30 +73,29 @@
7373 return False, err_msg, None
7474
7575 try:
7676 real_file_path = os.path.realpath(file_path)
77- except Exception:
78- err_msg = "Realpath parsing failed"
77+ except Exception as e:
78+ err_msg = f"Realpath parsing failed"
7979 return False, err_msg, None
8080
8181 base_dir = base_dir if base_dir[-1] == "/" else base_dir + '/'
8282 if not cls.is_base_dir_path(base_dir, real_file_path):
83- err_msg = 'the file path is not in base dir'
83+ err_msg = f'the file path is not in base dir'
8484 return False, err_msg, None
8585
8686 return True, None, real_file_path
@@ -101,7 +101,7 @@ class _FileUtils:
101101 """
102102 # Check if the file exists
103103 if not cls.check_file_exists(file_path):
104- err_msg = "Error: File not found."
104+ err_msg = f"Error: File not found."
105105 return False, err_msg
106106
107107 # Get the real_file_path
@@ -118,7 +118,7 @@ class _FileUtils:
118118 # Get the file size
119119 file_size = fp.tell()
120120 if file_size < DEFAULT_MIN_FILE_SIZE or file_size > DEFAULT_MAX_FILE_SIZE:
121- err_msg = "Read input file failed, file size is invalid"
代码逻辑和结构: 在错误消息字符串中不必要地使用了f-string,但字符串中没有包含任何变量插值。
问题类型: 代码逻辑和结构
文件路径:
mindie_motor/src/example/deploy_scripts/boot_helper/update_mindie_server_config.py行号: 121
问题代码:
err_msg = f"Read input file failed, file size is invalid"
修改建议:
将f-string改回普通字符串:err_msg = "Read input file failed, file size is invalid"
---
此评论由代码审查工具自动生成
此评论由代码审查工具自动生成


mindie_motor/src/example/deploy_scripts/boot_helper/update_mindie_server_config.py
@@ -80,30 +80,29 @@
8080
8181 base_dir = base_dir if base_dir[-1] == "/" else base_dir + '/'
8282 if not cls.is_base_dir_path(base_dir, real_file_path):
83- err_msg = 'the file path is not in base dir'
83+ err_msg = f'the file path is not in base dir'
8484 return False, err_msg, None
8585
8686 return True, None, real_file_path
@@ -101,7 +101,7 @@ class _FileUtils:
101101 """
102102 # Check if the file exists
103103 if not cls.check_file_exists(file_path):
104- err_msg = "Error: File not found."
104+ err_msg = f"Error: File not found."
105105 return False, err_msg
106106
107107 # Get the real_file_path
@@ -118,7 +118,7 @@ class _FileUtils:
118118 # Get the file size
119119 file_size = fp.tell()
120120 if file_size < DEFAULT_MIN_FILE_SIZE or file_size > DEFAULT_MAX_FILE_SIZE:
121- err_msg = "Read input file failed, file size is invalid"
121+ err_msg = f"Read input file failed, file size is invalid"
122122 return False, err_msg
123123 return True, None
124124 except Exception as e:
@@ -130,7 +130,7 @@ class _FileUtils:
130130 try:
131131 file_stat = os.stat(file_path)
132132 except FileNotFoundError:
133- err_msg = "Error: File not found."
代码逻辑和结构: 在错误消息字符串中不必要地使用了f-string,但字符串中没有包含任何变量插值。
问题类型: 代码逻辑和结构
文件路径:
mindie_motor/src/example/deploy_scripts/boot_helper/update_mindie_server_config.py行号: 133
问题代码:
err_msg = f"Error: File not found."
修改建议:
将f-string改回普通字符串:err_msg = "Error: File not found."
---
此评论由代码审查工具自动生成
此评论由代码审查工具自动生成


mindie_motor/src/example/deploy_scripts/boot_helper/update_mindie_server_config.py
@@ -101,30 +101,29 @@
101101 """
102102 # Check if the file exists
103103 if not cls.check_file_exists(file_path):
104- err_msg = "Error: File not found."
104+ err_msg = f"Error: File not found."
105105 return False, err_msg
106106
107107 # Get the real_file_path
@@ -118,7 +118,7 @@ class _FileUtils:
118118 # Get the file size
119119 file_size = fp.tell()
120120 if file_size < DEFAULT_MIN_FILE_SIZE or file_size > DEFAULT_MAX_FILE_SIZE:
121- err_msg = "Read input file failed, file size is invalid"
121+ err_msg = f"Read input file failed, file size is invalid"
122122 return False, err_msg
123123 return True, None
124124 except Exception as e:
@@ -130,7 +130,7 @@ class _FileUtils:
130130 try:
131131 file_stat = os.stat(file_path)
132132 except FileNotFoundError:
133- err_msg = "Error: File not found."
133+ err_msg = f"Error: File not found."
134134 return False, err_msg
135135 except PermissionError:
136136 err_msg = f"Error: Permission denied to access file: {file_path}"
@@ -160,7 +160,7 @@ class _FileUtils:
160160 try:
161161 file_stat = os.stat(file_path)
162162 except FileNotFoundError:
163- err_msg = "Error: File not found."
代码逻辑和结构: 在错误消息字符串中不必要地使用了f-string,但字符串中没有包含任何变量插值。
问题类型: 代码逻辑和结构
文件路径:
mindie_motor/src/example/deploy_scripts/boot_helper/update_mindie_server_config.py行号: 163
问题代码:
err_msg = f"Error: File not found."
修改建议:
将f-string改回普通字符串:err_msg = "Error: File not found."
---
此评论由代码审查工具自动生成
此评论由代码审查工具自动生成


Revert "修复ruff-check检查出来的规范性问题"