-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #347 from reagento/doc/why-no-pydantic-main
Add "Why not Pydantic?" article
- Loading branch information
Showing
18 changed files
with
606 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.. _benchmarks: | ||
|
||
================== | ||
Benchmarks | ||
================== | ||
|
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from pydantic import BaseModel, ConfigDict, Field | ||
|
||
|
||
class User(BaseModel): | ||
model_config = ConfigDict(populate_by_name=True, extra="allow") | ||
|
||
name: str = Field(alias="full_name") | ||
age: int | ||
|
||
|
||
data = {"name": "name_value", "age": 20} | ||
assert User.model_validate(data).model_extra == {} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# mypy: disable-error-code="arg-type" | ||
from decimal import Decimal | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class Product(BaseModel): | ||
id: int | ||
amount: Decimal | ||
|
||
|
||
assert Product(id=1, amount=14.6) == Product(id=1, amount=Decimal("14.6")) |
23 changes: 23 additions & 0 deletions
23
docs/examples/why_not_pydantic/implicit_conversions_workaround.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# mypy: disable-error-code="arg-type" | ||
from decimal import Decimal | ||
|
||
from pydantic import BaseModel, ConfigDict, ValidationError | ||
|
||
|
||
class Product(BaseModel): | ||
id: int | ||
amount: Decimal | ||
|
||
model_config = ConfigDict(strict=True) | ||
|
||
|
||
try: | ||
Product(id=1, amount=14.6) | ||
except ValidationError: | ||
pass | ||
|
||
assert ( | ||
Product.model_validate({"id": 1, "amount": 14.6}, strict=False) | ||
== | ||
Product(id=1, amount=Decimal("14.6")) | ||
) |
15 changes: 15 additions & 0 deletions
15
docs/examples/why_not_pydantic/instantiating_penalty_benchmark.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# ruff: noqa: T201 | ||
from datetime import datetime | ||
from timeit import timeit | ||
|
||
from .instantiating_penalty_models import UserDataclass, UserPydantic | ||
|
||
stmt = """ | ||
User( | ||
id=123, | ||
signup_ts=datetime(year=2019, month=6, day=1, hour=12, minute=22), | ||
tastes={'wine': 9, 'cheese': 7, 'cabbage': '1'}, | ||
) | ||
""" | ||
print("pydantic ", timeit(stmt, globals={"User": UserPydantic, "datetime": datetime})) | ||
print("dataclass", timeit(stmt, globals={"User": UserDataclass, "datetime": datetime})) |
26 changes: 26 additions & 0 deletions
26
docs/examples/why_not_pydantic/instantiating_penalty_benchmark_datetime.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# ruff: noqa: T201 | ||
from datetime import datetime | ||
from timeit import timeit | ||
|
||
from .instantiating_penalty_models import UserDataclass, UserPydantic | ||
|
||
stmt = """ | ||
User( | ||
id=123, | ||
signup_ts=dt, | ||
tastes={'wine': 9, 'cheese': 7, 'cabbage': '1'}, | ||
) | ||
""" | ||
dt = datetime(year=2019, month=6, day=1, hour=12, minute=22) | ||
print( | ||
"pydantic ", | ||
timeit(stmt, globals={"User": UserPydantic, "dt": dt}), | ||
) | ||
print( | ||
"pydantic (model_construct)", | ||
timeit(stmt, globals={"User": UserPydantic.model_construct, "dt": dt}), | ||
) | ||
print( | ||
"dataclass ", | ||
timeit(stmt, globals={"User": UserDataclass, "dt": dt}), | ||
) |
10 changes: 10 additions & 0 deletions
10
docs/examples/why_not_pydantic/instantiating_penalty_benchmark_model_construct.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# ruff: noqa: T201 | ||
from datetime import datetime | ||
from timeit import timeit | ||
|
||
from .instantiating_penalty_benchmark import UserPydantic, stmt | ||
|
||
print( | ||
"pydantic (model_construct)", | ||
timeit(stmt, globals={"User": UserPydantic.model_construct, "datetime": datetime}), | ||
) |
20 changes: 20 additions & 0 deletions
20
docs/examples/why_not_pydantic/instantiating_penalty_models.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from dataclasses import dataclass | ||
from datetime import datetime | ||
from typing import Optional | ||
|
||
from pydantic import BaseModel, PositiveInt | ||
|
||
|
||
class UserPydantic(BaseModel): | ||
id: int | ||
name: str = "John Doe" | ||
signup_ts: Optional[datetime] | ||
tastes: dict[str, PositiveInt] | ||
|
||
|
||
@dataclass(kw_only=True) | ||
class UserDataclass: | ||
id: int | ||
name: str = "John Doe" | ||
signup_ts: Optional[datetime] | ||
tastes: dict[str, PositiveInt] |
19 changes: 19 additions & 0 deletions
19
docs/examples/why_not_pydantic/migration_pydantic_model.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from pydantic import BaseModel | ||
|
||
from adaptix import Retort | ||
|
||
|
||
class Book(BaseModel): | ||
title: str | ||
price: int | ||
|
||
|
||
data = { | ||
"title": "Fahrenheit 451", | ||
"price": 100, | ||
} | ||
|
||
retort = Retort() | ||
book = retort.load(data, Book) | ||
assert book == Book(title="Fahrenheit 451", price=100) | ||
assert retort.dump(book) == data |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# mypy: disable-error-code="call-arg" | ||
from pydantic import BaseModel | ||
|
||
|
||
class SomeModel(BaseModel): | ||
a: int | ||
b: int | ||
|
||
|
||
SomeModel( | ||
a=1, | ||
b=2, | ||
c=3, # unknown filed! | ||
) |
21 changes: 21 additions & 0 deletions
21
docs/examples/why_not_pydantic/underdone_class_mapping_default.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# mypy: disable-error-code="call-arg" | ||
from dataclasses import dataclass | ||
from typing import Optional | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
@dataclass | ||
class Book: | ||
title: str | ||
author: str | ||
|
||
|
||
class BookDTO(BaseModel): | ||
title: str | ||
writer: Optional[str] = None # alias is forgotten! | ||
|
||
|
||
book = Book(title="Fahrenheit 451", author="Ray Bradbury") | ||
book_dto = BookDTO.model_validate(book, from_attributes=True) | ||
assert book_dto == BookDTO(title="Fahrenheit 451", author=None) |
19 changes: 19 additions & 0 deletions
19
docs/examples/why_not_pydantic/underdone_class_mapping_intro.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from dataclasses import dataclass | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
@dataclass | ||
class Person: | ||
name: str | ||
age: float | ||
|
||
|
||
class PersonDTO(BaseModel): | ||
name: str | ||
age: float | ||
|
||
|
||
person = Person(name="Anna", age=20) | ||
person_dto = PersonDTO.model_validate(person, from_attributes=True) | ||
assert person_dto == PersonDTO(name="Anna", age=20) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ Adaptix | |
|
||
overview | ||
benchmarks | ||
why-not-pydantic | ||
|
||
.. toctree:: | ||
:maxdepth: 2 | ||
|
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
Oops, something went wrong.