Skip to content

Commit 824fcca

Browse files
ropmyungCopilotwaketzheng
authored
Applies model generics on relational fields functions (#2156)
* Applies model generics on relational fields functions * fix: update type hints for model_name in relational fields * fix: update tests Co-authored-by: Copilot <copilot@github.com> * fix: update incorrect test application * Fix lint issues --------- Co-authored-by: Copilot <copilot@github.com> Co-authored-by: Waket Zheng <waketzheng@gmail.com>
1 parent 919fa2f commit 824fcca

3 files changed

Lines changed: 15 additions & 15 deletions

File tree

tests/migrations/test_schema_editor_sql.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class Meta:
8989

9090
class WidgetWithTags(Model):
9191
id = fields.IntField(pk=True)
92-
tags = fields.ManyToManyField("models.Tag", related_name="widgets")
92+
tags = fields.ManyToManyField(Tag, related_name="widgets")
9393

9494
class Meta:
9595
table = "widget"

tests/testmodels.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,7 @@ class Flavor(Model):
11521152
class Drink(Model):
11531153
id = fields.IntField(pk=True)
11541154
name = fields.CharField(max_length=100)
1155-
flavors = fields.ManyToManyField("models.Flavor", related_name="drinks", through="drink_flavor")
1155+
flavors = fields.ManyToManyField(Flavor, related_name="drinks", through="drink_flavor")
11561156
toppings = fields.ManyToManyField(
1157-
"models.Flavor", related_name="topping_drinks", through="drink_topping"
1157+
Flavor, related_name="topping_drinks", through="drink_topping"
11581158
)

tortoise/fields/relational.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from collections.abc import AsyncGenerator, Generator, Iterator
55
from typing import TYPE_CHECKING, Any, Generic, Literal, TypeVar, overload
66

7-
from pypika_tortoise import Table
7+
from pypika_tortoise.queries import Table
88

99
from tortoise.exceptions import ConfigurationError, NoValuesFetched, OperationalError
1010
from tortoise.fields.base import CASCADE, SET_NULL, Field, OnDelete
@@ -358,7 +358,7 @@ def __init__(
358358
class OneToOneFieldInstance(ForeignKeyFieldInstance[MODEL]):
359359
def __init__(
360360
self,
361-
model_name: type[Model] | str,
361+
model_name: type[MODEL] | str,
362362
related_name: str | None | Literal[False] = None,
363363
on_delete: OnDelete = CASCADE,
364364
**kwargs: Any,
@@ -381,7 +381,7 @@ class ManyToManyFieldInstance(RelationalField[MODEL]):
381381

382382
def __init__(
383383
self,
384-
model_name: type[Model] | str,
384+
model_name: type[MODEL] | str,
385385
through: str | None = None,
386386
forward_key: str | None = None,
387387
backward_key: str = "",
@@ -421,7 +421,7 @@ def describe(self, serializable: bool) -> dict:
421421
if isinstance(self.model_name, str):
422422
model_name = self.model_name
423423
else:
424-
model: type[Model] = self.model_name
424+
model: type[MODEL] = self.model_name
425425
model_name = f"{model._meta.app}.{model.__name__}"
426426
desc["model_name"] = model_name
427427
desc["related_name"] = self.related_name
@@ -435,7 +435,7 @@ def describe(self, serializable: bool) -> dict:
435435

436436
@overload
437437
def OneToOneField(
438-
to: type[Model] | str,
438+
to: type[MODEL] | str,
439439
related_name: str | None | Literal[False] = None,
440440
on_delete: OnDelete = CASCADE,
441441
db_constraint: bool = True,
@@ -447,7 +447,7 @@ def OneToOneField(
447447

448448
@overload
449449
def OneToOneField(
450-
to: type[Model] | str,
450+
to: type[MODEL] | str,
451451
related_name: str | None | Literal[False] = None,
452452
on_delete: OnDelete = CASCADE,
453453
db_constraint: bool = True,
@@ -457,7 +457,7 @@ def OneToOneField(
457457

458458

459459
def OneToOneField(
460-
to: type[Model] | str,
460+
to: type[MODEL] | str,
461461
related_name: str | None | Literal[False] = None,
462462
on_delete: OnDelete = CASCADE,
463463
db_constraint: bool = True,
@@ -510,7 +510,7 @@ def OneToOneField(
510510

511511
@overload
512512
def ForeignKeyField(
513-
to: type[Model] | str,
513+
to: type[MODEL] | str,
514514
related_name: str | None | Literal[False] = None,
515515
on_delete: OnDelete = CASCADE,
516516
db_constraint: bool = True,
@@ -522,7 +522,7 @@ def ForeignKeyField(
522522

523523
@overload
524524
def ForeignKeyField(
525-
to: type[Model] | str,
525+
to: type[MODEL] | str,
526526
related_name: str | None | Literal[False] = None,
527527
on_delete: OnDelete = CASCADE,
528528
db_constraint: bool = True,
@@ -532,7 +532,7 @@ def ForeignKeyField(
532532

533533

534534
def ForeignKeyField(
535-
to: type[Model] | str,
535+
to: type[MODEL] | str,
536536
related_name: str | None | Literal[False] = None,
537537
on_delete: OnDelete = CASCADE,
538538
db_constraint: bool = True,
@@ -584,7 +584,7 @@ def ForeignKeyField(
584584

585585

586586
def ManyToManyField(
587-
to: type[Model] | str,
587+
to: type[MODEL] | str,
588588
through: str | None = None,
589589
forward_key: str | None = None,
590590
backward_key: str = "",
@@ -593,7 +593,7 @@ def ManyToManyField(
593593
db_constraint: bool = True,
594594
unique: bool = True,
595595
**kwargs: Any,
596-
) -> ManyToManyRelation[Any]:
596+
) -> ManyToManyRelation[MODEL]:
597597
"""
598598
ManyToMany relation field.
599599

0 commit comments

Comments
 (0)