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)内部:- 类中存在
_name = "account.account"的赋值语句 - 类中存在
_inherit = "account.account"的赋值语句 - 类中存在
_inherit为列表且包含"account.account"的赋值(适配Odoo多继承场景)
- 类中存在
此规则可精准命中目标代码中3个符合要求的_the_best方法,排除全局函数及_name="sale.order"类中的方法。
内容的提问来源于stack exchange,提问作者kerbrose
相关产品推荐
相关产品推荐

