Allow using Pydantic v2 (#533)

Co-authored-by: Chayim <chayim@users.noreply.github.com>
This commit is contained in:
Manabu Niseki 2023-07-12 17:48:08 +09:00 committed by GitHub
parent 323787151e
commit 3a0fa0c7be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 45 additions and 14 deletions

View file

@ -54,7 +54,7 @@ lint: $(INSTALL_STAMP) dist
$(POETRY) run isort --profile=black --lines-after-imports=2 ./tests/ $(NAME) $(SYNC_NAME) $(POETRY) run isort --profile=black --lines-after-imports=2 ./tests/ $(NAME) $(SYNC_NAME)
$(POETRY) run black ./tests/ $(NAME) $(POETRY) run black ./tests/ $(NAME)
$(POETRY) run flake8 --ignore=W503,E501,F401,E731 ./tests/ $(NAME) $(SYNC_NAME) $(POETRY) run flake8 --ignore=W503,E501,F401,E731 ./tests/ $(NAME) $(SYNC_NAME)
$(POETRY) run mypy ./tests/ $(NAME) $(SYNC_NAME) --ignore-missing-imports --exclude migrate.py $(POETRY) run mypy ./tests/ $(NAME) $(SYNC_NAME) --ignore-missing-imports --exclude migrate.py --exclude _compat\.py$
$(POETRY) run bandit -r $(NAME) $(SYNC_NAME) -s B608 $(POETRY) run bandit -r $(NAME) $(SYNC_NAME) -s B608
.PHONY: format .PHONY: format

19
aredis_om/_compat.py Normal file
View file

@ -0,0 +1,19 @@
from pydantic.version import VERSION as PYDANTIC_VERSION
PYDANTIC_V2 = PYDANTIC_VERSION.startswith("2.")
if PYDANTIC_V2:
from pydantic.v1 import BaseModel, validator
from pydantic.v1.fields import FieldInfo, ModelField, Undefined, UndefinedType
from pydantic.v1.json import ENCODERS_BY_TYPE
from pydantic.v1.main import ModelMetaclass, validate_model
from pydantic.v1.typing import NoArgAnyCallable
from pydantic.v1.utils import Representation
else:
from pydantic import BaseModel, validator
from pydantic.fields import FieldInfo, ModelField, Undefined, UndefinedType
from pydantic.json import ENCODERS_BY_TYPE
from pydantic.main import ModelMetaclass, validate_model
from pydantic.typing import NoArgAnyCallable
from pydantic.utils import Representation

View file

@ -31,8 +31,7 @@ from pathlib import PurePath
from types import GeneratorType from types import GeneratorType
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union
from pydantic import BaseModel from .._compat import ENCODERS_BY_TYPE, BaseModel
from pydantic.json import ENCODERS_BY_TYPE
SetIntStr = Set[Union[int, str]] SetIntStr = Set[Union[int, str]]

View file

@ -25,18 +25,24 @@ from typing import (
) )
from more_itertools import ichunked from more_itertools import ichunked
from pydantic import BaseModel, validator
from pydantic.fields import FieldInfo as PydanticFieldInfo
from pydantic.fields import ModelField, Undefined, UndefinedType
from pydantic.main import ModelMetaclass, validate_model
from pydantic.typing import NoArgAnyCallable
from pydantic.utils import Representation
from redis.commands.json.path import Path from redis.commands.json.path import Path
from redis.exceptions import ResponseError from redis.exceptions import ResponseError
from typing_extensions import Protocol, get_args, get_origin from typing_extensions import Protocol, get_args, get_origin
from ulid import ULID from ulid import ULID
from .. import redis from .. import redis
from .._compat import BaseModel
from .._compat import FieldInfo as PydanticFieldInfo
from .._compat import (
ModelField,
ModelMetaclass,
NoArgAnyCallable,
Representation,
Undefined,
UndefinedType,
validate_model,
validator,
)
from ..checks import has_redis_json, has_redisearch from ..checks import has_redis_json, has_redisearch
from ..connections import get_redis_connection from ..connections import get_redis_connection
from ..util import ASYNC_MODE from ..util import ASYNC_MODE

View file

@ -37,7 +37,7 @@ include=[
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = ">=3.7,<4.0" python = ">=3.7,<4.0"
redis = ">=3.5.3,<5.0.0" redis = ">=3.5.3,<5.0.0"
pydantic = "^1.10.2" pydantic = ">=1.10.2,<2.1.0"
click = "^8.0.1" click = "^8.0.1"
types-redis = ">=3.5.9,<5.0.0" types-redis = ">=3.5.9,<5.0.0"
python-ulid = "^1.0.3" python-ulid = "^1.0.3"

7
tests/_compat.py Normal file
View file

@ -0,0 +1,7 @@
from aredis_om._compat import PYDANTIC_V2
if PYDANTIC_V2:
from pydantic.v1 import EmailStr, ValidationError
else:
from pydantic import EmailStr, ValidationError

View file

@ -10,7 +10,6 @@ from unittest import mock
import pytest import pytest
import pytest_asyncio import pytest_asyncio
from pydantic import ValidationError
from aredis_om import ( from aredis_om import (
Field, Field,
@ -24,6 +23,7 @@ from aredis_om import (
# We need to run this check as sync code (during tests) even in async mode # We need to run this check as sync code (during tests) even in async mode
# because we call it in the top-level module scope. # because we call it in the top-level module scope.
from redis_om import has_redisearch from redis_om import has_redisearch
from tests._compat import ValidationError
from .conftest import py_test_mark_asyncio from .conftest import py_test_mark_asyncio

View file

@ -10,7 +10,6 @@ from unittest import mock
import pytest import pytest
import pytest_asyncio import pytest_asyncio
from pydantic import ValidationError
from aredis_om import ( from aredis_om import (
EmbeddedJsonModel, EmbeddedJsonModel,
@ -25,6 +24,7 @@ from aredis_om import (
# We need to run this check as sync code (during tests) even in async mode # We need to run this check as sync code (during tests) even in async mode
# because we call it in the top-level module scope. # because we call it in the top-level module scope.
from redis_om import has_redis_json from redis_om import has_redis_json
from tests._compat import ValidationError
from .conftest import py_test_mark_asyncio from .conftest import py_test_mark_asyncio

View file

@ -6,9 +6,9 @@ from typing import Optional
import pytest import pytest
import pytest_asyncio import pytest_asyncio
from pydantic import ValidationError
from aredis_om import HashModel, Migrator, NotFoundError, RedisModelError from aredis_om import HashModel, Migrator, NotFoundError, RedisModelError
from tests._compat import ValidationError
from .conftest import py_test_mark_asyncio from .conftest import py_test_mark_asyncio

View file

@ -4,9 +4,9 @@ from collections import namedtuple
import pytest import pytest
import pytest_asyncio import pytest_asyncio
from pydantic import EmailStr, ValidationError
from aredis_om import Field, HashModel, Migrator from aredis_om import Field, HashModel, Migrator
from tests._compat import EmailStr, ValidationError
today = datetime.date.today() today = datetime.date.today()