Pycharm visual warning about unresolved attribute reference

67,882

Solution 1

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

Solution 2

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.

Share:
67,882
user
Author by

user

My code All of the code I've written and contributed to Stack Exchange is dedicated to the public domain, or (at your option) available under the terms of CC0. This of course doesn't include code which I posted without being its owner. You can distinguish that code by, for example, the attribution notices (sometimes when answering I might be using the OP's code without explicit attribution but it should be evident) .

Updated on December 25, 2021

Comments

  • user
    user over 2 years

    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)
    

    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.

    • Is there a reason why it's warning me (as in why i should avoid it)?
    • Is there something i should do differently?
  • user
    user over 9 years
    you should really change it to What problems might i have if i dont change it? Also, would defining THE_DCT in BaseClass reduce performance (perhaps a tiny reduction) unnecessarily?
  • dursk
    dursk over 9 years
    This has nothing to do with performance. Don't ever write bad code because of "performance."
  • user
    user over 9 years
    i understand that i should avoid optimizing prematurely but i m curious as to whether it will have an effect (even a tiny one) or not. PS: (my performance related question is irrelevant of my Topic question)
  • Admin
    Admin over 5 years
    Yes. Do not write code that relies on inheritance to find undefined variables. It is very bad practice. Define the variables as placeholders and comment how inheritance will replace such definition. @Fermiparadox
  • Rob
    Rob over 2 years
    I agree that type hints are the best way to handle these warnings.