关于未解析的属性引用的 Pycharm 视觉警告 [英] Pycharm visual warning about unresolved attribute reference

查看:69
本文介绍了关于未解析的属性引用的 Pycharm 视觉警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个看起来像这样的类:

I have two classes that look like this:

class BaseClass(object):

    def the_dct(self):
        return self.THE_DCT


class Kid(BaseClass):

    THE_DCT = {'vars': 'values'}


# Code i ll be running
inst = Kid()
print(inst.the_dct)

继承必须是这样;第二个类包含 THE_DCT,第一个类包含 def the_dct.

Inheritance has to be this way; second class containing THE_DCT and first class containing def the_dct.

它工作得很好,但我的问题是我在 Pycharm 中收到一个警告(未解析的属性引用),关于 BaseClass 中的 THE_DCT.

It works just fine, but my problem is that i get a warning in Pycharm (unresolved attribute reference), about THE_DCT in BaseClass.

  • 它是否有理由警告我(例如为什么我应该避免它)?
  • 有什么我应该做的不同的事情吗?

推荐答案

BaseClass 中,你引用了 self.THE_DCT,但是当 PyCharm 查看这个类时,它看到THE_DCT 不存在.

Within BaseClass you reference self.THE_DCT, yet when PyCharm looks at this class, it sees that THE_DCT doesn't exist.

假设您将其视为抽象类,PyCharm 不知道这是您的意图.它所看到的只是一个访问属性的类,该属性不存在,因此它会显示警告.

Assuming you are treating this as an Abstract Class, PyCharm doesn't know that that is your intention. All it sees is a class accessing an attribute, which doesn't exist, and therefore it displays the warning.

虽然你的代码会运行得很好(只要你不实例化BaseClass),你真的应该把它改成:

Although your code will run perfectly fine (as long as you never instantiate BaseClass), you should really change it to:

class BaseClass(object):
    THE_DCT = {}

    def the_dct(self):
        return self.THE_DCT

这篇关于关于未解析的属性引用的 Pycharm 视觉警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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