Skip to content

Commit

Permalink
Use pydantic.v1 fallback for v1 subpackage
Browse files Browse the repository at this point in the history
  • Loading branch information
surenkov committed Mar 26, 2024
1 parent d80f968 commit d346aa3
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 13 deletions.
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 *
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
6 changes: 5 additions & 1 deletion tests/v1/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
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
Expand Down

0 comments on commit d346aa3

Please sign in to comment.