Created
February 25, 2019 04:46
-
-
Save honewatson/9739a8923f1a9388abaa33ac90252e90 to your computer and use it in GitHub Desktop.
Pydantic Union Example
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 List, Union | |
from pydantic import BaseModel | |
from pydantic.dataclasses import dataclass | |
class MyConfig: | |
validate_assignment = True | |
@dataclass(config=MyConfig) | |
class Person: | |
title: str = 'Billy' | |
@dataclass(config=MyConfig) | |
class Robot: | |
title: int = 1234 | |
@dataclass(config=MyConfig) | |
class Fuku: | |
sitle: str = 'Haha' | |
@dataclass(config=MyConfig) | |
class Actors: | |
participants: List[Union[Person, Robot, Fuku]] = None | |
print(Actors(participants=[Robot(title=1234567), Person(title="Stinky"), Robot(title=1234567), Fuku(sitle='did')])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment