Last active
November 20, 2020 14:23
-
-
Save gidgid/0e4809a3464244d1eb96c09264b5c376 to your computer and use it in GitHub Desktop.
Showing wrong order and specialized models in jsons
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 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