Home » Python » python – Properly annotating my variable in a big conditional statement based on its specific type-Exceptionshub

python – Properly annotating my variable in a big conditional statement based on its specific type-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I have the following classes:

@dataclass
class Node:
    def token(self) 
        ...

@dataclass
class Program(Node):
    statements: List[str]

@dataclass
class Statement(Node):
    left: Node

...

They is a fair amount of them but all in one or the other way inherit from Node.

In a later point in my code I am trying

def run(node: Node):
    if type(node) == Program:
        x = node.statements
    elif type(node) == Statement:
        x = node.left

Pycharm is complaining for Unresolved attribute reference 'statements' for class 'Node'.

How can I correctly hint the specific node type in each case?

How to&Answers: