I'm currently working with a dataclass (defined in one of the dependencies I'm currently working with inside of my project), which is defined similar to this snippet: from dataclasses import dataclass from typing import Union class X: ... class Y: ... @dataclass class Container: data: Union[X, Y, dict] Then, within my project, I wanted to specify some cases where I already know the type of data. With Container being a dataclass, I know it is not possible to hint directly over the attribute as following: my_container = Container(X()) my_container.data: X # IDE complains I was wondering if it is possible then to specify it anyhow similar to Container[X] to define that the only attribute it is from the type I already know of. If not, is there any dunder method to add at the dataclass to allow this typing? Note: I am not allowed to modify the original Container code. Continue reading...