From 6768a26a9d59949fc28948b95e80eb5b5542c4db Mon Sep 17 00:00:00 2001 From: Serhii Tereshchenko Date: Sat, 1 Jun 2024 10:08:11 +0300 Subject: [PATCH] chore(tests): Type-check tests --- modeltranslation/tests/models.py | 14 ++++++++++---- modeltranslation/tests/settings.py | 3 +++ modeltranslation/tests/tests.py | 2 +- modeltranslation/tests/translation.py | 4 ++-- pyproject.toml | 16 +++++++++++++++- 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/modeltranslation/tests/models.py b/modeltranslation/tests/models.py index 57524f93..3caa288d 100644 --- a/modeltranslation/tests/models.py +++ b/modeltranslation/tests/models.py @@ -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): @@ -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( @@ -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): @@ -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: diff --git a/modeltranslation/tests/settings.py b/modeltranslation/tests/settings.py index 076171e7..c408918b 100644 --- a/modeltranslation/tests/settings.py +++ b/modeltranslation/tests/settings.py @@ -1,7 +1,10 @@ import os import warnings +import django_stubs_ext + warnings.simplefilter("always", DeprecationWarning) +django_stubs_ext.monkeypatch() def _get_database_config(): diff --git a/modeltranslation/tests/tests.py b/modeltranslation/tests/tests.py index 80158cf6..9de64ecf 100644 --- a/modeltranslation/tests/tests.py +++ b/modeltranslation/tests/tests.py @@ -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 diff --git a/modeltranslation/tests/translation.py b/modeltranslation/tests/translation.py index 587a7218..52da3295 100644 --- a/modeltranslation/tests/translation.py +++ b/modeltranslation/tests/translation.py @@ -241,13 +241,13 @@ class FieldInheritanceCTranslationOptions(FieldInheritanceBTranslationOptions): class FieldInheritanceDTranslationOptions(FieldInheritanceBTranslationOptions): - fields = ("titled",) + fields = ["titled"] class FieldInheritanceETranslationOptions( FieldInheritanceCTranslationOptions, FieldInheritanceDTranslationOptions ): - fields = ("titlee",) + fields = ["titlee"] # ######### Integration testing diff --git a/pyproject.toml b/pyproject.toml index 0d3dce83..ab933a0f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 @@ -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"] @@ -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"