Django DeletionMixin and BaseDetailView causes a strange error with Mypy 0.991
So I have a very simple Django view that basically deletes a user account:
class UserDeleteView(LoginRequiredMixin, SuccessMessageMixin, DeleteView): success_message = _("Your account has been deleted") success_url = reverse_lazy('account_logout') def get_object(self): return self.request.user
After upgrading to Mypy 0.991 I am seeing this error:
error: Definition of "object" in base class "DeletionMixin" is incompatible with definition in base class "BaseDetailView" [misc]
For the life of me I can't figure out what this means, any help would be much appreciated.
DeletionMixin class is a
mixin that provides a delete action for a view, and the
BaseDetailView class is a base class for detail views that display a single object.
To resolve this issue, you may need to annotate the types of variables and parameters in your code to ensure that they are correctly inferred by
Mypy. For example, you might need to specify the type of the object being deleted or the type of the object being displayed in the detail view.
Here is an example of how you might use the
BaseDetailView classes with type annotations:
from django.views.generic import DeletionMixin, BaseDetailView from myapp.models import MyModel class MyDetailView(DeletionMixin, BaseDetailView): model = MyModel template_name = 'myapp/detail.html' success_url = reverse_lazy('account_logout') def delete(self, request: HttpRequest, *args: Any, **kwargs: Any) # Perform the delete action
In this example, the
MyDetailView class is a detail view that displays a single object of type
MyModel, and it also provides a delete action that takes an
HttpRequest object and returns an
HttpResponse object. By
annotating the types of the request parameter and the delete method's return value,
Mypy is able to infer the correct types and can check for type errors in your code.