Skip to content

fix: accept both str and re.Pattern in RegexFilter#7633

Open
ShadowLemoon wants to merge 2 commits intoAstrBotDevs:masterfrom
ShadowLemoon:fix/regex-filter-accept-pattern
Open

fix: accept both str and re.Pattern in RegexFilter#7633
ShadowLemoon wants to merge 2 commits intoAstrBotDevs:masterfrom
ShadowLemoon:fix/regex-filter-accept-pattern

Conversation

@ShadowLemoon
Copy link
Copy Markdown

@ShadowLemoon ShadowLemoon commented Apr 17, 2026

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__ 现在同时接受 strre.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.
    / 我的更改没有引入恶意代码。

RegexFilter.__init__ now handles compiled re.Pattern objects by
extracting .pattern for regex_str, preventing TypeError during
JSON serialization in the dashboard plugin API.
@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend feature:plugin The bug / feature is about AstrBot plugin system. labels Apr 17, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread astrbot/core/star/filter/regex.py
Comment thread astrbot/core/star/filter/regex.py Outdated
@dosubot dosubot bot added size:XS This PR changes 0-9 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend feature:plugin The bug / feature is about AstrBot plugin system. size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant