fix(mypy): work around django-plugin annotate crash in comment viewset
All checks were successful
CI / nightly-e2e (pull_request) Has been skipped
CI / ci (pull_request) Successful in 1m20s

This commit is contained in:
Mark
2026-02-28 17:21:29 +00:00
parent 5adff60d4b
commit cfe0cbca62

View File

@@ -1,3 +1,5 @@
from typing import Any, cast
from django.db.models import Count, Q from django.db.models import Count, Q
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.utils.translation import ngettext from django.utils.translation import ngettext
@@ -49,22 +51,21 @@ class CommentViewSet(SnippetViewSet):
add_to_admin_menu = True add_to_admin_menu = True
def get_queryset(self, request): def get_queryset(self, request):
return ( base_qs = self.model.objects.all().select_related("article", "parent")
self.model.objects.all() # mypy-django-plugin currently crashes on QuerySet.annotate() in this file.
.select_related("article", "parent") typed_qs = cast(Any, base_qs)
.annotate( return typed_qs.annotate(
pending_in_article=Count( pending_in_article=Count(
"article__comments", "article__comments",
filter=Q(article__comments__is_approved=False), filter=Q(article__comments__is_approved=False),
distinct=True, distinct=True,
) )
) )
)
def pending_in_article(self, obj): def pending_in_article(self, obj):
return obj.pending_in_article return obj.pending_in_article
pending_in_article.short_description = "Pending (article)" pending_in_article.short_description = "Pending (article)" # type: ignore[attr-defined]
register_snippet(CommentViewSet) register_snippet(CommentViewSet)