Skip to content

Commit 3a0fa0c

Browse files
ninosekichayim
andauthored
Allow using Pydantic v2 (#533)
Co-authored-by: Chayim <[email protected]>
1 parent 3237871 commit 3a0fa0c

10 files changed

+45
-14
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ lint: $(INSTALL_STAMP) dist
5454
$(POETRY) run isort --profile=black --lines-after-imports=2 ./tests/ $(NAME) $(SYNC_NAME)
5555
$(POETRY) run black ./tests/ $(NAME)
5656
$(POETRY) run flake8 --ignore=W503,E501,F401,E731 ./tests/ $(NAME) $(SYNC_NAME)
57-
$(POETRY) run mypy ./tests/ $(NAME) $(SYNC_NAME) --ignore-missing-imports --exclude migrate.py
57+
$(POETRY) run mypy ./tests/ $(NAME) $(SYNC_NAME) --ignore-missing-imports --exclude migrate.py --exclude _compat\.py$
5858
$(POETRY) run bandit -r $(NAME) $(SYNC_NAME) -s B608
5959

6060
.PHONY: format

aredis_om/_compat.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from pydantic.version import VERSION as PYDANTIC_VERSION
2+
3+
4+
PYDANTIC_V2 = PYDANTIC_VERSION.startswith("2.")
5+
6+
if PYDANTIC_V2:
7+
from pydantic.v1 import BaseModel, validator
8+
from pydantic.v1.fields import FieldInfo, ModelField, Undefined, UndefinedType
9+
from pydantic.v1.json import ENCODERS_BY_TYPE
10+
from pydantic.v1.main import ModelMetaclass, validate_model
11+
from pydantic.v1.typing import NoArgAnyCallable
12+
from pydantic.v1.utils import Representation
13+
else:
14+
from pydantic import BaseModel, validator
15+
from pydantic.fields import FieldInfo, ModelField, Undefined, UndefinedType
16+
from pydantic.json import ENCODERS_BY_TYPE
17+
from pydantic.main import ModelMetaclass, validate_model
18+
from pydantic.typing import NoArgAnyCallable
19+
from pydantic.utils import Representation

aredis_om/model/encoders.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
from types import GeneratorType
3232
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union
3333

34-
from pydantic import BaseModel
35-
from pydantic.json import ENCODERS_BY_TYPE
34+
from .._compat import ENCODERS_BY_TYPE, BaseModel
3635

3736

3837
SetIntStr = Set[Union[int, str]]

aredis_om/model/model.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,24 @@
2525
)
2626

2727
from more_itertools import ichunked
28-
from pydantic import BaseModel, validator
29-
from pydantic.fields import FieldInfo as PydanticFieldInfo
30-
from pydantic.fields import ModelField, Undefined, UndefinedType
31-
from pydantic.main import ModelMetaclass, validate_model
32-
from pydantic.typing import NoArgAnyCallable
33-
from pydantic.utils import Representation
3428
from redis.commands.json.path import Path
3529
from redis.exceptions import ResponseError
3630
from typing_extensions import Protocol, get_args, get_origin
3731
from ulid import ULID
3832

3933
from .. import redis
34+
from .._compat import BaseModel
35+
from .._compat import FieldInfo as PydanticFieldInfo
36+
from .._compat import (
37+
ModelField,
38+
ModelMetaclass,
39+
NoArgAnyCallable,
40+
Representation,
41+
Undefined,
42+
UndefinedType,
43+
validate_model,
44+
validator,
45+
)
4046
from ..checks import has_redis_json, has_redisearch
4147
from ..connections import get_redis_connection
4248
from ..util import ASYNC_MODE

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ include=[
3737
[tool.poetry.dependencies]
3838
python = ">=3.7,<4.0"
3939
redis = ">=3.5.3,<5.0.0"
40-
pydantic = "^1.10.2"
40+
pydantic = ">=1.10.2,<2.1.0"
4141
click = "^8.0.1"
4242
types-redis = ">=3.5.9,<5.0.0"
4343
python-ulid = "^1.0.3"

tests/_compat.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from aredis_om._compat import PYDANTIC_V2
2+
3+
4+
if PYDANTIC_V2:
5+
from pydantic.v1 import EmailStr, ValidationError
6+
else:
7+
from pydantic import EmailStr, ValidationError

tests/test_hash_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import pytest
1212
import pytest_asyncio
13-
from pydantic import ValidationError
1413

1514
from aredis_om import (
1615
Field,
@@ -24,6 +23,7 @@
2423
# We need to run this check as sync code (during tests) even in async mode
2524
# because we call it in the top-level module scope.
2625
from redis_om import has_redisearch
26+
from tests._compat import ValidationError
2727

2828
from .conftest import py_test_mark_asyncio
2929

tests/test_json_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import pytest
1212
import pytest_asyncio
13-
from pydantic import ValidationError
1413

1514
from aredis_om import (
1615
EmbeddedJsonModel,
@@ -25,6 +24,7 @@
2524
# We need to run this check as sync code (during tests) even in async mode
2625
# because we call it in the top-level module scope.
2726
from redis_om import has_redis_json
27+
from tests._compat import ValidationError
2828

2929
from .conftest import py_test_mark_asyncio
3030

tests/test_oss_redis_features.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
import pytest
88
import pytest_asyncio
9-
from pydantic import ValidationError
109

1110
from aredis_om import HashModel, Migrator, NotFoundError, RedisModelError
11+
from tests._compat import ValidationError
1212

1313
from .conftest import py_test_mark_asyncio
1414

tests/test_pydantic_integrations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
import pytest
66
import pytest_asyncio
7-
from pydantic import EmailStr, ValidationError
87

98
from aredis_om import Field, HashModel, Migrator
9+
from tests._compat import EmailStr, ValidationError
1010

1111

1212
today = datetime.date.today()

0 commit comments

Comments
 (0)