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 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') # 1 |
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
import pytest | |
from pydantic import BaseModel, Field, ValidationError | |
class Item(BaseModel): | |
item_id: str | |
is_available: bool = Field(alias='isAvailable') # 1 | |
def test_field_aliasing_bridges_inconsistant_conventions(): |
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 pydantic import BaseModel | |
class Item(BaseModel): | |
_id: str # 1 | |
is_available: bool | |
def test_pydantic_hides_names_with_preceding_underscores(): | |
item = Item(_id='test-item-id', is_available=True) # 1 |
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 pydantic import BaseModel | |
class Item(BaseModel): | |
id: str # 1 | |
isAvailable: bool # 2 | |
def test_does_it_look_pythonic_to_you(): | |
item = Item(id='test-item-id', isAvailable=True) # 3 |
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 Optional | |
from pydantic import validate_arguments, HttpUrl, PositiveInt | |
@validate_arguments | |
def better_get_payload( | |
url: HttpUrl, # 1 | |
retries: PositiveInt | |
) -> Optional[dict]: | |
# send the actual request - no need to verify URL # 2 | |
return {} |
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
import pytest | |
from typing import Optional | |
from pydantic import validate_arguments, PositiveInt, ValidationError | |
@validate_arguments # 1 | |
def validated_get_payload(url: str, retries: PositiveInt) -> Optional[dict]: | |
if url.startswith("http"): | |
# send the actual request and return the payload if valid response | |
return {} |
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
import pytest | |
from typing import Optional | |
from pydantic import PositiveInt, ValidationError | |
def get_payload(url: str, retries: PositiveInt) -> Optional[dict]: # 1 | |
if url.startswith("http"): | |
# send the actual request and return the payload if valid response | |
return {} | |
return None |
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
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 |
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
import os | |
import pytest | |
from pydantic import BaseSettings, HttpUrl, ValidationError | |
class Config(BaseSettings): # 1, 2 | |
auth_url: HttpUrl | |
db_name: str | |
max_retries: int |
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
import os | |
import pytest | |
from pydantic import BaseSettings, HttpUrl, ValidationError | |
class Config(BaseSettings): # 1, 2 | |
auth_url: HttpUrl | |
db_name: str | |
max_retries: int |