1

When I run mypy, I get this error:

emails/admin.py:43: error: Missing type parameters for generic type "ModelAdmin"

And this is the corresponding code snippet:

@admin.register(Message)
class MessageAdmin(admin.ModelAdmin):
    list_display = ('subject', 'thread', 'user', 'email_account', 'from_name', 'from_email', 'local_timestamp')
    search_fields = ('subject','thread__synced_with_email', 'user__username', 'email_account__synced_email_current', 'from_name', 'from_email')
    ordering = ('subject', 'thread')

This class is not exactly a function so I do not understand why the type-hint is checked by mypy. There is nothing that gets returned and no parameters are passed.

Nick An
  • 27
  • 1
  • 5

1 Answers1

3

This happens because run-time dependency which mypy is unable to figure out. To get rid of the error, you need to install django_stubs_ext like pip install django-stubs-ext.

Then in your settings file add these two lines.

import django_stubs_ext
django_stubs_ext.monkeypatch()

Bind admin.ModelAdmin to specific model like admin.ModelAdmin[Message].

Modify admin.py file like

@admin.register(Message)
class MessageAdmin(admin.ModelAdmin[Message]):
    list_display = ('subject', 'thread', 'user', 'email_account', 'from_name', 'from_email', 'local_timestamp')
    search_fields = ('subject','thread__synced_with_email', 'user__username', 'email_account__synced_email_current', 'from_name', 'from_email')
    ordering = ('subject', 'thread')

The relevant PR introduced the change.

Kracekumar
  • 19,457
  • 10
  • 47
  • 56