带有访问受保护成员的属性的 Pylint 警告“W0212":如何避免? [英] Pylint warning `W0212` with properties accessing a protected member: how to avoid?

查看:85
本文介绍了带有访问受保护成员的属性的 Pylint 警告“W0212":如何避免?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Pylint 警告对对象受保护成员的可疑访问.当访问来自对象本身时,它知道如何不警告,但不知道当访问来自对象的属性时如何不警告.

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.

例如

class C(object):

    def __init__(self):
        C.__a = 0

    a = property(lambda self: self.__a)

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

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

我不想全局禁用 W0212 并且我不满意为每个这样的属性定义在本地重复禁用它 (*).

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

有没有已知的方法可以解决这个问题?

Is there a known way to work around this?

(*)如:

class C(object):

    def __init__(self):
        C.__a = 0

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

边注

作为一个有趣的旁注,我选择的答案为实际的 Pylint 带来了额外的好处(在未来的版本中可能会改变,我不知道):它保留了 Pylint 检查不存在成员的能力,因为这个测试显示:

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:

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)

您将收到 print(oa().nonexistent)print(obnonexistent) 的警告,但不会收到 print(ocnonexistent) 的警告代码>.

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

推荐答案

在我看来,您可以使用装饰器,而 linter 可能不会抱怨:

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

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

因为这样 linter 可以很容易地将 a 识别为类上的方法.否则,您几乎无法使用您列出的选项.您可以在全局或本地禁用警告,也可以根本不禁用 - 据我所知,没有办法仅在特定上下文中禁用警告.

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.

这篇关于带有访问受保护成员的属性的 Pylint 警告“W0212":如何避免?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆