-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pydantic v2 #160
pydantic v2 #160
Conversation
update minimum python version according to pydantic/pydantic#6426
sphinx 4 was released in 2021
Hi @awoimbee, thanks for the PR draft! I have not run any tests yet but the changes look rather minor and yet promising so far. I hope to get some time off to make progress with the pydantic v2 support. I'm happy for any more contribution :-). Greetings |
I tried to check this out, I ran all unit tests, they were okay. But I couldn't build the docs to test that. Had a hard time with graphviz (on Windows) |
@awoimbee Thanks so much for continuing your work on this PR! I will have some time the next days to support you. Could you allow me to push on your branch? I just tried but I don't seem to have permissions. |
Just sent you an invite to the fork, don't hesitate to push commits ! |
`field1 : Optional[int]` is no longer optional but required in v2.
It works, I can push now. Regarding |
pyproject.toml
Outdated
python = ">=3.7.1,<4.0.0" | ||
Sphinx = ">=3.4" | ||
pydantic = ">=1.5,<2.0.0" | ||
python = ">=3.8.0,<4.0.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@awoimbee Was there a specific reason to remove support for python 3.7? It is EOL by June 2023, however I would like to keep backwards compatibility if possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought pydantic would remove py37 support after seeing pydantic/pydantic#6426 but they fixed the issue. I'll add py37 back, there is a workaround needed around importlib_metadata
-> importlib.metadata
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was taking a look at the changes, even though I know it is still a draft, hopefully you don't mind for the comments about some stuff I found along the way
Also in the end the README will also need to be updated, for example:
Supports pydantic >= 1.5.0, < 2.0.0 and sphinx >= 3.4.0
@@ -153,7 +153,7 @@ Asterisk and root validators | |||
---------------------------- | |||
|
|||
This example highlights how `asterisk <https://pydantic-docs.helpmanual.io/usage/validators/#pre-and-per-item-validators>`_ | |||
(``@validator('*')``) and `root validators <https://pydantic-docs.helpmanual.io/usage/validators/#root-validators>`_ (``@root_validator``) | |||
(``@field_validator('*')``) and `root validators <https://pydantic-docs.helpmanual.io/usage/validators/#root-validators>`_ (``@root_validator``) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Root validator is deprecated
(``@field_validator('*')``) and `root validators <https://pydantic-docs.helpmanual.io/usage/validators/#root-validators>`_ (``@root_validator``) | |
(``@field_validator('*')``) and `model validators <https://docs.pydantic.dev/latest/usage/validators/#model-validators>`_ (``@model_validator``) |
self.attribute: Dict[str, Decorator[ValidatorDecoratorInfo]] = dict( | ||
**self.model.__pydantic_decorators__.validators, | ||
**self.model.__pydantic_decorators__.field_validators, | ||
**self.model.__pydantic_decorators__.root_validators |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since root_validator
got deprecated, probably this should be also updated
mapping["*"].append(ValidatorAdapter(func=func, | ||
root_post=True)) | ||
# root validators | ||
for validator in model_decorators.root_validators.values(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for validator in model_decorators.root_validators.values(): | |
for validator in model_decorators.model_validators.values(): |
@@ -1,6 +1,7 @@ | |||
from typing import Optional | |||
|
|||
from pydantic import BaseModel, validator, Field, BaseSettings, root_validator | |||
from pydantic import BaseModel, field_validator, Field, model_validator, ConfigDict, root_validator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from pydantic import BaseModel, field_validator, Field, model_validator, ConfigDict, root_validator | |
from pydantic import BaseModel, field_validator, Field, model_validator, ConfigDict |
def check(cls, v): | ||
"""Check.""" | ||
return v | ||
|
||
@root_validator | ||
@root_validator(skip_on_failure=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@root_validator(skip_on_failure=True) | |
@model_validator |
@@ -24,7 +24,7 @@ class Response(GenericModel, Generic[DataT]): | |||
data: Optional[DataT] | |||
error: Optional[Error] | |||
|
|||
@validator('error', always=True) | |||
@field_validator('error', always=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By the migration docs, it seems field_validator
doesn't have always
anymore, but I couldn't find what would be equivalent
@@ -1,4 +1,4 @@ | |||
from pydantic import BaseModel, validator, root_validator | |||
from pydantic import BaseModel, field_validator, root_validator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from pydantic import BaseModel, field_validator, root_validator | |
from pydantic import BaseModel, field_validator, model_validator |
env_prefix = "foo_" | ||
allow_mutation = True | ||
|
||
model_config = ConfigDict(frozen=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
model_config = ConfigDict(frozen=False) | |
model_config = SettingsConfigDict(env_prefix="foo_", frozen=False) |
"""Dummy DocString""" | ||
env_prefix = "foo_" | ||
allow_mutation = True | ||
model_config = ConfigDict(frozen=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
model_config = ConfigDict(frozen=False) | |
model_config = SettingsConfigDict(env_prefix="foo_", frozen=False) |
Thanks @PriOliveira - all comments are more than appreciated. |
Codecov ReportPatch coverage has no change and project coverage change:
Additional details and impacted files@@ Coverage Diff @@
## main #160 +/- ##
==========================================
- Coverage 96.54% 0.00% -96.55%
==========================================
Files 12 12
Lines 1071 1040 -31
==========================================
- Hits 1034 0 -1034
- Misses 37 1040 +1003
☔ View full report in Codecov by Sentry. |
I'm going merge this PR into the new "main-2.x" branch even though it is still WIP. Almost all tests have passed now. I will generate the new docs based on pydantic v2 which help to further improve the docs and update outdated information. |
I may not have the time to complete this PR, feel free to create a new one using this one as inspiration/base !