Skip to content

Allow using Pydantic v2 #533

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

Merged
merged 5 commits into from
Jul 12, 2023
Merged
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ lint: $(INSTALL_STAMP) dist
$(POETRY) run isort --profile=black --lines-after-imports=2 ./tests/ $(NAME) $(SYNC_NAME)
$(POETRY) run black ./tests/ $(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

.PHONY: format
Expand Down
19 changes: 19 additions & 0 deletions aredis_om/_compat.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 1 addition & 2 deletions aredis_om/model/encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
from types import GeneratorType
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union

from pydantic import BaseModel
from pydantic.json import ENCODERS_BY_TYPE
from .._compat import ENCODERS_BY_TYPE, BaseModel


SetIntStr = Set[Union[int, str]]
Expand Down
18 changes: 12 additions & 6 deletions aredis_om/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,24 @@
)

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.exceptions import ResponseError
from typing_extensions import Protocol, get_args, get_origin
from ulid import ULID

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 ..connections import get_redis_connection
from ..util import ASYNC_MODE
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ include=[
[tool.poetry.dependencies]
python = ">=3.7,<4.0"
redis = ">=3.5.3,<5.0.0"
pydantic = "^1.10.2"
pydantic = ">=1.10.2,<2.1.0"
click = "^8.0.1"
types-redis = ">=3.5.9,<5.0.0"
python-ulid = "^1.0.3"
Expand Down
7 changes: 7 additions & 0 deletions tests/_compat.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion tests/test_hash_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import pytest
import pytest_asyncio
from pydantic import ValidationError

from aredis_om import (
Field,
Expand All @@ -24,6 +23,7 @@
# 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.
from redis_om import has_redisearch
from tests._compat import ValidationError

from .conftest import py_test_mark_asyncio

Expand Down
2 changes: 1 addition & 1 deletion tests/test_json_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import pytest
import pytest_asyncio
from pydantic import ValidationError

from aredis_om import (
EmbeddedJsonModel,
Expand All @@ -25,6 +24,7 @@
# 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.
from redis_om import has_redis_json
from tests._compat import ValidationError

from .conftest import py_test_mark_asyncio

Expand Down
2 changes: 1 addition & 1 deletion tests/test_oss_redis_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

import pytest
import pytest_asyncio
from pydantic import ValidationError

from aredis_om import HashModel, Migrator, NotFoundError, RedisModelError
from tests._compat import ValidationError

from .conftest import py_test_mark_asyncio

Expand Down
2 changes: 1 addition & 1 deletion tests/test_pydantic_integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import pytest
import pytest_asyncio
from pydantic import EmailStr, ValidationError

from aredis_om import Field, HashModel, Migrator
from tests._compat import EmailStr, ValidationError


today = datetime.date.today()
Expand Down