user
user

Reputation: 5696

Pycharm visual warning about unresolved attribute reference

I have two classes that look like this:

class BaseClass:

    def the_dct(self):
        return self.THE_DCT


class Kid(BaseClass):

    THE_DCT = {'key': 'value'}


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

Inheritance has to be this way; second class containing THE_DCT and first class containing def 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.

Upvotes: 39

Views: 124000

Answers (3)

Donald Duck
Donald Duck

Reputation: 692

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

I'd say this is conceptually wrong: a base class cannot use (know about) fields(methods) in a subclass.

The fact that this works in Python is mind-boggling. :)

Upvotes: -1

Andrew
Andrew

Reputation: 170

In addition to the existing answers, or as an alternative, you can use Type Hints. This satisfies PyCharm's warnings and also distinguishes the attribute as being inherited (or at least not native to the class). It's as simple as adding THE_DCT: dict at the very top of your class (before anything else).

class BaseClass(object):
    THE_DCT: dict  # Add a type-hint at the top of the class, before anything else

    def the_dct(self):
        return self.THE_DCT


class Kid(BaseClass):
    THE_DCT = {'vars': 'values'}

I prefer this approach because it negates the need to unnecessarily add a placeholder attribute (self.THE_DCT = {}) and, because it's visually different than declaring an attribute, it can also negate the need for adding a comment next to the placeholder attribute to explain that it's inherited.

Upvotes: 11

dursk
dursk

Reputation: 4445

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

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.

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

Upvotes: 25

Related Questions