Python: 3.7+
I have a dataclass and a subclass of it as following:
from abc import ABC
from dataclasses import dataclass
from typing import Dict, List, Optional
from dbconn import DBConnector
@dataclass
class User:
uid: int
name: str
@dataclass
class Model(ABC):
database: DBConnector
user: User
def func(self, *args, **kwargs):
pass
@dataclass
class Command(Model):
message: Optional[str] = "Hello"
def __post_init__(self):
self.user_id: str = str(self.user.uid)
self.message = f"{self.user.name}: {self.message}"
I could get the type hint for database
, user
and message
using typing.get_type_hints(Command)
.
How can I get the type hints for user_id
?
One workaround would to be pass in the user.uid and user.name as separate params to Command but that's not pragmatic when User object has many useful attributes.
I believe the reason why it doesn't work in the first place is because init gets called at runtime and that's why type checking doesn't take those attrs into account. One possible solution would be to parse the ast of the class but I'm not sure if that's recommended and generic enough. If yes, would appreciate a working example.