Skip to content

Conversation

@aekasitt
Copy link

This pull request is a divergence away from the last two pull requests that pins down Pydantic version to V1 but instead updates to PydanticV2 with all of its added benefits and adds pydantic-settings for its BaseSettings class over deprecated one.
.
Other changes:

  • Replace deprecated "root_validator" decorators with "field_validator"
  • Replace this deprecated syntax:
from pydantic import BaseModel
class Example(BaseModel):
    __root__: ...

with

from pydantic import RootModel
class Example(RootModel):
    root: ...

PS. Since the minimum required version is now Python 3.10, I suggest a project-wide change of change this 3.9 syntax using typing.Optional as well:

from pydantic import BaseModel
from typing import Optional
class Example(BaseModel):
    value: Optional[str]  # pydantic v1

to

from pydantic import BaseModel
class Example(BaseModel):
    value: None | str = None  # with pydantic v2, nullable syntax is more strict

or

from pydantic import BaseModel, Field
class Example(BaseModel):
    value: None | str = Field(default=None)  # with pydantic v2, nullable syntax is more strict

@callebtc
Copy link
Collaborator

Hi, thank you for the PR! I researched pydantic v2 before and it seemed a lot less performant. did you try any benchmarks?

@aekasitt
Copy link
Author

Good idea. I found this closed issue on Pydantic codebase not yet fully resolved but going in a good direction:

One of the participants made a good benchmark harness so I made a fork of it:

The fork is here as well as the results from my mac; expanding to the most up-to-date Pydantic V2 version.

Here are the benchmarks:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants