Created
October 29, 2019 20:23
-
-
Save Xowap/1995b321027813d6b1cb134756cba374 to your computer and use it in GitHub Desktop.
Pydantic
This file contains 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 pydantic import BaseModel, ValidationError | |
from typing import Union | |
class A(BaseModel): | |
a: Union["A", "B"] | |
class B(BaseModel): | |
a: Union["A", "B"] | |
def generate_data(n): | |
out = {"a": "nope"} | |
for i in range(0, n): | |
out = {"a": out} | |
return out | |
if __name__ == "__main__": | |
A.update_forward_refs() | |
B.update_forward_refs() | |
data = generate_data(10) | |
try: | |
a = A(**data) | |
except ValidationError as e: | |
print(e.json()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment