Skip to content

Commit 907e457

Browse files
Merge branch 'develop' into fix/pydantic-creator-use-explicit-ns
2 parents 4e2c558 + 1d4d60b commit 907e457

35 files changed

+209
-221
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Changelog
1313
------
1414
Fixed
1515
^^^^^
16+
- Fixed asyncio "no current event loop" deprecation warning by replacing `asyncio.get_event_loop()` with modern event loop handling using `get_running_loop()` with fallback to `new_event_loop()` (#1865)
1617
- Add support for globalns and localns in pydantic_model_creator
1718

1819
Changed

examples/router.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
"""
22
This example to use router to implement read/write separation
33
"""
4-
5-
from typing import Type
6-
7-
from tortoise import Tortoise, fields, run_async
8-
from tortoise.models import Model
4+
from tortoise import Model, Tortoise, fields, run_async
95

106

117
class Event(Model):
@@ -21,10 +17,10 @@ def __str__(self):
2117

2218

2319
class Router:
24-
def db_for_read(self, model: Type[Model]):
20+
def db_for_read(self, model: type[Model]):
2521
return "slave"
2622

27-
def db_for_write(self, model: Type[Model]):
23+
def db_for_write(self, model: type[Model]):
2824
return "master"
2925

3026

examples/signals.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
This example demonstrates model signals usage
33
"""
44

5-
from typing import Optional, Type
5+
from typing import Optional
66

77
from tortoise import BaseDBAsyncClient, Tortoise, fields, run_async
88
from tortoise.models import Model
@@ -22,14 +22,14 @@ def __str__(self):
2222

2323
@pre_save(Signal)
2424
async def signal_pre_save(
25-
sender: "Type[Signal]", instance: Signal, using_db, update_fields
25+
sender: "type[Signal]", instance: Signal, using_db, update_fields
2626
) -> None:
2727
print(sender, instance, using_db, update_fields)
2828

2929

3030
@post_save(Signal)
3131
async def signal_post_save(
32-
sender: "Type[Signal]",
32+
sender: "type[Signal]",
3333
instance: Signal,
3434
created: bool,
3535
using_db: "Optional[BaseDBAsyncClient]",
@@ -40,14 +40,14 @@ async def signal_post_save(
4040

4141
@pre_delete(Signal)
4242
async def signal_pre_delete(
43-
sender: "Type[Signal]", instance: Signal, using_db: "Optional[BaseDBAsyncClient]"
43+
sender: "type[Signal]", instance: Signal, using_db: "Optional[BaseDBAsyncClient]"
4444
) -> None:
4545
print(sender, instance, using_db)
4646

4747

4848
@post_delete(Signal)
4949
async def signal_post_delete(
50-
sender: "Type[Signal]", instance: Signal, using_db: "Optional[BaseDBAsyncClient]"
50+
sender: "type[Signal]", instance: Signal, using_db: "Optional[BaseDBAsyncClient]"
5151
) -> None:
5252
print(sender, instance, using_db)
5353

tests/fields/subclass_fields.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from enum import Enum, IntEnum
2-
from typing import Any, Type
2+
from typing import Any
33

44
from tortoise import ConfigurationError
55
from tortoise.fields import CharField, IntField
@@ -13,7 +13,7 @@ class EnumField(CharField):
1313

1414
__slots__ = ("enum_type",)
1515

16-
def __init__(self, enum_type: Type[Enum], **kwargs):
16+
def __init__(self, enum_type: type[Enum], **kwargs):
1717
super().__init__(128, **kwargs)
1818
if not issubclass(enum_type, Enum):
1919
raise ConfigurationError(f"{enum_type} is not a subclass of Enum!")
@@ -48,7 +48,7 @@ class IntEnumField(IntField):
4848

4949
__slots__ = ("enum_type",)
5050

51-
def __init__(self, enum_type: Type[IntEnum], **kwargs):
51+
def __init__(self, enum_type: type[IntEnum], **kwargs):
5252
super().__init__(**kwargs)
5353
if not issubclass(enum_type, IntEnum):
5454
raise ConfigurationError(f"{enum_type} is not a subclass of IntEnum!")

tests/fields/test_time.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from datetime import date, datetime, time, timedelta
33
from datetime import timezone as dt_timezone
44
from time import sleep
5-
from typing import Type
65
from unittest.mock import patch
76

87
import pytz
@@ -18,7 +17,7 @@
1817

1918

2019
class TestEmpty(test.TestCase):
21-
model: Type[Model] = testmodels.DatetimeFields
20+
model: type[Model] = testmodels.DatetimeFields
2221

2322
async def test_empty(self):
2423
with self.assertRaises(IntegrityError):

tests/test_queryset.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import Type
2-
31
from tests.testmodels import (
42
Author,
53
Book,
@@ -845,7 +843,7 @@ async def test_joins_in_arithmetic_expressions(self):
845843

846844

847845
class TestNotExist(test.TestCase):
848-
exp_cls: Type[NotExistOrMultiple] = DoesNotExist
846+
exp_cls: type[NotExistOrMultiple] = DoesNotExist
849847

850848
@test.requireCapability(dialect="sqlite")
851849
def test_does_not_exist(self):

tests/test_signals.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional, Type
1+
from typing import Optional
22

33
from tests.testmodels import Signals
44
from tortoise import BaseDBAsyncClient
@@ -8,15 +8,15 @@
88

99
@pre_save(Signals)
1010
async def signal_pre_save(
11-
sender: "Type[Signals]", instance: Signals, using_db, update_fields
11+
sender: "type[Signals]", instance: Signals, using_db, update_fields
1212
) -> None:
1313
await Signals.filter(name="test1").update(name="test_pre-save")
1414
await Signals.filter(name="test5").update(name="test_pre-save")
1515

1616

1717
@post_save(Signals)
1818
async def signal_post_save(
19-
sender: "Type[Signals]",
19+
sender: "type[Signals]",
2020
instance: Signals,
2121
created: bool,
2222
using_db: "Optional[BaseDBAsyncClient]",
@@ -28,14 +28,14 @@ async def signal_post_save(
2828

2929
@pre_delete(Signals)
3030
async def signal_pre_delete(
31-
sender: "Type[Signals]", instance: Signals, using_db: "Optional[BaseDBAsyncClient]"
31+
sender: "type[Signals]", instance: Signals, using_db: "Optional[BaseDBAsyncClient]"
3232
) -> None:
3333
await Signals.filter(name="test3").update(name="test_pre-delete")
3434

3535

3636
@post_delete(Signals)
3737
async def signal_post_delete(
38-
sender: "Type[Signals]", instance: Signals, using_db: "Optional[BaseDBAsyncClient]"
38+
sender: "type[Signals]", instance: Signals, using_db: "Optional[BaseDBAsyncClient]"
3939
) -> None:
4040
await Signals.filter(name="test4").update(name="test_post-delete")
4141

tests/test_table_name.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
from typing import Type
2-
31
from tortoise import Tortoise, fields
42
from tortoise.contrib.test import SimpleTestCase
53
from tortoise.models import Model
64

75

8-
def table_name_generator(model_cls: Type[Model]):
6+
def table_name_generator(model_cls: type[Model]):
97
return f"test_{model_cls.__name__.lower()}"
108

119

tortoise/__init__.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from copy import deepcopy
1212
from inspect import isclass
1313
from types import ModuleType
14-
from typing import Any, Type, cast
14+
from typing import Any, cast
1515

1616
from pypika_tortoise import Query, Table
1717

@@ -34,8 +34,8 @@
3434

3535

3636
class Tortoise:
37-
apps: dict[str, dict[str, Type["Model"]]] = {}
38-
table_name_generator: Callable[[Type["Model"]], str] | None = None
37+
apps: dict[str, dict[str, type["Model"]]] = {}
38+
table_name_generator: Callable[[type["Model"]], str] | None = None
3939
_inited: bool = False
4040

4141
@classmethod
@@ -53,7 +53,7 @@ def get_connection(cls, connection_name: str) -> BaseDBAsyncClient:
5353

5454
@classmethod
5555
def describe_model(
56-
cls, model: Type["Model"], serializable: bool = True
56+
cls, model: type["Model"], serializable: bool = True
5757
) -> dict[str, Any]: # pragma: nocoverage
5858
"""
5959
Describes the given list of models or ALL registered models.
@@ -79,7 +79,7 @@ def describe_model(
7979

8080
@classmethod
8181
def describe_models(
82-
cls, models: list[Type["Model"]] | None = None, serializable: bool = True
82+
cls, models: list[type["Model"]] | None = None, serializable: bool = True
8383
) -> dict[str, dict[str, Any]]:
8484
"""
8585
Describes the given list of models or ALL registered models.
@@ -115,7 +115,7 @@ def describe_models(
115115

116116
@classmethod
117117
def _init_relations(cls) -> None:
118-
def get_related_model(related_app_name: str, related_model_name: str) -> Type["Model"]:
118+
def get_related_model(related_app_name: str, related_model_name: str) -> type["Model"]:
119119
"""
120120
Test, if app and model really exist. Throws a ConfigurationError with a hopefully
121121
helpful message. If successful, returns the requested model.
@@ -151,7 +151,7 @@ def split_reference(reference: str) -> tuple[str, str]:
151151
)
152152
return items[0], items[1]
153153

154-
def init_fk_o2o_field(model: Type["Model"], field: str, is_o2o=False) -> None:
154+
def init_fk_o2o_field(model: type["Model"], field: str, is_o2o=False) -> None:
155155
fk_object = cast(
156156
"OneToOneFieldInstance | ForeignKeyFieldInstance", model._meta.fields_map[field]
157157
)
@@ -284,7 +284,7 @@ def init_fk_o2o_field(model: Type["Model"], field: str, is_o2o=False) -> None:
284284
related_model._meta.add_field(backward_relation_name, m2m_relation)
285285

286286
@classmethod
287-
def _discover_models(cls, models_path: ModuleType | str, app_label: str) -> list[Type["Model"]]:
287+
def _discover_models(cls, models_path: ModuleType | str, app_label: str) -> list[type["Model"]]:
288288
if isinstance(models_path, ModuleType):
289289
module = models_path
290290
else:
@@ -329,7 +329,7 @@ def init_models(
329329
330330
:raises ConfigurationError: If models are invalid.
331331
"""
332-
app_models: list[Type[Model]] = []
332+
app_models: list[type[Model]] = []
333333
for models_path in models_paths:
334334
app_models += cls._discover_models(models_path, app_label)
335335

@@ -399,7 +399,7 @@ async def init(
399399
use_tz: bool = False,
400400
timezone: str = "UTC",
401401
routers: list[str | type] | None = None,
402-
table_name_generator: Callable[[Type["Model"]], str] | None = None,
402+
table_name_generator: Callable[[type["Model"]], str] | None = None,
403403
) -> None:
404404
"""
405405
Sets up Tortoise-ORM: loads apps and models, configures database connections but does not

tortoise/backends/base/client.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import abc
44
import asyncio
55
from collections.abc import Sequence
6-
from typing import Any, Generic, Type, TypeVar, cast
6+
from typing import Any, Generic, TypeVar, cast
77

88
from pypika_tortoise import Query
99

@@ -85,17 +85,17 @@ class BaseDBAsyncClient(abc.ABC):
8585
Parameters get passed as kwargs, and is mostly driver specific.
8686
8787
.. attribute:: query_class
88-
:annotation: Type[pypika_tortoise.Query]
88+
:annotation: type[pypika_tortoise.Query]
8989
9090
The PyPika Query dialect (low level dialect)
9191
9292
.. attribute:: executor_class
93-
:annotation: Type[BaseExecutor]
93+
:annotation: type[BaseExecutor]
9494
9595
The executor dialect class (high level dialect)
9696
9797
.. attribute:: schema_generator
98-
:annotation: Type[BaseSchemaGenerator]
98+
:annotation: type[BaseSchemaGenerator]
9999
100100
The DDL schema generator
101101
@@ -109,9 +109,9 @@ class BaseDBAsyncClient(abc.ABC):
109109
_parent: "BaseDBAsyncClient"
110110
_pool: Any
111111
connection_name: str
112-
query_class: Type[Query] = Query
113-
executor_class: Type[BaseExecutor] = BaseExecutor
114-
schema_generator: Type[BaseSchemaGenerator] = BaseSchemaGenerator
112+
query_class: type[Query] = Query
113+
executor_class: type[BaseExecutor] = BaseExecutor
114+
schema_generator: type[BaseSchemaGenerator] = BaseSchemaGenerator
115115
capabilities: Capabilities = Capabilities("")
116116

117117
def __init__(self, connection_name: str, fetch_inserted: bool = True, **kwargs: Any) -> None:

0 commit comments

Comments
 (0)