Skip to content

Generic Type Problem #50

Open
Open
@Esatyilmaz0

Description

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions