I’m trying to write a function that accepts a dataclass type as a parameter, does some reflection on that type, and then returns an instance of that dataclass type.
How do I type hint this?
import dataclasses
from typing import Type
AnyDataclassT = TypeVar("AnyDataclassT", bound=dataclasses.DataclassInstance)
def gather_dataclass(
dataclass_type: Type[AnyDataclassT],
) -> AnyDataclassT | None:
try:
dataclass = some_network_operation(dataclass_type)
return dataclass
except Exception:
return None
This is giving me:
"DataclassInstance" is not a known attribute of module "dataclasses"