Skip to content

Instantly share code, notes, and snippets.

@gidgid
Last active November 20, 2020 14:23
Show Gist options
  • Save gidgid/0e4809a3464244d1eb96c09264b5c376 to your computer and use it in GitHub Desktop.
Save gidgid/0e4809a3464244d1eb96c09264b5c376 to your computer and use it in GitHub Desktop.
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'''
# mypy will generate type errors if we accidentally invoke divide_age
# in the wrong order
# divide_age(4, 5) # 2
# divide_age(Age.parse_obj(4), Age.parse_obj(5)) # 2
class Person(BaseModel):
age: Age
name: str
def test_age_is_converted_to_primitive_when_being_jsoned():
person = Person.parse_obj({'age': 37, 'name': 'John'})
assert person.json() == '{"age": 37, "name": "John"}' # 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment