Skip to content

Applies model generics on relational fields functions#2156

Merged
waketzheng merged 6 commits intotortoise:developfrom
ropmyung:develop
May 7, 2026
Merged

Applies model generics on relational fields functions#2156
waketzheng merged 6 commits intotortoise:developfrom
ropmyung:develop

Conversation

@ropmyung
Copy link
Copy Markdown
Contributor

Description

Applied generic syntax on relational fields functions for type hinting

ForeignKeyField
OneToOneField

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Mar 28, 2026

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing ropmyung:develop (e46fc47) with develop (919fa2f)

Open in CodSpeed

@waketzheng
Copy link
Copy Markdown
Contributor

Why do that? What's the benefit?

@ropmyung
Copy link
Copy Markdown
Contributor Author

ropmyung commented Apr 3, 2026

Why do that? What's the benefit?

Of course for type hint. Otherwise, It shows like Model, not the specific model class

@waketzheng
Copy link
Copy Markdown
Contributor

Could you show a demo of how 'Model' raises a type issue, while 'MODEL' does not?

@ropmyung
Copy link
Copy Markdown
Contributor Author

ropmyung commented Apr 4, 2026

Could you show a demo of how 'Model' raises a type issue, while 'MODEL' does not?

class Test1(Model):
    id = fields.BigIntField(pk=True)


class Test2(Model):
    id = fields.BigIntField(pk=True)
    test1 = fields.ForeignKeyField(Test1)
image image

@waketzheng
Copy link
Copy Markdown
Contributor

OK, get it. Seems that the ManyToManyField function in line 587 should be changed as well.

@waketzheng
Copy link
Copy Markdown
Contributor

The following lines should be changed also:
361
384
424
438
450
460
525

@waketzheng
Copy link
Copy Markdown
Contributor

@ropmyung, would you like to introduce generic syntax for ManyToManyField in this PR, or leave it for a separate one?

@ropmyung
Copy link
Copy Markdown
Contributor Author

ropmyung commented Apr 14, 2026

Oops, sorry I was too busy because of university stuff. I'll be happy if anyone addresses that; otherwise let me try implementing the ManyToMany generics within this week.

@waketzheng
Copy link
Copy Markdown
Contributor

@ropmyung Please fix lint issue, it can be:

+++ b/tests/migrations/test_schema_editor_sql.py
@@ -89,7 +89,9 @@ async def test_add_field_m2m_generates_table_sql() -> None:

     class WidgetWithTags(Model):
         id = fields.IntField(pk=True)
-        tags = fields.ManyToManyField("models.Tag", related_name="widgets")
+        tags: fields.ManyToManyRelation[Tag] = fields.ManyToManyField(
+            "models.Tag", related_name="widgets"
+        )

         class Meta:
             table = "widget"
diff --git a/tests/testmodels.py b/tests/testmodels.py
index 24ca2fa9..62b3b23f 100644
--- a/tests/testmodels.py
+++ b/tests/testmodels.py
@@ -1152,7 +1152,9 @@ class Flavor(Model):
 class Drink(Model):
     id = fields.IntField(pk=True)
     name = fields.CharField(max_length=100)
-    flavors = fields.ManyToManyField("models.Flavor", related_name="drinks", through="drink_flavor")
-    toppings = fields.ManyToManyField(
+    flavors: fields.ManyToManyRelation[Flavor] = fields.ManyToManyField(
+        "models.Flavor", related_name="drinks", through="drink_flavor"
+    )
+    toppings: fields.ManyToManyRelation[Flavor] = fields.ManyToManyField(
         "models.Flavor", related_name="topping_drinks", through="drink_topping"
     )

ropmyung and others added 2 commits April 25, 2026 11:51
Co-authored-by: Copilot <copilot@github.com>
@waketzheng
Copy link
Copy Markdown
Contributor

If you are using Linux/MacOS, run the following command to auto fix style issue:

make lint

And for Windows, you can do it by:

uv sync --reinstall-package tortoise-orm --frozen --all-groups --extra asyncpg --extra accel --extra psycopg --extra asyncodbc --extra aiomysql
uv run --frozen ruff format tortoise/ examples/ tests/ conftest.py
uv run --frozen ruff check --fix tortoise/ examples/ tests/ conftest.py
uv run --frozen mypy tortoise/ examples/ tests/ conftest.py
uv run --frozen bandit -c pyproject.toml -r tortoise

@waketzheng
Copy link
Copy Markdown
Contributor

@ropmyung You can run ruff format or uvx ruff format to auto-fix the lint issue.

@waketzheng
Copy link
Copy Markdown
Contributor

Alternatively, run the following command to check style:

prek run --all-files

Note: prek is a Rust-based version of pre-commit, which can be installed globally via uv tool install prek.

@ropmyung
Copy link
Copy Markdown
Contributor Author

ropmyung commented May 4, 2026

Eh... I can't understand...

@waketzheng
Copy link
Copy Markdown
Contributor

There is a lint issue with your latest commit: https://github.com/tortoise/tortoise-orm/actions/runs/24920902132/job/73011497824?pr=2156

image

If you are using macOS or Linux, run make style to fix it. If you are using Windows, run uvx ruff format instead.

After auto-fixing the lint issue, you can run uvx prek run --all-files to check the code style.

@waketzheng waketzheng merged commit 824fcca into tortoise:develop May 7, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants