Skip to content

Instantly share code, notes, and snippets.

@gidgid
gidgid / copy_update_without_type_coercion.py
Created September 28, 2020 12:24
Copy with update doesn't perform type coercion
from pydantic import BaseModel
class Pizza(BaseModel):
toppings_count: int
size: str
def test_copy_with_update_doesnt_perform_conversion():
pizza = Pizza(toppings_count='4', size='XL')
@gidgid
gidgid / set_without_type_coercion.py
Last active November 20, 2020 13:39
Pydantic models set method doesnt execute type coercion
import pytest
from pydantic import BaseModel, ValidationError
class Pizza(BaseModel):
toppings_count: int
size: str
def test_field_set_doesnt_perform_conversion_either():
@gidgid
gidgid / validate_assignment_enforces_coercion.py
Last active October 2, 2020 11:46
Using validate_assignment to enforce type coercion
import pytest
from pydantic import BaseModel, ValidationError
class StrictPizza(BaseModel):
toppings_count: int
size: str
class Config:
validate_assignment = True # enforces coercion
@gidgid
gidgid / non_key_value_model.py
Created September 29, 2020 19:12
Defining non key-value models
from pydantic import BaseModel
class Age(BaseModel):
__root__: int
def test_specialized_models_are_not_equal_to_their_primitives():
age = Age(__root__=42)
assert age != 42
@gidgid
gidgid / non_key_values.py
Last active September 29, 2020 19:17
Basic non key-value models
from pydantic import BaseModel
class Age(BaseModel):
__root__: int # 1
def test_specialized_models_are_not_equal_to_their_primitives():
age = Age(__root__=42)
assert age != 42 # 2
@gidgid
gidgid / functions_are_dry_with_models.py
Last active October 4, 2020 19:04
Why is it useful to pass specialized models to functions
from pydantic import BaseModel
class Age(BaseModel):
__root__: int
def age_in_days(age: Age) -> int:
return age.__root__ * 365 # 1
@gidgid
gidgid / last_custom_root_examples.py
Last active November 20, 2020 14:23
Showing wrong order and specialized models in jsons
from pydantic import BaseModel
class Age(BaseModel):
__root__: int
def divide_age(x: Age, y: int): # 1
'''implementation irrelevant'''
@gidgid
gidgid / settings_with_pydantic.py
Last active November 20, 2020 14:42
Reading settings with Pydantic
import os
import pytest
from pydantic import BaseSettings, HttpUrl, ValidationError
class Config(BaseSettings): # 1, 2
auth_url: HttpUrl
db_name: str
max_retries: int
@gidgid
gidgid / settings_with_pydantic.py
Last active November 20, 2020 14:48
settings with pydantic
import os
import pytest
from pydantic import BaseSettings, HttpUrl, ValidationError
class Config(BaseSettings): # 1, 2
auth_url: HttpUrl
db_name: str
max_retries: int
@gidgid
gidgid / root_with_literals.py
Last active September 30, 2020 19:24
Reading settings models according to the environment
import os
import pytest
from pydantic import BaseSettings, HttpUrl, ValidationError
from typing import Union
from typing_extensions import Literal
class LocalContext(BaseSettings): # 1
env: Literal['local'] # 1
echo_server_url: str