How to resolve attribute reference for inherited objects in Python?

Question:

I would like to have a proper Python typing for the setup I have created.

The issue I have is connected with class B, in which my IDE (pyCharm) reports unresolved attribute reference.
However, this setup is working fine.

class ConfigA:
    def __init__(self):
        self.param1: int = 0


class ConfigB(ConfigA):
    def __init__(self):
        super().__init__()
        self.param2: int = 1


class A:
    def __init__(self, config: ConfigA):
        self.config: ConfigA = config
        self.do_basic_stuff()

    def do_basic_stuff(self):
        print(self.config.param1)


class B(A):
    def __init__(self, config: ConfigB):
        super().__init__(config)

    def do_advanced_stuff(self):
        # Unresolved attribute reference 'param2' for class 'ConfigA'
        print(self.config.param2)


if __name__ == "__main__":
    b = B(ConfigB())
    b.do_advanced_stuff()

Is there a way to properly set the typing that the IDE would recognise that the object self.config is from specialised ConfigB class?

Asked By: mlokos

||

Answers:

You can just override the type of self.config in B:

class B(A):
    def __init__(self, config: ConfigB):
        self.config: ConfigB
        super().__init__(config)

This works fine in mypy and pyright (which flag the original error, using --check-untyped-defs for mypy), and executes correctly.

For a somewhat more usual typing scheme, you could also lift both to the classes:

class A:
    config: ConfigA
    def __init__(self, config: ConfigA):
        self.config = config
        self.do_basic_stuff()

    def do_basic_stuff(self):
        print(self.config.param1)


class B(A):
    config: ConfigB
    def __init__(self, config: ConfigB):
        super().__init__(config)

    def do_advanced_stuff(self):
        # Unresolved attribute reference 'param2' for class 'ConfigA'
        print(self.config.param2)

Same with ConfigA and ConfigB, putting the type annotations for instance variables inside the methods is non-standard.

Answered By: Masklinn
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.