1+ from __future__ import annotations
2+
3+ from typing import TYPE_CHECKING , Any
4+
15from django .contrib import admin
2- from django .db .models import Count
6+ from django .db .models import Count , QuerySet
37from guardian .admin import GuardedModelAdmin
48
59from opencontractserver .annotations .models import (
1115 Relationship ,
1216)
1317
18+ if TYPE_CHECKING :
19+ from django .http import HttpRequest
20+
1421
1522class ContentModalityFilter (admin .SimpleListFilter ):
1623 """
@@ -21,7 +28,11 @@ class ContentModalityFilter(admin.SimpleListFilter):
2128 title = "content modality"
2229 parameter_name = "modality"
2330
24- def lookups (self , request , model_admin ):
31+ def lookups (
32+ self ,
33+ request : HttpRequest ,
34+ model_admin : admin .ModelAdmin [Any ],
35+ ) -> tuple [tuple [str , str ], ...]:
2536 return (
2637 ("text" , "TEXT only" ),
2738 ("image" , "IMAGE only" ),
@@ -31,7 +42,11 @@ def lookups(self, request, model_admin):
3142 ("empty" , "No modalities" ),
3243 )
3344
34- def queryset (self , request , queryset ):
45+ def queryset (
46+ self ,
47+ request : HttpRequest ,
48+ queryset : QuerySet [Annotation ],
49+ ) -> QuerySet [Annotation ]:
3550 if self .value () == "text" :
3651 return queryset .filter (content_modalities = ["TEXT" ])
3752 if self .value () == "image" :
@@ -61,7 +76,7 @@ class AnnotationEmbeddingInline(admin.TabularInline):
6176 readonly_fields = ("id" , "embedder_path" , "dimension" , "created" , "modified" )
6277 extra = 0
6378
64- def dimension (self , obj ) :
79+ def dimension (self , obj : Embedding ) -> str :
6580 """Display which vector dimension is populated."""
6681 if obj .vector_384 is not None :
6782 return "384"
@@ -73,7 +88,7 @@ def dimension(self, obj):
7388 return "3072"
7489 return "Unknown"
7590
76- dimension .short_description = "Dimension"
91+ dimension .short_description = "Dimension" # type: ignore[attr-defined]
7792
7893
7994class NoteEmbeddingInline (admin .TabularInline ):
@@ -87,7 +102,7 @@ class NoteEmbeddingInline(admin.TabularInline):
87102 readonly_fields = ("id" , "embedder_path" , "dimension" , "created" , "modified" )
88103 extra = 0
89104
90- def dimension (self , obj ) :
105+ def dimension (self , obj : Embedding ) -> str :
91106 """Display which vector dimension is populated."""
92107 if obj .vector_384 is not None :
93108 return "384"
@@ -99,7 +114,7 @@ def dimension(self, obj):
99114 return "3072"
100115 return "Unknown"
101116
102- dimension .short_description = "Dimension"
117+ dimension .short_description = "Dimension" # type: ignore[attr-defined]
103118
104119
105120@admin .register (Annotation )
@@ -125,31 +140,31 @@ class AnnotationAdmin(GuardedModelAdmin):
125140 raw_id_fields = ("annotation_label" , "document" , "corpus" , "analysis" , "creator" )
126141 inlines = [AnnotationEmbeddingInline ]
127142
128- def get_queryset (self , request ) :
143+ def get_queryset (self , request : HttpRequest ) -> QuerySet [ Any ] :
129144 """
130145 Override queryset to annotate with embedding count.
131146 """
132147 qs = super ().get_queryset (request )
133148 return qs .annotate (total_embeddings = Count ("embedding_set" , distinct = True ))
134149
135- def total_embeddings (self , obj ) :
150+ def total_embeddings (self , obj : Annotation ) -> int :
136151 """
137152 Display the total number of embeddings for this annotation.
138153 """
139- return obj .total_embeddings
154+ return obj .total_embeddings # type: ignore[attr-defined]
140155
141- total_embeddings .admin_order_field = "total_embeddings"
142- total_embeddings .short_description = "Embeddings"
156+ total_embeddings .admin_order_field = "total_embeddings" # type: ignore[attr-defined]
157+ total_embeddings .short_description = "Embeddings" # type: ignore[attr-defined]
143158
144- def modality_display (self , obj ) :
159+ def modality_display (self , obj : Annotation ) -> str :
145160 """
146161 Display content modalities as a formatted string.
147162 """
148163 if obj .content_modalities :
149164 return ", " .join (obj .content_modalities )
150165 return "-"
151166
152- modality_display .short_description = "Modality"
167+ modality_display .short_description = "Modality" # type: ignore[attr-defined]
153168
154169
155170@admin .register (Relationship )
@@ -194,21 +209,21 @@ class NoteAdmin(GuardedModelAdmin):
194209 raw_id_fields = ("parent" , "document" , "annotation" , "creator" )
195210 inlines = [NoteEmbeddingInline ]
196211
197- def get_queryset (self , request ) :
212+ def get_queryset (self , request : HttpRequest ) -> QuerySet [ Any ] :
198213 """
199214 Override queryset to annotate with embedding count.
200215 """
201216 qs = super ().get_queryset (request )
202217 return qs .annotate (total_embeddings = Count ("embedding_set" , distinct = True ))
203218
204- def total_embeddings (self , obj ) :
219+ def total_embeddings (self , obj : Note ) -> int :
205220 """
206221 Display the total number of embeddings for this note.
207222 """
208- return obj .total_embeddings
223+ return obj .total_embeddings # type: ignore[attr-defined]
209224
210- total_embeddings .admin_order_field = "total_embeddings"
211- total_embeddings .short_description = "Embeddings"
225+ total_embeddings .admin_order_field = "total_embeddings" # type: ignore[attr-defined]
226+ total_embeddings .short_description = "Embeddings" # type: ignore[attr-defined]
212227
213228
214229@admin .register (Embedding )
@@ -227,7 +242,7 @@ class EmbeddingAdmin(GuardedModelAdmin):
227242 search_fields = ["embedder_path" , "id" ]
228243 raw_id_fields = ("document" , "annotation" , "note" , "creator" )
229244
230- def reference_type (self , obj ) :
245+ def reference_type (self , obj : Embedding ) -> str :
231246 """Display what type of object this embedding belongs to."""
232247 if obj .document_id :
233248 return f"Document #{ obj .document_id } "
@@ -237,11 +252,11 @@ def reference_type(self, obj):
237252 return f"Note #{ obj .note_id } "
238253 return "Unknown"
239254
240- reference_type .short_description = "Referenced Object"
255+ reference_type .short_description = "Referenced Object" # type: ignore[attr-defined]
241256
242- def dimension_info (self , obj ) :
257+ def dimension_info (self , obj : Embedding ) -> str :
243258 """Display which vector dimension is populated."""
244- dimensions = []
259+ dimensions : list [ str ] = []
245260 if obj .vector_384 is not None :
246261 dimensions .append ("384" )
247262 if obj .vector_768 is not None :
@@ -252,4 +267,4 @@ def dimension_info(self, obj):
252267 dimensions .append ("3072" )
253268 return ", " .join (dimensions ) if dimensions else "None"
254269
255- dimension_info .short_description = "Dimensions"
270+ dimension_info .short_description = "Dimensions" # type: ignore[attr-defined]
0 commit comments