Created
May 27, 2021 17:07
-
-
Save Fkawala/00a5b79d80b3dbe8e4e6254ae5638a4d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import ClassVar, Type, Dict, Generic, TypeVar, Any, List | |
from pydantic import BaseModel | |
# | |
T = TypeVar('T', bound=BaseModel) | |
class Response(BaseModel, Generic[T]): | |
data: Type[BaseModel] | |
metadata: Dict[str, Any] | |
def __init__(self, *a, **kw) -> None: | |
_metadata = kw.pop("metadata", {}) | |
_data = type(Type[T])(*a, **kw) | |
super().__init__(data=_data, metadata=_metadata) | |
class X(BaseModel): | |
""" | |
This class is the model used to answer to the client on the /parse route. | |
""" | |
successes: List[str] | |
failures: List[int] | |
class MyResponse(Response[X]): | |
data: X | |
print(MyResponse(successes=["1","qsd"], failures=[3,4,5], metadata={"zzz": 133}).dict()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.