dataclass-wizard
dataclass-wizard copied to clipboard
Generic Type Problem
- Dataclass Wizard version:
- Python version:
- Operating System:
Description
Creating and using a data class to generate Generic classes work for me, dataclass-wizard fromdict not work . What is the best way for this?
What I Did
@dataclass class User: username: str email:str
@dataclass class Post: title:str slug:str
T = TypeVar("T")
@dataclass class Collection(Generic[T]): results:List[T] count: int
@dataclass class PostCollection(Collection[Post]): pass
@dataclass class UserCollection(Collection[User]): pass
a = fromdict(UserCollection, {"count":35, "results":[{"username":"esat", "email":"[email protected]"}, {"username":"test1", "email":"[email protected]"}]})
results is a dict not User Object List
UserCollection(results=[{'username': 'esat', 'email': '[email protected]'}, {'username': 'test1', 'email': '[email protected]'}], count=35)