Skip to content

Commit

Permalink
chore(tests): Type-check tests
Browse files Browse the repository at this point in the history
  • Loading branch information
last-partizan committed Jun 1, 2024
1 parent 5a3cd77 commit 6768a26
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
14 changes: 10 additions & 4 deletions modeltranslation/tests/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# mypy: disable-error-code="django-manager-missing"
from __future__ import annotations

from django.contrib.auth.models import Permission
from django.core import validators
from django.db import models
from django.utils.translation import gettext_lazy

from modeltranslation.manager import MultilingualManager
from typing import Any


class TestModel(models.Model):
Expand Down Expand Up @@ -124,8 +128,10 @@ class ManyToManyFieldModel(models.Model):
self_call_1 = models.ManyToManyField("self")
# test multiple self m2m
self_call_2 = models.ManyToManyField("self")
through_model = models.ManyToManyField(TestModel, through="CustomThroughModel")
trans_through_model = models.ManyToManyField(
through_model = models.ManyToManyField[TestModel, "CustomThroughModel"](
TestModel, through="CustomThroughModel"
)
trans_through_model = models.ManyToManyField[TestModel, Any](
TestModel, related_name="m2m_trans_through_model_ref", through="RegisteredThroughModel"
)
untrans = models.ManyToManyField(
Expand Down Expand Up @@ -414,7 +420,7 @@ class Meta:
class CustomManagerChildTestModel(CustomManagerBaseModel):
title = models.CharField(gettext_lazy("title"), max_length=255)

objects = CustomManager2()
objects = CustomManager2() # type: ignore[misc]


class PlainChildTestModel(CustomManagerBaseModel):
Expand Down Expand Up @@ -505,7 +511,7 @@ class CustomManagerY(models.Manager):

class AbstractModelY(models.Model):
title = models.CharField(max_length=255)
xs = models.ManyToManyField("ModelX", through="ModelXY")
xs = models.ManyToManyField[ModelX, ModelXY]("ModelX", through="ModelXY")
objects = CustomManagerY()

class Meta:
Expand Down
3 changes: 3 additions & 0 deletions modeltranslation/tests/settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import os
import warnings

import django_stubs_ext

warnings.simplefilter("always", DeprecationWarning)
django_stubs_ext.monkeypatch()


def _get_database_config():
Expand Down
2 changes: 1 addition & 1 deletion modeltranslation/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from django.test import TestCase, TransactionTestCase
from django.test.utils import override_settings
from django.utils.translation import get_language, override, trans_real
from parameterized import parameterized
from parameterized import parameterized # type: ignore[import-untyped]

from modeltranslation import admin, translator
from modeltranslation import settings as mt_settings
Expand Down
4 changes: 2 additions & 2 deletions modeltranslation/tests/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,13 @@ class FieldInheritanceCTranslationOptions(FieldInheritanceBTranslationOptions):


class FieldInheritanceDTranslationOptions(FieldInheritanceBTranslationOptions):
fields = ("titled",)
fields = ["titled"]


class FieldInheritanceETranslationOptions(
FieldInheritanceCTranslationOptions, FieldInheritanceDTranslationOptions
):
fields = ("titlee",)
fields = ["titlee"]


# ######### Integration testing
Expand Down
16 changes: 15 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ ignore = [
"E501", # line length is handled by formatter
]

[tool.ruff.lint.pyflakes]
extend-generics = [
"django.db.models.ForeignKey",
"django.db.models.OneToOneField",
"django.db.models.ManyToManyField",
"django.db.models.Manager",
"django.db.models.manager.RelatedManager",
]

[tool.mypy]
python_version = "3.8"
incremental = true
Expand All @@ -48,9 +57,11 @@ warn_redundant_casts = true
warn_unused_configs = true
show_error_context = true
exclude = [
'tests/'
'tests/migrations/',
'tests/urls.py',
]
disable_error_code = ["method-assign"]
plugins = ["mypy_django_plugin.main"]

[[tool.mypy.overrides]]
module = ["modeltranslation.fields"]
Expand All @@ -67,3 +78,6 @@ disable_error_code = ["override", "attr-defined"]
[[tool.mypy.overrides]]
module = ["modeltranslation.manager"]
disable_error_code = ["override", "attr-defined", "return-value", "misc"]

[tool.django-stubs]
django_settings_module = "modeltranslation.tests.settings"

0 comments on commit 6768a26

Please sign in to comment.