像静态方法一样定义自定义装饰器

Define custom decorator like staticmethod

我使用 factory-boy package and pylint 进行静态检查。对于以下代码,linter 会发出 no-self-argument 错误。

import factory
from factory import Factory, Faker

class MyTestFactory(Factory):
    class Meta:
        model = dict

    a = Faker("pyint")
    b = Faker("pyint")

    @factory.lazy_attribute
    def a_and_b(obj):  # <-- no-self-argument here
        return obj.a + obj.b

if __name__ == "__main__":
    O1 = MyTestFactory.build()
    print(f"dbg: {O1=}")

example2.py:12:4: E0213: Method should have "self" as first argument (no-self-argument)

我不想完全隐藏消息。但是相反,我想告诉 pylint @factory.lazy_attribute 装饰器的行为就像 @staticmethod 内置函数一样,因此该方法需要一个参数。可能吗? pylintrc中是否有专门负责静态方法声明的设置?

根据 doc:

This decorates an instance method that should take a single argument, self; the name of the method will be used as the name of the attribute to fill with the return value of the method:

这意味着你应该将你的论点命名为 self 而不是 obj