fix: 优化__str__输出格式 #58
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
问题描述
ChatbotMessage.__str__()
直接返回原始字符串内容,未通过repr()
处理,导致:""
和不存在内容。\n
、\t
)显示为实际控制字符,而非转义形式,降低日志可读性。建议修复
遵循 Python 惯例:
__str__
: 返回友好字符串(可保持当前行为,但建议至少包裹引号)。__repr__
: 返回明确表示,如ChatbotMessage(content={self.content!r}, ...)
。当前方法
改进方法
另外,Python 中
__str__
和__repr__
的调用规则如下:str(obj)
会先尝试调用__str__
,如果没有则使用__repr__
作为后备repr(obj)
直接调用__repr__
,永远不会调用__str__
因此建议定义
ChatbotMessage
的__repr__
方法,以提供更明确的表示方式。另外,我的formatter自动优化了代码里的一些格式