fix: accept both str and re.Pattern in RegexFilter#7633
Open
ShadowLemoon wants to merge 2 commits intoAstrBotDevs:masterfrom
Open
fix: accept both str and re.Pattern in RegexFilter#7633ShadowLemoon wants to merge 2 commits intoAstrBotDevs:masterfrom
ShadowLemoon wants to merge 2 commits intoAstrBotDevs:masterfrom
Conversation
RegexFilter.__init__ now handles compiled re.Pattern objects by extracting .pattern for regex_str, preventing TypeError during JSON serialization in the dashboard plugin API.
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the RegexFilter class and the register_regex function to support both strings and pre-compiled re.Pattern objects. The review feedback suggests adding from future import annotations to ensure compatibility with Python versions older than 3.10 when using the | operator for type unions. Additionally, it is recommended to simplify the initialization logic in RegexFilter, as re.compile() can directly handle both strings and existing pattern objects.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
RegexFilter.__init__现在可以处理编译后的re.Pattern对象,通过提取.pattern确保regex_str始终为字符串,防止 Dashboard 插件 API 在 JSON 序列化时出现TypeError。RegexFilter.__init__原本只接受str类型的regex参数,但用户可以通过@filter.regex(re.compile(...))传入编译后的re.Pattern对象。虽然re.compile()在 Python 3.7+ 中可以静默接受 Pattern 输入,但原始 Pattern 对象会被直接存储在self.regex_str中。当 Dashboard 的/api/plugin/get接口尝试 JSON 序列化所有插件的 handler 信息时,re.Pattern不可序列化,导致TypeError。由于get_plugins将所有插件的 handler 信息放在单个响应中序列化,一个插件的regex_str类型错误会导致整个插件管理 API 崩溃。这个bug引起插件面板用不了的同时,插件本身功能反而是可以正常使用的,所以排查有点费劲
Modifications / 改动点
regex.py:
RegexFilter.__init__现在同时接受str和re.Pattern,对编译后的正则提取.pattern属性,确保regex_str始终为字符串。star_handler.py:更新
register_regex的类型注解为str | re.Pattern。This is NOT a breaking change. / 这不是一个破坏性变更。
Screenshots or Test Results / 运行截图或测试结果
Ruff check 和 format 检查均通过。
Checklist / 检查清单
My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了验证步骤和运行截图。
I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
/ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txt 和 pyproject.toml 文件相应位置。
My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。