Vadim Beglov
Vadim Beglov

Reputation: 438

How to solve mypy error for django abstract class?

I have the next django abstract class, describing preview mixin:

from django.core.files.storage import default_storage
from django.db import models
from sorl.thumbnail import get_thumbnail


class PreviewMixin(models.Model):
    preview = models.ImageField(blank=True, null=True)

    class Meta:
        abstract = True

    def _get_preview_thumbnail(self, geometry_string: str, crop: str = 'noop', quality: int = 100) -> str:
        preview = ''

        if self.preview:
            thumbnail = get_thumbnail(file_=self.preview, geometry_string=geometry_string, crop=crop, quality=quality)
            preview = default_storage.url(name=thumbnail)

        return preview

When I am running mypy, I get an error: error: "DefaultStorage" has no attribute "url" [attr-defined]

My code works correct for me without errors. What should I fix or add or update to pass this mypy checking?

Versions of packages are:

django 4.2.2 
mypy 1.4.1
django-stubs 4.2.3
django-stubs-ext 4.2.2
sorl.thumbnail 12.9.0

mypy.ini

[mypy]
python_version = 3.11
plugins = mypy_django_plugin.main, mypy_drf_plugin.main
exclude = .git, .idea, .mypy_cache, .ruff_cache, node_modules
check_untyped_defs = true
disallow_untyped_decorators = true
disallow_untyped_calls = true
ignore_errors = false
ignore_missing_imports = true
implicit_reexport = false
local_partial_types = true
no_implicit_optional = true
strict_optional = true
strict_equality = true
warn_unused_ignores = true
warn_redundant_casts = true
warn_unused_configs = true
warn_unreachable = true
warn_no_return = true

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

Upvotes: 0

Views: 291

Answers (1)

boyenec
boyenec

Reputation: 1617

you are trying to use url attribute from default_storage which doesn't belong to the default_storage

in your model you are using preview = default_storage.url(name=thumbnail)

but the default_storage doesn't have any attribute url so use the correct attribute which really belongs from default_storage

Upvotes: 0

Related Questions