Last active
March 15, 2023 17:38
-
-
Save WaylonWalker/be038878a57f8000f1af3b18ac58ebd9 to your computer and use it in GitHub Desktop.
The idea is to make pluggable pydantic models where other libs can have config within a libraries config, and be type checked.
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 datetime import datetime | |
from typing import List, Optional | |
from pydantic import BaseModel, create_model, validator | |
import toml | |
class User(BaseModel): | |
id: int | |
name = "John Doe" | |
signup_ts: Optional[datetime] = None | |
friends: List[int] = [] | |
@validator("name") | |
def name_must_contain_space(cls, v): | |
if " " not in v: | |
raise ValueError("must contain a space") | |
return v.title() | |
class Load(BaseModel): | |
plugin_id: int = 5 | |
name: str = "Plugin" | |
class LoadPlugin(BaseModel): | |
# defaults to Load() if it is missing from the config, all values in Load | |
# must have a default, but does not require user to have a [load] key in | |
# config | |
load: Load = Load() | |
class UserPlugin(BaseModel): | |
# User has required keys, so you cannot set a default and the user must | |
# have a [user] key with at least an id in it. | |
user: User | |
plugins = ( | |
UserPlugin, | |
LoadPlugin, | |
) | |
DynamicUser = create_model("DynamicUser", __base__=plugins) | |
def make_user(): | |
external_data = toml.loads( | |
""" | |
[user] | |
id='123' | |
name='waylon walker' # this will fail name_must_contain_space | |
""" | |
) | |
user = DynamicUser(**external_data) | |
return user |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment