Skip to content

Commit

Permalink
feat: allow pydantic v2 usage
Browse files Browse the repository at this point in the history
* feat: start migration to pydantic v2
* update tests for pydantic v2
  • Loading branch information
cfbevan committed Jul 5, 2024
1 parent 31dc8d7 commit 694a384
Show file tree
Hide file tree
Showing 15 changed files with 195 additions and 165 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
14 changes: 7 additions & 7 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 SortDirection.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 SortDirection.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 SortDirection.DESC"
f" RETURN {repr(i)}"
)

assert repr(aql) == expected_repr
Expand Down Expand Up @@ -117,7 +117,7 @@ def test_binary_expression():
f"FOR {repr(i2)} IN <CollectionExpression: posts> "
f"FILTER {repr(i2)}.user == {repr(i1)}.id &&"
f" {repr(i2)}.created_date > ? "
f"SORT {repr(i2)}.likes ASC, {repr(i1)}.age DESC "
f"SORT {repr(i2)}.likes SortDirection.ASC, {repr(i1)}.age SortDirection.DESC "
f"RETURN {repr(i1)}"
)

Expand Down
34 changes: 18 additions & 16 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def test_simple_query():
aql_query, coll = simple_query(param1)
expected_compiled = "FOR u IN `users` FILTER u.age > @param1 SORT u.age ASC RETURN u"
repr_i = repr(coll.iterator)
expected_repr = f"FOR {repr_i} IN {repr(coll)} FILTER {repr_i}.age > ? SORT {repr_i}.age ASC RETURN {repr_i}"
expected_repr = (
f"FOR {repr_i} IN {repr(coll)} FILTER {repr_i}.age > ? SORT {repr_i}.age SortDirection.ASC RETURN {repr_i}"
)
repr_query = repr(aql_query)
assert repr_query == expected_repr
assert aql_query.compile() == expected_compiled
Expand All @@ -53,7 +55,7 @@ def test_complex_query():
f"FILTER {repr(coll1.iterator)}.age > ? "
f"FOR {repr(coll2.iterator)} IN {repr(coll2)} "
f"FILTER {repr(coll2.iterator)}.date > {repr(coll1.iterator)}.date "
f"SORT {repr(coll1.iterator)}.name ASC "
f"SORT {repr(coll1.iterator)}.name SortDirection.ASC "
f"RETURN {repr(coll1.iterator)}"
)
repr_query = repr(aql_query)
Expand Down Expand Up @@ -84,7 +86,7 @@ def test_multiple_filters():
f"FOR {repr(coll.iterator)} IN {repr(coll)} "
f"FILTER {repr(coll.iterator)}.age > ? "
f"FILTER {repr(coll.iterator)}.gender == ? "
f"SORT {repr(coll.iterator)}.name ASC "
f"SORT {repr(coll.iterator)}.name SortDirection.ASC "
f"RETURN {repr(coll.iterator)}"
)
repr_query = repr(aql_query)
Expand All @@ -103,7 +105,7 @@ def test_subquery_in_for():
expected_repr = (
f"FOR {repr(it)} IN (FOR {repr(coll.iterator)} IN {repr(coll)} "
f"FILTER {repr(coll.iterator)}.name == ? "
f"SORT {repr(coll.iterator)}.name ASC "
f"SORT {repr(coll.iterator)}.name SortDirection.ASC "
f"RETURN {repr(coll.iterator)}) FILTER {repr(it)}.a == ? "
f"RETURN {repr(it)}"
)
Expand Down Expand Up @@ -300,11 +302,11 @@ def test_multiple_subqueries_with_multiple_params():
f"RETURN {{user: {repr(coll1.iterator)}.name, latest_order: "
f"(FOR {repr(coll2.iterator)} IN {repr(coll2)} "
f"FILTER {repr(coll2.iterator)}.status == ? "
f"SORT {repr(coll2.iterator)}.date DESC "
f"SORT {repr(coll2.iterator)}.date SortDirection.DESC "
f"RETURN {repr(coll2.iterator)}), active_users: "
f"(FOR {repr(coll1.iterator)} IN {repr(coll1)} "
f"FILTER {repr(coll1.iterator)}.age > ? "
f"SORT {repr(coll1.iterator)}.name ASC "
f"SORT {repr(coll1.iterator)}.name SortDirection.ASC "
f"RETURN {repr(coll1.iterator)})}}"
)

Expand Down Expand Up @@ -362,11 +364,11 @@ def test_nested_return_expression():
f"RETURN {{user: {repr(coll1.iterator)}.name, latest_order: "
f"(FOR {repr(coll2.iterator)} IN {repr(coll2)} "
f"FILTER {repr(coll2.iterator)}.status == ? "
f"SORT {repr(coll2.iterator)}.date DESC "
f"SORT {repr(coll2.iterator)}.date SortDirection.DESC "
f"RETURN {repr(coll2.iterator)}), top_products: {{expensive_products: "
f"(FOR {repr(coll3.iterator)} IN {repr(coll3)} "
f"FILTER {repr(coll3.iterator)}.price > ? "
f"SORT {repr(coll3.iterator)}.price DESC "
f"SORT {repr(coll3.iterator)}.price SortDirection.DESC "
f"RETURN {repr(coll3.iterator)}), cheap_products: ?}}}}"
)

Expand Down Expand Up @@ -450,15 +452,15 @@ def test_complex_query2():
f"FOR {repr(coll1.iterator)} IN {repr(coll1)} "
f"FILTER {repr(coll1.iterator)}.age > ? FILTER {repr(coll1.iterator)}.gender == ? "
f"FOR {repr(coll2.iterator)} IN {repr(coll2)} "
f"FILTER {repr(coll2.iterator)}.date >= ? SORT {repr(coll2.iterator)}.date DESC LIMIT 100 "
f"FILTER {repr(coll2.iterator)}.date >= ? SORT {repr(coll2.iterator)}.date SortDirection.DESC LIMIT 100 "
f"RETURN {{user: {repr(coll1.iterator)}.name, "
f"top_orders: (FOR {repr(coll2.iterator)} IN {repr(coll2)} "
f"FILTER {repr(coll2.iterator)}.user_id == {repr(coll1.iterator)}.id "
f"SORT {repr(coll2.iterator)}.total_amount DESC LIMIT 10 "
f"SORT {repr(coll2.iterator)}.total_amount SortDirection.DESC LIMIT 10 "
f"RETURN {repr(coll2.iterator)}), "
f"products: (FOR {repr(coll3.iterator)} IN {repr(coll3)} "
f"FILTER {repr(coll3.iterator)}.order_id == {repr(coll2.iterator)}.id "
f"SORT {repr(coll3.iterator)}.price ASC LIMIT 5 "
f"SORT {repr(coll3.iterator)}.price SortDirection.ASC LIMIT 5 "
f"RETURN {repr(coll3.iterator)})}}"
)

Expand Down Expand Up @@ -512,19 +514,19 @@ def test_nested_subqueries():
f"FILTER {repr(coll1.iterator)}.age > ? "
f"FOR {repr(coll2.iterator)} IN {repr(coll2)} "
f"FILTER {repr(coll2.iterator)}.date >= ? "
f"SORT {repr(coll2.iterator)}.date DESC LIMIT 100 "
f"SORT {repr(coll2.iterator)}.date SortDirection.DESC LIMIT 100 "
f"RETURN {{user: {repr(coll1.iterator)}.name, "
f"top_orders: (FOR {repr(coll2.iterator)} IN {repr(coll2)} "
f"FILTER {repr(coll2.iterator)}.user_id == {repr(coll1.iterator)}.id "
f"SORT {repr(coll2.iterator)}.total_amount DESC LIMIT 10 "
f"SORT {repr(coll2.iterator)}.total_amount SortDirection.DESC LIMIT 10 "
f"RETURN {repr(coll2.iterator)}), "
f"latest_order: (FOR {repr(coll2.iterator)} IN {repr(coll2)} "
f"FILTER {repr(coll2.iterator)}.user_id == {repr(coll1.iterator)}.id "
f"SORT {repr(coll2.iterator)}.date DESC LIMIT 1 "
f"SORT {repr(coll2.iterator)}.date SortDirection.DESC LIMIT 1 "
f"RETURN {repr(coll2.iterator)}), "
f"products: (FOR {repr(coll3.iterator)} IN {repr(coll3)} "
f"FILTER {repr(coll3.iterator)}.order_id == {repr(coll2.iterator)}.id "
f"SORT {repr(coll3.iterator)}.price ASC LIMIT 5 "
f"SORT {repr(coll3.iterator)}.price SortDirection.ASC LIMIT 5 "
f"RETURN {repr(coll3.iterator)})}}"
)

Expand Down Expand Up @@ -700,7 +702,7 @@ def test_let_query():

expected_repr = (
f"LET {DYNAMIC_ALIAS} = (FOR {repr(coll.iterator)} IN {repr(coll)} FILTER {repr(coll.iterator)}.age > ? SORT"
f" {repr(coll.iterator)}.age ASC RETURN {repr(coll.iterator)}) RETURN {repr(let)}"
f" {repr(coll.iterator)}.age SortDirection.ASC RETURN {repr(coll.iterator)}) RETURN {repr(let)}"
)

assert repr(aql) == expected_repr
Expand Down

0 comments on commit 694a384

Please sign in to comment.