dataclass-wizard icon indicating copy to clipboard operation
dataclass-wizard copied to clipboard

Generic Type Problem

Open Esatyilmaz0 opened this issue 2 years ago • 7 comments

  • 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)

Esatyilmaz0 avatar Apr 02 '22 09:04 Esatyilmaz0