使用访问受保护成员的属性的Pylint警告“W0212”:如何避免?

时间:2022-05-24 16:46:01

Pylint warns for suspicious access to object's protected members. It know how to not warn when the access is from the object it‑self, however does not know how to not warn when the access is from a property of the object.

Pylint警告要对对象的受保护成员进行可疑访问。它知道如何在访问来自它自己的对象时不发出警告,但是不知道如何在访问来自对象的属性时不发出警告。

Ex.

前女友。

class C(object):

    def __init__(self):
        C.__a = 0

    a = property(lambda self: self.__a)

Pylint tells “W0212 (protected-access): Access to a protected member __a of a client class”

Pylint告诉“W0212(受保护访问):对客户端类受保护成员__a的访问”

I don't want to globally disable W0212 and I am not happy with repeatedly disabling it locally (*) for each such property definition.

我不想在全球范围内禁用W0212我不满意反复禁用它本地 (*)对于每一次这样的属性定义。

Is there a known way to work around this?

有没有一种已知的方法来解决这个问题?

(*) As in:

(*) 如:

class C(object):

    def __init__(self):
        C.__a = 0

    a = property(lambda self: self.__a)  # pylint: disable=W0212

Margin notes

As an interesting side note, the answer I selected presents an additional benefit with the actual Pylint (may change in future versions, I can't tell): it preserves Pylint ability to check for non‑existent members, as this test shows:

作为一个有趣的补充说明,我选择的答案对实际的Pylint有额外的好处(我不能说将来的版本可能会有所改变):它保留了Pylint检查不存在的成员的能力,正如这个测试所显示的:

class C1(object):

    member = 0


class C2(object):

    def __init__(self):
        self.__a = C1()

    def a(self):
        return self.__a

    @property
    def b(self):
        return self.__a

    c = property(lambda self: self.__a)


def test_member():

    o = C2()

    print(o.a().member)
    print(o.b.member)
    print(o.c.member)


def test_nonexistent():

    o = C2()

    print(o.a().nonexistent)
    print(o.b.nonexistent)
    print(o.c.nonexistent)

You will get a warning for print(o.a().nonexistent) and print(o.b.nonexistent) but not for print(o.c.nonexistent).

您将得到一个关于print(o.a(.不存在)和print(o.b.不存在)的警告,但对于print(o.c.不存在)则不会。

1 个解决方案

#1


5  

Seems to me that you could use a decorator and the linter probably wouldn't complain:

在我看来,你可以用一个装饰师,而linter可能不会抱怨:

class C(object):

    def __init__(self):
        self.__a = 0

    @property
    def a(self):
        return self.__a

    # You can use the decorator to create setters too...
    @a.setter
    def a(self, value):
        self.__a = value

because then the linter can easily identify a as a method on the class. Otherwise, you're pretty much stuck with the options you listed. You can either disable a warning globally or locally or not at all -- There's no way to disable the warning only in a specific context as far as I know.

因为这样linter就可以很容易地将a标识为类上的一个方法。否则,你会被你列出的选项所困扰。您可以在全局或局部禁用警告,或者根本不禁用警告——就我所知,没有办法只在特定的上下文中禁用警告。

#1


5  

Seems to me that you could use a decorator and the linter probably wouldn't complain:

在我看来,你可以用一个装饰师,而linter可能不会抱怨:

class C(object):

    def __init__(self):
        self.__a = 0

    @property
    def a(self):
        return self.__a

    # You can use the decorator to create setters too...
    @a.setter
    def a(self, value):
        self.__a = value

because then the linter can easily identify a as a method on the class. Otherwise, you're pretty much stuck with the options you listed. You can either disable a warning globally or locally or not at all -- There's no way to disable the warning only in a specific context as far as I know.

因为这样linter就可以很容易地将a标识为类上的一个方法。否则,你会被你列出的选项所困扰。您可以在全局或局部禁用警告,或者根本不禁用警告——就我所知,没有办法只在特定的上下文中禁用警告。