Skip to content

Conversation

@Soulter
Copy link
Member

@Soulter Soulter commented Feb 3, 2026

fixes: #4777

Modifications / 改动点

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果


Checklist / 检查清单

  • 😊 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。/ If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
  • 👀 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。/ My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
  • 🤓 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到了 requirements.txtpyproject.toml 文件相应位置。/ 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.
  • 😮 我的更改没有引入恶意代码。/ My changes do not introduce malicious code.

由 Sourcery 提供的总结

修复自定义过滤器的验证和注册逻辑,避免在组合或注册它们时出现运行时错误。

错误修复:

  • 更正自定义过滤器操作数和注册的类型检查,使用标准的 isinstance 元组形式,从而在使用自定义过滤器时防止无效参数错误。

增强项:

  • 修复无效自定义过滤器组合的错误消息文本,使其正确引用 CustomFilter 类。
Original summary in English

Summary by Sourcery

Fix validation and registration of custom filters to avoid runtime errors when composing or registering them.

Bug Fixes:

  • Correct type checks for custom filter operands and registration to use standard isinstance tuple form, preventing invalid argument errors when using custom filters.

Enhancements:

  • Fix the error message text for invalid custom filter combinations to correctly reference the CustomFilter class.

@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Feb 3, 2026
@dosubot
Copy link

dosubot bot commented Feb 3, 2026

Related Documentation

Checked 1 published document(s) in 1 knowledge base(s). No updates required.

How did I do? Any feedback?  Join Discord

Copy link
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 - 我发现了 1 个问题,并且留下了一些整体性的反馈:

  • CustomFilterAnd.__init__ 中的错误信息仍然包含拼写错误 CustomFilter lass;请将其与 CustomFilterOr 中已修正的错误信息保持一致。
  • 考虑将 CustomFilterOrCustomFilterAnd 中重复的 isinstance 逻辑(以及可能在 register_custom_filter 中的检查)抽取到一个共享的辅助函数或公共的基础校验逻辑中,以避免重复并降低未来出现不一致的风险。
供 AI 代理使用的提示词
Please address the comments from this code review:

## Overall Comments
- The error message in `CustomFilterAnd.__init__` still contains the typo `CustomFilter lass`; align it with the corrected message used in `CustomFilterOr`.
- Consider consolidating the repeated isinstance logic for `CustomFilterOr` and `CustomFilterAnd` (and possibly the check in `register_custom_filter`) into a shared helper or a common base validation to avoid duplication and reduce the risk of future inconsistencies.

## Individual Comments

### Comment 1
<location> `astrbot/core/star/filter/custom_filter.py:40` </location>
<code_context>
     def __init__(self, filter1: CustomFilter, filter2: CustomFilter):
         super().__init__()
-        if not isinstance(filter1, CustomFilter | CustomFilterAnd | CustomFilterOr):
+        if not isinstance(filter1, (CustomFilter, CustomFilterAnd, CustomFilterOr)):
             raise ValueError(
-                "CustomFilter lass can only operate with other CustomFilter.",
</code_context>

<issue_to_address>
**suggestion (bug_risk):** Only `filter1` is validated, which may allow unsupported types in `filter2`.

Both `CustomFilterOr` and `CustomFilterAnd` validate only `filter1`. If `filter2` should meet the same constraints, apply the same `isinstance` check there to prevent invalid filter combinations at runtime.

Suggested implementation:

```python
        if not isinstance(filter1, (CustomFilter, CustomFilterAnd, CustomFilterOr)) or not isinstance(
            filter2, (CustomFilter, CustomFilterAnd, CustomFilterOr)
        ):

```

```python
        if not isinstance(filter1, (CustomFilter, CustomFilterAnd, CustomFilterOr)) or not isinstance(
            filter2, (CustomFilter, CustomFilterAnd, CustomFilterOr)
        ):

```
</issue_to_address>

Sourcery 对开源项目是免费的——如果你觉得这次评审有帮助,欢迎分享给更多人 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据这些反馈改进后续的评审。
Original comment in English

Hey - I've found 1 issue, and left some high level feedback:

  • The error message in CustomFilterAnd.__init__ still contains the typo CustomFilter lass; align it with the corrected message used in CustomFilterOr.
  • Consider consolidating the repeated isinstance logic for CustomFilterOr and CustomFilterAnd (and possibly the check in register_custom_filter) into a shared helper or a common base validation to avoid duplication and reduce the risk of future inconsistencies.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The error message in `CustomFilterAnd.__init__` still contains the typo `CustomFilter lass`; align it with the corrected message used in `CustomFilterOr`.
- Consider consolidating the repeated isinstance logic for `CustomFilterOr` and `CustomFilterAnd` (and possibly the check in `register_custom_filter`) into a shared helper or a common base validation to avoid duplication and reduce the risk of future inconsistencies.

## Individual Comments

### Comment 1
<location> `astrbot/core/star/filter/custom_filter.py:40` </location>
<code_context>
     def __init__(self, filter1: CustomFilter, filter2: CustomFilter):
         super().__init__()
-        if not isinstance(filter1, CustomFilter | CustomFilterAnd | CustomFilterOr):
+        if not isinstance(filter1, (CustomFilter, CustomFilterAnd, CustomFilterOr)):
             raise ValueError(
-                "CustomFilter lass can only operate with other CustomFilter.",
</code_context>

<issue_to_address>
**suggestion (bug_risk):** Only `filter1` is validated, which may allow unsupported types in `filter2`.

Both `CustomFilterOr` and `CustomFilterAnd` validate only `filter1`. If `filter2` should meet the same constraints, apply the same `isinstance` check there to prevent invalid filter combinations at runtime.

Suggested implementation:

```python
        if not isinstance(filter1, (CustomFilter, CustomFilterAnd, CustomFilterOr)) or not isinstance(
            filter2, (CustomFilter, CustomFilterAnd, CustomFilterOr)
        ):

```

```python
        if not isinstance(filter1, (CustomFilter, CustomFilterAnd, CustomFilterOr)) or not isinstance(
            filter2, (CustomFilter, CustomFilterAnd, CustomFilterOr)
        ):

```
</issue_to_address>

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.

def __init__(self, filter1: CustomFilter, filter2: CustomFilter):
super().__init__()
if not isinstance(filter1, CustomFilter | CustomFilterAnd | CustomFilterOr):
if not isinstance(filter1, (CustomFilter, CustomFilterAnd, CustomFilterOr)):
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (bug_risk): 目前只对 filter1 进行了校验,这可能会让不受支持的类型出现在 filter2 中。

CustomFilterOrCustomFilterAnd 都只会校验 filter1。如果 filter2 也应满足相同约束,请在它上面应用同样的 isinstance 检查,以避免在运行时出现无效的过滤器组合。

建议实现:

        if not isinstance(filter1, (CustomFilter, CustomFilterAnd, CustomFilterOr)) or not isinstance(
            filter2, (CustomFilter, CustomFilterAnd, CustomFilterOr)
        ):
        if not isinstance(filter1, (CustomFilter, CustomFilterAnd, CustomFilterOr)) or not isinstance(
            filter2, (CustomFilter, CustomFilterAnd, CustomFilterOr)
        ):
Original comment in English

suggestion (bug_risk): Only filter1 is validated, which may allow unsupported types in filter2.

Both CustomFilterOr and CustomFilterAnd validate only filter1. If filter2 should meet the same constraints, apply the same isinstance check there to prevent invalid filter combinations at runtime.

Suggested implementation:

        if not isinstance(filter1, (CustomFilter, CustomFilterAnd, CustomFilterOr)) or not isinstance(
            filter2, (CustomFilter, CustomFilterAnd, CustomFilterOr)
        ):
        if not isinstance(filter1, (CustomFilter, CustomFilterAnd, CustomFilterOr)) or not isinstance(
            filter2, (CustomFilter, CustomFilterAnd, CustomFilterOr)
        ):

@Soulter Soulter merged commit 387bba0 into master Feb 3, 2026
6 checks passed
@dosubot dosubot bot added the area:core The bug / feature is about astrbot's core, backend label Feb 3, 2026
@Soulter Soulter deleted the fix/4777 branch February 3, 2026 12:37
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 size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] v4.13.1 自定义过滤器注册时使用 A | B 进行 isinstance 判断触发 __or__,导致插件导入失败

2 participants