You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python规则匹配失败,如何匹配指定属性类中的_the_best方法

问题:无法匹配指定类中的特定方法

原YAML匹配规则无法命中目标Python代码中符合要求的方法:

id: match-specific-function
language: python
rule:
  kind: function_definition
  inside:
    kind: class_definition
    regex: account.account

需求说明

需要匹配**类定义中包含_name = 'account.account'或_inherit = 'account.account'**的类内部,名称为_the_best的方法,仅命中如下格式的方法:

def _the_best(self):
    pass

原规则问题分析

原规则直接对class_definition使用regex: account.account,无法精准定位类中的_name/_inherit属性赋值,匹配逻辑过于宽泛且不准确,导致无法命中目标内容。

修正后的匹配规则

id: match-specific-function
language: python
rule:
  kind: function_definition
  name: _the_best
  inside:
    kind: class_definition
    any:
      - has:
          kind: assign
          left:
            kind: name
            name: _name
          right:
            kind: string
            value: "account.account"
      - has:
          kind: assign
          left:
            kind: name
            name: _inherit
          right:
            kind: string
            value: "account.account"
      - has:
          kind: assign
          left:
            kind: name
            name: _inherit
          right:
            kind: list
            any:
              kind: string
              value: "account.account"

规则说明

  • 明确匹配名称为_the_best的函数定义(function_definition)
  • 限定函数必须在满足以下任一条件的类(class_definition)内部:
    1. 类中存在_name = "account.account"的赋值语句
    2. 类中存在_inherit = "account.account"的赋值语句
    3. 类中存在_inherit为列表且包含"account.account"的赋值(适配Odoo多继承场景)

此规则可精准命中目标代码中3个符合要求的_the_best方法,排除全局函数及_name="sale.order"类中的方法。

内容的提问来源于stack exchange,提问作者kerbrose

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 19:53:15