Skip to content

Commit

Permalink
feat: allow pydantic v2 usage (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
cfbevan authored Jul 20, 2024
1 parent 31dc8d7 commit 62d50a9
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 152 deletions.
262 changes: 145 additions & 117 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pydango/connection/graph_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def db_traverse(
continue

if isinstance(relation_doc, LazyProxy):
relation_doc = relation_doc.__instance__
relation_doc = relation_doc.__instance__ # type: ignore[assignment]

if model.edges:
for edge_field, obj in model.edges.__dict__.items():
Expand Down
4 changes: 2 additions & 2 deletions pydango/orm/encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
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 pydantic.v1 import BaseModel
from pydantic.v1.json import ENCODERS_BY_TYPE

SetIntStr = Set[Union[int, str]]
DictIntStrAny = Dict[Union[int, str], Any]
Expand Down
12 changes: 6 additions & 6 deletions pydango/orm/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
get_origin,
)

from pydantic import BaseConfig, ConfigError, Field
from pydantic.fields import SHAPE_SINGLETON, ModelField, PrivateAttr, Undefined
from pydantic.main import BaseModel, ModelMetaclass
from pydantic.typing import resolve_annotations
from pydantic.v1 import BaseConfig, ConfigError, Field
from pydantic.v1.fields import SHAPE_SINGLETON, ModelField, PrivateAttr, Undefined
from pydantic.v1.main import BaseModel, ModelMetaclass
from pydantic.v1.typing import resolve_annotations

from pydango.connection.consts import PYDANGO_SESSION_KEY
from pydango.indexes import Indexes
Expand All @@ -41,8 +41,8 @@
from pydango.query.expressions import FieldExpression, ObjectExpression

if TYPE_CHECKING:
from pydantic.main import GetterDict, Model
from pydantic.typing import AbstractSet, DictStrAny, MappingIntStrAny
from pydantic.v1.main import GetterDict, Model
from pydantic.v1.typing import AbstractSet, DictStrAny, MappingIntStrAny

from pydango.connection.session import PydangoSession
from pydango.orm.models.types import RelationshipFields, Relationships
Expand Down
4 changes: 2 additions & 2 deletions pydango/orm/models/edge.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from abc import ABC
from typing import TYPE_CHECKING, Generic, Optional, TypeVar, Union

from pydantic import BaseModel, Field
from pydantic.v1 import BaseModel, Field

from pydango.orm.encoders import jsonable_encoder
from pydango.orm.models import BaseArangoModel, CollectionConfig, CollectionType
from pydango.query.consts import FROM, TO

if TYPE_CHECKING:
from pydantic.typing import DictStrAny
from pydantic.v1.typing import DictStrAny

TEdge = TypeVar("TEdge", bound="EdgeModel")

Expand Down
4 changes: 2 additions & 2 deletions pydango/orm/models/fields.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import TYPE_CHECKING, Any, Dict, Optional, Type, Union, cast

from pydantic.fields import ModelField
from pydantic.v1.fields import ModelField

from pydango.orm.models.sentinel import LazyFetch
from pydango.query.expressions import (
Expand All @@ -11,7 +11,7 @@
)

if TYPE_CHECKING:
from pydantic.fields import LocStr, ModelOrDc, ValidateReturn
from pydantic.v1.fields import LocStr, ModelOrDc, ValidateReturn

from pydango.orm.models.vertex import TVertexModel
from pydango.query.expressions import QueryExpression
Expand Down
4 changes: 2 additions & 2 deletions pydango/orm/models/relations.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from typing import TYPE_CHECKING, ForwardRef, Optional, Type

if TYPE_CHECKING:
from pydantic.fields import ModelField
from pydantic.typing import ReprArgs
from pydantic.v1.fields import ModelField
from pydantic.v1.typing import ReprArgs

from pydango.orm.models.base import LinkTypes
from pydango.orm.models.edge import TEdge
Expand Down
2 changes: 1 addition & 1 deletion pydango/orm/models/shapes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pydantic.fields import (
from pydantic.v1.fields import (
SHAPE_FROZENSET,
SHAPE_ITERABLE,
SHAPE_LIST,
Expand Down
8 changes: 4 additions & 4 deletions pydango/orm/models/vertex.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
cast,
)

from pydantic.fields import Field, ModelField
from pydantic.main import create_model
from pydantic.typing import evaluate_forwardref
from pydantic.v1.fields import Field, ModelField
from pydantic.v1.main import create_model
from pydantic.v1.typing import evaluate_forwardref

from pydango.orm.consts import EDGES
from pydango.orm.encoders import jsonable_encoder
Expand All @@ -24,7 +24,7 @@
from pydango.orm.utils import evaluate_forward_ref, get_globals

if TYPE_CHECKING:
from pydantic.typing import AbstractSetIntStr, DictStrAny, MappingIntStrAny
from pydantic.v1.typing import AbstractSetIntStr, DictStrAny, MappingIntStrAny

TVertexModel = TypeVar("TVertexModel", bound="VertexModel")
TEdges = TypeVar("TEdges", bound=EdgeData)
Expand Down
4 changes: 2 additions & 2 deletions pydango/orm/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import sys
from typing import TYPE_CHECKING, Optional, Sequence, Type, Union, cast, overload

from pydantic import BaseModel
from pydantic.utils import lenient_issubclass
from pydantic.v1 import BaseModel
from pydantic.v1.utils import lenient_issubclass

from pydango.orm.encoders import jsonable_encoder
from pydango.orm.models.base import Aliased, BaseArangoModel, LazyProxy
Expand Down
2 changes: 1 addition & 1 deletion pydango/orm/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
from functools import lru_cache

from pydantic.typing import evaluate_forwardref
from pydantic.v1.typing import evaluate_forwardref


def get_globals(cls):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ version = "0.2.1"
[tool.poetry.dependencies]
aioarango = "^1.0.0"
indexed = "^1.3.0"
pydantic = "==1.10.12"
pydantic = ">=1.10.17"
python = ">=3.9,<4.0"
urllib3 = "==1.26.15"

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

import pytest
from _pytest.fixtures import FixtureRequest
from pydantic import Field
from pydantic.v1 import Field
from pydiction import ANY_NOT_NONE, Matcher

from pydango.connection.session import PydangoSession
Expand Down
8 changes: 4 additions & 4 deletions tests/session/test_family.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,28 +278,28 @@ async def test_save(session: PydangoSession, request: FixtureRequest):
fiona.father = father
fiona.mother = mother

fiona.edges = sister_edges.copy()
fiona.edges = sister_edges.copy() # type: ignore[assignment]

jessica.sisters = [fiona]
jessica.brothers = [ben, john]
jessica.father = father
jessica.mother = mother

jessica.edges = sister_edges.copy()
jessica.edges = sister_edges.copy() # type: ignore[assignment]

john.sisters = [fiona, jessica]
john.brothers = [ben]
john.father = father
john.mother = mother

john.edges = brother_edges.copy()
john.edges = brother_edges.copy() # type: ignore[assignment]

ben.sisters = [fiona, jessica]
ben.brothers = [john]
ben.father = father
ben.mother = mother

ben.edges = brother_edges.copy()
ben.edges = brother_edges.copy() # type: ignore[assignment]

p = await session.save(john)
request.config.cache.set("person_key", p.key) # type: ignore[union-attr]
Expand Down
12 changes: 6 additions & 6 deletions tests/test_orm_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def test_simple_query():
aql = orm_query.for_(User).filter(User.age > 10).sort(+User.age).return_(User)
i = aql.orm_bound_vars[User]
expected_repr = (
f"FOR {repr(i)} IN <CollectionExpression: users> FILTER {repr(i)}.age > ? SORT {repr(i)}.age ASC RETURN"
f" {repr(i)}"
f"FOR {repr(i)} IN <CollectionExpression: users> FILTER {repr(i)}.age > ? SORT {repr(i)}.age ASC"
f" RETURN {repr(i)}"
)
expected_compilation = "FOR var1 IN `users` FILTER var1.age > @param1 SORT var1.age ASC RETURN var1"

Expand All @@ -51,8 +51,8 @@ def test_named_aliased():
i = aql.orm_bound_vars[aliased_user]
expected_compiled = "FOR u IN `users` FILTER u.age > @param1 SORT u.age DESC RETURN u"
expected_repr = (
f"FOR {repr(i)} IN <CollectionExpression: users> FILTER {repr(i)}.age > ? SORT {repr(i)}.age DESC RETURN"
f" {repr(i)}"
f"FOR {repr(i)} IN <CollectionExpression: users> FILTER {repr(i)}.age > ? SORT {repr(i)}.age DESC"
f" RETURN {repr(i)}"
)

assert repr(aql) == expected_repr
Expand All @@ -66,8 +66,8 @@ def test_aliased():
i = aql.orm_bound_vars[aliased_user]
expected_compiled = "FOR var1 IN `users` FILTER var1.age > @param1 SORT var1.age DESC RETURN var1"
expected_repr = (
f"FOR {repr(i)} IN <CollectionExpression: users> FILTER {repr(i)}.age > ? SORT {repr(i)}.age DESC RETURN"
f" {repr(i)}"
f"FOR {repr(i)} IN <CollectionExpression: users> FILTER {repr(i)}.age > ? SORT {repr(i)}.age DESC"
f" RETURN {repr(i)}"
)

assert repr(aql) == expected_repr
Expand Down

0 comments on commit 62d50a9

Please sign in to comment.