django, mypy : queryset, choices. error: Incompatible types

1.2k views Asked by At

We have:

In models.py

class Language(models.IntegerChoices):
    en = 1
    ru = 2

class Translation(models.Model):
    language_id = models.PositiveIntegerField(choices=Language.choices)
    mnemonic = models.CharField(max_length=255)
    translation = models.CharField(max_length=255)
  1. in serializers.py
    66    def get_translations(self, obj):
    67        language: int = self.initial_data.get('language') or settings.SERVER['current_language_id']
    68        common_public = Translation.objects.filter(language_id=language).exclude(mnemonic__startswith='dictionary')

When i check it by mypy:

resources/serializers.py:67: error: Incompatible types in assignment (expression has type "Union[Any, object]", variable has type "int")

  1. if in serializers.py
    66    def get_translations(self, obj):
    67        language: Union[Any, object] = self.initial_data.get('language') or settings.SERVER['current_language_id']
    68        common_public = Translation.objects.filter(language_id=language).exclude(mnemonic__startswith='dictionary')

Check it by mypy:

resources/serializers.py:68: error: Incompatible type for lookup 'language_id': (got "Union[Any, object]", expected "Union[str, int]")

3)

66    def get_translations(self, obj):
67        language: Union[str, int] = self.initial_data.get('language') or settings.SERVER['current_language_id']
68        common_public = Translation.objects.filter(language_id=language).exclude(mnemonic__startswith='dictionary')

Check it:

resources/serializers.py:67: error: Incompatible types in assignment (expression has type "Union[Any, object]", variable has type "Union[str, int]")

What`s wrong?

mypy.ini:

[mypy]
python_version = 3.8
mypy_path = ./stubs

check_untyped_defs = True
disallow_any_generics = True
disallow_untyped_calls = True
disallow_untyped_decorators = True
ignore_errors = False
ignore_missing_imports = True
implicit_reexport = False
strict_optional = True
strict_equality = True
no_implicit_optional = True
warn_unused_ignores = True
warn_redundant_casts = True
warn_unused_configs = True
warn_unreachable = True
warn_no_return = True
plugins = mypy_django_plugin.main

[mypy.plugins.django-stubs]
django_settings_module = server.settings

[mypy-*.migrations.*]
ignore_errors = True
1

There are 1 answers

0
Yury Dr On

error was in settings.SERVER['current_language_id']

right way:

getattr(settings.SERVER, 'current_language_id')

Now mypy all right passed