-
Notifications
You must be signed in to change notification settings - Fork 0
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 #7 from eadwinCode/pydantic_v2_support
Pydantic v2 and v1 support
- Loading branch information
Showing
25 changed files
with
2,297 additions
and
842 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[report] | ||
exclude_lines = | ||
pragma: no cover | ||
def __repr__ | ||
def __str__ | ||
if self.debug: | ||
if IS_PYDANTIC_V1 | ||
if TYPE_CHECKING: | ||
if t.TYPE_CHECKING: | ||
if settings.DEBUG | ||
raise AssertionError | ||
raise NotImplementedError | ||
if 0: | ||
if __name__ == .__main__.: | ||
class .*\bProtocol\): | ||
@(abc\.)?abstractmethod |
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
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,46 @@ | ||
repos: | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v2.3.0 | ||
hooks: | ||
- id: check-merge-conflict | ||
- repo: https://github.com/asottile/yesqa | ||
rev: v1.3.0 | ||
hooks: | ||
- id: yesqa | ||
- repo: local | ||
hooks: | ||
- id: code_formatting | ||
args: [] | ||
name: Code Formatting | ||
entry: "make fmt" | ||
types: [python] | ||
language_version: python3 | ||
language: python | ||
- id: code_linting | ||
args: [ ] | ||
name: Code Linting | ||
entry: "make lint" | ||
types: [ python ] | ||
language_version: python3 | ||
language: python | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v2.3.0 | ||
hooks: | ||
- id: end-of-file-fixer | ||
exclude: >- | ||
^examples/[^/]*\.svg$ | ||
- id: requirements-txt-fixer | ||
- id: trailing-whitespace | ||
types: [python] | ||
- id: check-case-conflict | ||
- id: check-json | ||
- id: check-xml | ||
- id: check-executables-have-shebangs | ||
- id: check-toml | ||
- id: check-xml | ||
- id: check-yaml | ||
- id: debug-statements | ||
- id: check-added-large-files | ||
- id: check-symlinks | ||
- id: debug-statements | ||
exclude: ^tests/ |
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
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
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
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,16 +1,60 @@ | ||
from typing import Callable, Type | ||
import typing as t | ||
import warnings | ||
|
||
from django.db.models import Model as DjangoModel | ||
|
||
from ..pydanticutils import IS_PYDANTIC_V1 | ||
from ..types import DictStrAny | ||
from .getters import DjangoGetter | ||
|
||
|
||
class SchemaMixins: | ||
dict: Callable | ||
|
||
class BaseMixins: | ||
def apply_to_model( | ||
self, model_instance: Type[DjangoModel], **kwargs: DictStrAny | ||
) -> Type[DjangoModel]: | ||
for attr, value in self.dict(**kwargs).items(): | ||
self, model_instance: t.Type[DjangoModel], **kwargs: DictStrAny | ||
) -> t.Type[DjangoModel]: | ||
for attr, value in self.dict(**kwargs).items(): # type:ignore[attr-defined] | ||
setattr(model_instance, attr, value) | ||
return model_instance | ||
|
||
|
||
if not IS_PYDANTIC_V1: | ||
from pydantic import BaseModel, model_validator | ||
from pydantic.json_schema import GenerateJsonSchema | ||
from pydantic_core.core_schema import ValidationInfo | ||
|
||
class BaseMixinsV2(BaseMixins): | ||
@model_validator(mode="before") | ||
def _run_root_validator(cls, values: t.Any, info: ValidationInfo) -> t.Any: | ||
values = DjangoGetter(values, cls, info.context) | ||
return values | ||
|
||
@classmethod | ||
def from_orm(cls, obj: t.Any, **options: t.Any) -> BaseModel: | ||
return cls.model_validate( # type:ignore[attr-defined,no-any-return] | ||
obj, **options | ||
) | ||
|
||
def dict(self, *a: t.Any, **kw: t.Any) -> DictStrAny: | ||
# Backward compatibility with pydantic 1.x | ||
return self.model_dump(*a, **kw) # type:ignore[attr-defined,no-any-return] | ||
|
||
@classmethod | ||
def json_schema(cls) -> DictStrAny: | ||
return cls.model_json_schema( # type:ignore[attr-defined,no-any-return] | ||
schema_generator=GenerateJsonSchema | ||
) | ||
|
||
@classmethod | ||
def schema(cls) -> DictStrAny: | ||
warnings.warn( | ||
".schema() is deprecated, use .json_schema() instead", | ||
DeprecationWarning, | ||
stacklevel=2, | ||
) | ||
return cls.json_schema() | ||
|
||
BaseMixins = BaseMixinsV2 # type:ignore[misc] | ||
|
||
|
||
class SchemaMixins(BaseMixins): | ||
pass |
Oops, something went wrong.