Skip to content

Instantly share code, notes, and snippets.

@Xowap
Created October 29, 2019 20:23
Show Gist options
  • Save Xowap/1995b321027813d6b1cb134756cba374 to your computer and use it in GitHub Desktop.
Save Xowap/1995b321027813d6b1cb134756cba374 to your computer and use it in GitHub Desktop.
Pydantic
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