Skip to content
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

Merged
merged 42 commits into from
Jul 22, 2023
Merged

pydantic v2 #160

merged 42 commits into from
Jul 22, 2023

Conversation

awoimbee
Copy link
Contributor

@awoimbee awoimbee commented Jul 6, 2023

I may not have the time to complete this PR, feel free to create a new one using this one as inspiration/base !

@mansenfranzen
Copy link
Owner

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

@Galarzaa90
Copy link

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)

@mansenfranzen
Copy link
Owner

@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.

@awoimbee
Copy link
Contributor Author

Just sent you an invite to the fork, don't hesitate to push commits !
Maybe you'll be able to make some stuff to work that I could not get to (like PydanticConfigClassDocumenter with the new model_config, so I just removed it).

Franz Wöllert added 2 commits July 20, 2023 17:42
@mansenfranzen
Copy link
Owner

It works, I can push now.

Regarding PydanticConfigClassDocumenter: This was an exotic feature and as far as I can see, it is not even needed by the model/settings documenter. Hence, since there are many changes for v2, I suggest to remove it to reduce complexity and maintenance costs.

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"
Copy link
Owner

@mansenfranzen mansenfranzen Jul 20, 2023

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.

Copy link
Contributor Author

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

Copy link

@PriOliveira PriOliveira left a 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``)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Root validator is deprecated

Suggested change
(``@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

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():

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@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)

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
model_config = ConfigDict(frozen=False)
model_config = SettingsConfigDict(env_prefix="foo_", frozen=False)

@mansenfranzen
Copy link
Owner

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

Thanks @PriOliveira - all comments are more than appreciated.

@codecov-commenter
Copy link

codecov-commenter commented Jul 22, 2023

Codecov Report

Patch coverage has no change and project coverage change: -96.55 ⚠️

Comparison is base (bc07bf3) 96.54% compared to head (d6ef69f) 0.00%.

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     
Impacted Files Coverage Δ
sphinxcontrib/autodoc_pydantic/__init__.py 0.00% <0.00%> (-100.00%) ⬇️
...rib/autodoc_pydantic/directives/autodocumenters.py 0.00% <0.00%> (-98.13%) ⬇️
...xcontrib/autodoc_pydantic/directives/directives.py 0.00% <ø> (-98.24%) ⬇️
.../autodoc_pydantic/directives/options/definition.py 0.00% <ø> (-100.00%) ⬇️
sphinxcontrib/autodoc_pydantic/events.py 0.00% <ø> (-100.00%) ⬇️
sphinxcontrib/autodoc_pydantic/inspection.py 0.00% <0.00%> (-98.70%) ⬇️

... and 6 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@mansenfranzen mansenfranzen temporarily deployed to ci July 22, 2023 14:29 — with GitHub Actions Inactive
@mansenfranzen mansenfranzen temporarily deployed to ci July 22, 2023 14:29 — with GitHub Actions Inactive
@mansenfranzen mansenfranzen changed the base branch from main to main-2.x July 22, 2023 14:41
@mansenfranzen mansenfranzen marked this pull request as ready for review July 22, 2023 14:42
@mansenfranzen mansenfranzen self-assigned this Jul 22, 2023
@mansenfranzen mansenfranzen added documentation Improvements or additions to documentation enhancement New feature or request help wanted Extra attention is needed labels Jul 22, 2023
@mansenfranzen
Copy link
Owner

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.

@mansenfranzen mansenfranzen merged commit c0b011a into mansenfranzen:main-2.x Jul 22, 2023
7 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants