Skip to content
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

Fix unittest error with pydantic2.9 #1734

Merged
merged 3 commits into from
Oct 13, 2024
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
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,20 @@ You can also install with your db driver (`aiosqlite` is builtin):

.. code-block:: bash

pip install tortoise-orm[asyncpg]
pip install "tortoise-orm[asyncpg]"


For `MySQL`:

.. code-block:: bash

pip install tortoise-orm[asyncmy]
pip install "tortoise-orm[asyncmy]"

For `Microsoft SQL Server`/`Oracle` (**not fully tested**):

.. code-block:: bash

pip install tortoise-orm[asyncodbc]
pip install "tortoise-orm[asyncodbc]"

Quick Tutorial
--------------
Expand Down
2,370 changes: 1,318 additions & 1,052 deletions poetry.lock

Large diffs are not rendered by default.

11 changes: 5 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "tortoise-orm"
version = "0.21.6"
version = "0.21.7"
description = "Easy async ORM for python, built with relations in mind"
authors = ["Andrey Bondar <[email protected]>", "Nickolas Grigoriadis <[email protected]>", "long2ice <[email protected]>"]
license = "Apache-2.0"
Expand Down Expand Up @@ -36,9 +36,9 @@ classifiers = [

[tool.poetry.dependencies]
python = "^3.8"
pypika-tortoise = "^0.1.6"
iso8601 = "^1.0.2"
aiosqlite = ">=0.16.0, <0.18.0"
pypika-tortoise = "^0.2.1"
iso8601 = "^2.1.0"
aiosqlite = ">=0.16.0, <0.21.0"
pytz = "*"
ciso8601 = { version = "*", markers = "sys_platform != 'win32' and implementation_name == 'cpython'", optional = true }
uvloop = { version = "*", markers = "sys_platform != 'win32' and implementation_name == 'cpython'", optional = true }
Expand All @@ -56,7 +56,6 @@ mypy = "*"
ruff = "*"
darglint = "*"
pylint = "*"
pygments = "*"
bandit = "*"
black = "*"
codespell = "*"
Expand All @@ -76,7 +75,7 @@ starlette = "*"
# Pydantic support
pydantic = "^2.0,!=2.7.0"
# FastAPI support
fastapi = "^0.100.0"
fastapi = "^0.115.0"
asgi_lifespan = "*"
httpx = "*"
# Aiohttp support
Expand Down
24 changes: 4 additions & 20 deletions tests/contrib/test_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,7 @@ def test_event_schema(self):
},
"name": {"description": "The name", "title": "Name", "type": "string"},
"tournament": {
"allOf": [
{
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Tournament__leaf"
}
],
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Tournament__leaf",
"description": "What tournaments is a happenin'",
},
"reporter": {
Expand Down Expand Up @@ -247,11 +243,7 @@ def test_eventlist_schema(self):
},
"name": {"description": "The name", "title": "Name", "type": "string"},
"tournament": {
"allOf": [
{
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Tournament__leaf"
}
],
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Tournament__leaf",
"description": "What tournaments is a happenin'",
},
"reporter": {
Expand Down Expand Up @@ -429,11 +421,7 @@ def test_address_schema(self):
},
"name": {"description": "The name", "title": "Name", "type": "string"},
"tournament": {
"allOf": [
{
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Tournament__leaf"
}
],
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Tournament__leaf",
"description": "What tournaments is a happenin'",
},
"reporter": {
Expand Down Expand Up @@ -763,11 +751,7 @@ def test_team_schema(self):
},
"name": {"description": "The name", "title": "Name", "type": "string"},
"tournament": {
"allOf": [
{
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Tournament__leaf"
}
],
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Tournament__leaf",
"description": "What tournaments is a happenin'",
},
"reporter": {
Expand Down
2 changes: 1 addition & 1 deletion tortoise/backends/psycopg/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ async def execute_query(
else:
rows = []

return rowcount, rows
return rowcount, typing.cast(typing.List[dict], rows)

async def execute_query_dict(
self, query: str, values: typing.Optional[list] = None
Expand Down
2 changes: 1 addition & 1 deletion tortoise/contrib/fastapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def register_tortoise(
# Leave on_event here to compare with old versions
# So people can upgrade tortoise-orm in running project without changing any code

@app.on_event("startup") # type: ignore[unreachable]
@app.on_event("startup")
async def init_orm() -> None: # pylint: disable=W0612
await orm.init_orm()

Expand Down
14 changes: 13 additions & 1 deletion tortoise/contrib/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@
from asyncio.events import AbstractEventLoop
from functools import partial, wraps
from types import ModuleType
from typing import Any, Callable, Coroutine, Iterable, List, Optional, TypeVar, Union
from typing import (
Any,
Callable,
Coroutine,
Iterable,
List,
Optional,
TypeVar,
Union,
cast,
)
from unittest import SkipTest, expectedFailure, skip, skipIf, skipUnless

from tortoise import Model, Tortoise, connections
Expand Down Expand Up @@ -488,4 +498,6 @@ async def runner(*args, **kwargs) -> T:
models = default_models
elif isinstance(models, str):
models = [models]
else:
models = cast(list, models)
return partial(wrapper, ms=models)
4 changes: 2 additions & 2 deletions tortoise/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def resolver_arithmetic_expression(
left_field_object,
) = cls.resolver_arithmetic_expression(model, left)
if left_field_object:
if field_object and type(field_object) != type(left_field_object):
if field_object and type(field_object) is not type(left_field_object):
raise FieldError(
"Cannot use arithmetic expression between different field type"
)
Expand All @@ -82,7 +82,7 @@ def resolver_arithmetic_expression(
right_field_object,
) = cls.resolver_arithmetic_expression(model, right)
if right_field_object:
if field_object and type(field_object) != type(right_field_object):
if field_object and type(field_object) is not type(right_field_object):
raise FieldError(
"Cannot use arithmetic expression between different field type"
)
Expand Down
19 changes: 14 additions & 5 deletions tortoise/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
Set,
Tuple,
Type,
TypedDict,
TypeVar,
Union,
cast,
)

from pypika import Order, Query, Table
Expand Down Expand Up @@ -87,6 +89,12 @@ def prepare_default_ordering(meta: "Model.Meta") -> Tuple[Tuple[str, Order], ...
return parsed_ordering


class FkSetterKwargs(TypedDict):
_key: str
relation_field: str
to_field: str


def _fk_setter(
self: "Model",
value: "Optional[Model]",
Expand Down Expand Up @@ -339,9 +347,9 @@ def _generate_lazy_fk_m2m_fields(self) -> None:
for key in self.fk_fields:
_key = f"_{key}"
fk_field_object: ForeignKeyFieldInstance = self.fields_map[key] # type: ignore
relation_field = fk_field_object.source_field
relation_field = cast(str, fk_field_object.source_field)
to_field = fk_field_object.to_field_instance.model_field_name
property_kwargs = dict(
property_kwargs: FkSetterKwargs = dict(
_key=_key,
relation_field=relation_field,
to_field=to_field,
Expand Down Expand Up @@ -388,8 +396,8 @@ def _generate_lazy_fk_m2m_fields(self) -> None:
# Create lazy one to one fields on model.
for key in self.o2o_fields:
_key = f"_{key}"
o2o_field_object: OneToOneFieldInstance = self.fields_map[key] # type: ignore
relation_field = o2o_field_object.source_field
o2o_field_object = cast(OneToOneFieldInstance, self.fields_map[key])
relation_field = cast(str, o2o_field_object.source_field)
to_field = o2o_field_object.to_field_instance.model_field_name
property_kwargs = dict(
_key=_key,
Expand Down Expand Up @@ -440,10 +448,11 @@ def _generate_lazy_fk_m2m_fields(self) -> None:
# Create lazy M2M fields on model.
for key in self.m2m_fields:
_key = f"_{key}"
field_object = cast(ManyToManyFieldInstance, self.fields_map[key])
setattr(
self._model,
key,
property(partial(_m2m_getter, _key=_key, field_object=self.fields_map[key])),
property(partial(_m2m_getter, _key=_key, field_object=field_object)),
)

def _generate_db_fields(self) -> None:
Expand Down