Amaranth
Amaranth

Reputation: 53

Unresolved Reference Warning in Docstring - Python 3.6 - Pycharm 17.1.4

I received this warning from PyCharm about an unresolved reference. Here's code that is structured similarly, and should receive a warning as well.

class Parent:
    """
    === Attributes ===
    @type public_attribute_1: str
    """

    def __init__(self):
        public_attribute_1 = ''
    pass

class Child(Parent):
    """
    === Attributes ===
    @type public_attribute_1: str
        > Unresolved reference 'public_attribute_1'
    @type public_attribute_2: str
    """

    def __init__(self):
        Parent.__init__(self)
        public_attribute_2 = ''

    pass

I understand that public_attribute_1 is not explicitly initiated in Child.__init__(), but Child.__init__() calls Parent.__init__(self) which initiates public_attribute_1. Thus the error raised concerns readability and documentation more than functionality.

How can I make such code more readable, without inserting redundancies, thereby defeating the whole point of inheritance? Would it be sufficient to document thoroughly via docstrings and comments, and just ignore the warning from PyCharm? Or is there a pythonic way of doing it?

Upvotes: 0

Views: 1039

Answers (1)

nanotek
nanotek

Reputation: 3087

There are a number of issues here.

  1. In Python 3 use super()

  2. You are calling them "attributes", but this isn't how attributes work in Python. Glossing over details, use self.

Your question seems to be in regards to you want the docstring of Child to have all the attributes of Parent re-defined. This is pretty dangerous from a maintainability perspective though if anything ever changes. When I see a class inherits Parent, if I'm not familiar with Parent, I will go to the definition of Parent (which PyCharm makes easy with Ctrl + B).

I'll let someone else say if that's truly pythonic, but that's the way I'm used to working. Regardless, to fix your inheritance, your code should look more like this:

class Parent:
    """
    === Attributes ===
    @type public_attribute_1: str
    """

    def __init__(self):
        self.public_attribute_1 = ''


class Child(Parent):
    """
    === Attributes ===
    @type public_attribute_2: str
    """

    def __init__(self):
        super().__init__()
        self.public_attribute_2 = ''
        print(self.public_attribute_1)

Upvotes: 1

Related Questions