Skip to content

Inheriting from model_utils SoftDeletableModel causes issues #2011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JannKleen opened this issue Mar 15, 2024 · 4 comments
Closed

Inheriting from model_utils SoftDeletableModel causes issues #2011

JannKleen opened this issue Mar 15, 2024 · 4 comments
Labels
3rd party Presence of a third party dependency has been mentioned bug Something isn't working

Comments

@JannKleen
Copy link

JannKleen commented Mar 15, 2024

Bug report

What's wrong

I'm using https://github.com/jazzband/django-model-utils/blob/master/model_utils/models.py#L133-L158 (I know this isn't a great implementation for soft deletion, but I needed something quick and simple) and have the following models set up:

from model_utils.models import SoftDeletableModel


class BaseModel(SoftDeletableModel):
    class Meta:
        abstract = True

class MyModelA(BaseModel):
    sub_items = models.ManyToManyField(
        "self", related_name="parents", symmetrical=False, blank=True
    )

class MyModelB(BaseModel):
    model_a = models.ManyToManyField(MyModelA, related_name="model_b")
    deleted_model_a = models.ManyToManyField(MyModelA, related_name="model_b_deleted")

This works fine if I don't inherit from SoftDeletableModel but if I do I get:

error: Need type annotation for "sub_items"  [var-annotated]
error: Need type annotation for "model_a"  [var-annotated]
error: Need type annotation for "deleted_model_a"  [var-annotated]

System information

  • OS: Linux (python:3.11.7-slim-bullseye)
  • python version: 3.11.7
  • django version: django==4.2.7
  • mypy version: mypy==1.7.0
  • django-stubs version: django-stubs[compatible-mypy]==4.2.7
  • django-stubs-ext version: django-stubs-ext==4.2.7
@JannKleen JannKleen added the bug Something isn't working label Mar 15, 2024
@JannKleen
Copy link
Author

What works is manually annotating those fields with field : models.ManyToManyField = ....

@flaeppe flaeppe added the 3rd party Presence of a third party dependency has been mentioned label Mar 19, 2024
@flaeppe
Copy link
Member

flaeppe commented Mar 22, 2024

I'm not completely sure; but since SoftDeletableModel seem to lack type hints it might be that mypy can't resolve that class/type properly.

With the above said it looks like this is rather an issue (at least initially) for django-model-utils than for django-stubs.

I'm happy to reopen if it there's anything else that I might have missed and would need to be fixed.

@flaeppe flaeppe closed this as not planned Won't fix, can't repro, duplicate, stale Mar 22, 2024
@JannKleen
Copy link
Author

@flaeppe Thanks for the reply! I get that this is more related to django-model-utils, but since you're probably more familiar with python (and Django) typing, could you maybe point me at what could cause the issue? It doesn't look like they're doing anything crazy, is it the Mixin that could be causing this?

@intgr
Copy link
Collaborator

intgr commented Mar 24, 2024

I don't understand either, why this would happen. While django-model-utils is missing many specific type hints, it actually includes a py.typed marker. So mypy should be fully aware that SoftDeletableModel inherits from models.Model.

If you're looking for a quick work-around hack, this might help:

from typing import TYPE_CHECKING

class BaseModel(models.Model if TYPE_CHECKING else SoftDeletableModel):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3rd party Presence of a third party dependency has been mentioned bug Something isn't working
Development

No branches or pull requests

3 participants