Which type hint should I give to an attribute which changes type in the __post_init__
method?
In the below example the argument passed to the class instance is of type int
. However, it gets converted to type str
. What is the correct type hint to show?
from dataclasses import dataclass
@dataclass
class Base:
apples: int # Should the type hint be 'int' OR 'int | str'
def __post_init__(self):
self.apples = str(self.apples) # Type changes from int to str
b = Base(apples=1) # Type is initially int
I am reusing the same attribute name here because I would like it show up in the __repr__
of the dataclass.