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

[WIP] pydantic.v1 compatibility module #54

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions django_pydantic_field/v1/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
from django_pydantic_field.compat.pydantic import PYDANTIC_V1

if not PYDANTIC_V1:
raise ImportError("django_pydantic_field.v1 package is only compatible with Pydantic v1")

from .fields import * # noqa: F403
12 changes: 9 additions & 3 deletions django_pydantic_field/v1/base.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import typing as t

import pydantic
try:
from pydantic import v1 as pydantic
from pydantic.v1.json import pydantic_encoder
from pydantic.v1.typing import display_as_type
except ImportError:
import pydantic
from pydantic.json import pydantic_encoder
from pydantic.typing import display_as_type

from django.core.serializers.json import DjangoJSONEncoder
from pydantic.json import pydantic_encoder
from pydantic.typing import display_as_type

from .utils import get_local_namespace, inherit_configs

Expand Down
6 changes: 5 additions & 1 deletion django_pydantic_field/v1/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import typing as t
from functools import partial

import pydantic
try:
import pydantic.v1 as pydantic
except ImportError:
import pydantic

from django.core import exceptions as django_exceptions
from django.db.models.expressions import BaseExpression, Value
from django.db.models.fields import NOT_PROVIDED
Expand Down
6 changes: 5 additions & 1 deletion django_pydantic_field/v1/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import typing as t
from functools import partial

import pydantic
try:
import pydantic.v1 as pydantic
except ImportError:
import pydantic

from django.core.exceptions import ValidationError
from django.forms.fields import InvalidJSONInput, JSONField
from django.utils.translation import gettext_lazy as _
Expand Down
7 changes: 6 additions & 1 deletion django_pydantic_field/v1/rest_framework.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import typing as t

from django.conf import settings
from pydantic import BaseModel, ValidationError

try:
from pydantic.v1 import BaseModel, ValidationError
except ImportError:
from pydantic import BaseModel, ValidationError

from rest_framework import exceptions, parsers, renderers, serializers
from rest_framework.schemas import openapi
from rest_framework.schemas.utils import is_list_view
Expand Down
5 changes: 4 additions & 1 deletion django_pydantic_field/v1/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import sys
import typing as t

from pydantic.config import BaseConfig, inherit_config
try:
from pydantic.v1.config import BaseConfig, inherit_config
except ImportError:
from pydantic.config import BaseConfig, inherit_config

if t.TYPE_CHECKING:
from pydantic import BaseModel
Expand Down
26 changes: 26 additions & 0 deletions tests/v1/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import typing as t
from datetime import date

try:
import pydantic.v1 as pydantic
except ImportError:
import pydantic

from pydantic.dataclasses import dataclass


class InnerSchema(pydantic.BaseModel):
stub_str: str
stub_int: int = 1
stub_list: t.List[date]

class Config:
allow_mutation = True
frozen = False


@dataclass
class SampleDataclass:
stub_str: str
stub_list: t.List[date]
stub_int: int = 1
8 changes: 6 additions & 2 deletions tests/v1/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
from datetime import date
from uuid import UUID

import pydantic
try:
import pydantic.v1 as pydantic
except ImportError:
import pydantic

import pytest

from tests.conftest import InnerSchema, SampleDataclass
from .schema import InnerSchema, SampleDataclass

base = pytest.importorskip("django_pydantic_field.v1.base")

Expand Down
2 changes: 1 addition & 1 deletion tests/v1/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.core.exceptions import ValidationError
from django.forms import Form, modelform_factory

from tests.conftest import InnerSchema
from .schema import InnerSchema
from tests.test_app.models import SampleForwardRefModel, SampleSchema

fields = pytest.importorskip("django_pydantic_field.v1.fields")
Expand Down
2 changes: 1 addition & 1 deletion tests/v1/test_rest_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from rest_framework.decorators import api_view, parser_classes, renderer_classes, schema
from rest_framework.response import Response

from tests.conftest import InnerSchema
from .schema import InnerSchema
from tests.test_app.models import SampleModel

rest_framework = pytest.importorskip("django_pydantic_field.v1.rest_framework")
Expand Down
Loading