Skip to content

Commit e8c8679

Browse files
zyvsobolevn
andauthored
Add ClassVar annotations on mutable ModelAdmin types for ruff (#2524)
Signed-off-by: Yury V. Zaytsev <yury@shurup.com> Co-authored-by: sobolevn <mail@sobolevn.me>
1 parent 63d4342 commit e8c8679

File tree

4 files changed

+54
-52
lines changed

4 files changed

+54
-52
lines changed

django-stubs/contrib/admin/options.pyi

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import enum
22
from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence
3-
from typing import Any, Generic, Literal, TypeVar, cast, overload, type_check_only
3+
from typing import Any, ClassVar, Generic, Literal, TypeVar, cast, overload, type_check_only
44

55
from django import forms
66
from django.contrib.admin.filters import FieldListFilter, ListFilter
@@ -84,23 +84,25 @@ _ListFilterT: TypeAlias = (
8484
_ModelT = TypeVar("_ModelT", bound=Model)
8585
_DisplayT: TypeAlias = _ListOrTuple[str | Callable[[_ModelT], str | bool]]
8686

87+
# Options `form`, `list_display`, `list_display_links` and `actions` are not marked as `ClassVar` due to the
88+
# limitations of the current type system: `ClassVar` cannot contain type variables.
8789
class BaseModelAdmin(Generic[_ModelT]):
88-
autocomplete_fields: _ListOrTuple[str]
89-
raw_id_fields: _ListOrTuple[str]
90-
fields: _FieldGroups | None
91-
exclude: _ListOrTuple[str] | None
92-
fieldsets: _FieldsetSpec | None
90+
autocomplete_fields: ClassVar[_ListOrTuple[str]]
91+
raw_id_fields: ClassVar[_ListOrTuple[str]]
92+
fields: ClassVar[_FieldGroups | None]
93+
exclude: ClassVar[_ListOrTuple[str] | None]
94+
fieldsets: ClassVar[_FieldsetSpec | None]
9395
form: type[forms.ModelForm[_ModelT]]
94-
filter_vertical: _ListOrTuple[str]
95-
filter_horizontal: _ListOrTuple[str]
96-
radio_fields: Mapping[str, _Direction]
97-
prepopulated_fields: dict[str, Sequence[str]]
98-
formfield_overrides: Mapping[type[Field], Mapping[str, Any]]
99-
readonly_fields: _ListOrTuple[str]
100-
ordering: _ListOrTuple[str] | None
101-
sortable_by: _ListOrTuple[str] | None
102-
show_full_result_count: bool
103-
checks_class: Any
96+
filter_vertical: ClassVar[_ListOrTuple[str]]
97+
filter_horizontal: ClassVar[_ListOrTuple[str]]
98+
radio_fields: ClassVar[Mapping[str, _Direction]]
99+
prepopulated_fields: ClassVar[dict[str, Sequence[str]]]
100+
formfield_overrides: ClassVar[Mapping[type[Field], Mapping[str, Any]]]
101+
readonly_fields: ClassVar[_ListOrTuple[str]]
102+
ordering: ClassVar[_ListOrTuple[str] | None]
103+
sortable_by: ClassVar[_ListOrTuple[str] | None]
104+
show_full_result_count: ClassVar[bool]
105+
checks_class: ClassVar[Any]
104106
model: type[_ModelT]
105107
opts: Options[_ModelT]
106108
admin_site: AdminSite
@@ -150,33 +152,33 @@ _ActionCallable: TypeAlias = Callable[[_ModelAdmin, HttpRequest, QuerySet[_Model
150152
class ModelAdmin(BaseModelAdmin[_ModelT]):
151153
list_display: _DisplayT[_ModelT]
152154
list_display_links: _DisplayT[_ModelT] | None
153-
list_filter: _ListOrTuple[_ListFilterT]
154-
list_select_related: bool | _ListOrTuple[str]
155-
list_per_page: int
156-
list_max_show_all: int
157-
list_editable: _ListOrTuple[str]
158-
search_fields: _ListOrTuple[str]
159-
search_help_text: _StrOrPromise | None
160-
date_hierarchy: str | None
161-
save_as: bool
162-
save_as_continue: bool
163-
save_on_top: bool
164-
paginator: type
165-
preserve_filters: bool
166-
show_facets: ShowFacets
167-
inlines: _ListOrTuple[type[InlineModelAdmin]]
168-
add_form_template: _TemplateForResponseT | None
169-
change_form_template: _TemplateForResponseT | None
170-
change_list_template: _TemplateForResponseT | None
171-
delete_confirmation_template: _TemplateForResponseT | None
172-
delete_selected_confirmation_template: _TemplateForResponseT | None
173-
object_history_template: _TemplateForResponseT | None
174-
popup_response_template: _TemplateForResponseT | None
155+
list_filter: ClassVar[_ListOrTuple[_ListFilterT]]
156+
list_select_related: ClassVar[bool | _ListOrTuple[str]]
157+
list_per_page: ClassVar[int]
158+
list_max_show_all: ClassVar[int]
159+
list_editable: ClassVar[_ListOrTuple[str]]
160+
search_fields: ClassVar[_ListOrTuple[str]]
161+
search_help_text: ClassVar[_StrOrPromise | None]
162+
date_hierarchy: ClassVar[str | None]
163+
save_as: ClassVar[bool]
164+
save_as_continue: ClassVar[bool]
165+
save_on_top: ClassVar[bool]
166+
paginator: ClassVar[type]
167+
preserve_filters: ClassVar[bool]
168+
show_facets: ClassVar[ShowFacets]
169+
inlines: ClassVar[_ListOrTuple[type[InlineModelAdmin]]]
170+
add_form_template: ClassVar[_TemplateForResponseT | None]
171+
change_form_template: ClassVar[_TemplateForResponseT | None]
172+
change_list_template: ClassVar[_TemplateForResponseT | None]
173+
delete_confirmation_template: ClassVar[_TemplateForResponseT | None]
174+
delete_selected_confirmation_template: ClassVar[_TemplateForResponseT | None]
175+
object_history_template: ClassVar[_TemplateForResponseT | None]
176+
popup_response_template: ClassVar[_TemplateForResponseT | None]
175177
actions: Sequence[_ActionCallable[Self, _ModelT] | str] | None
176-
action_form: Any
177-
actions_on_top: bool
178-
actions_on_bottom: bool
179-
actions_selection_counter: bool
178+
action_form: ClassVar[Any]
179+
actions_on_top: ClassVar[bool]
180+
actions_on_bottom: ClassVar[bool]
181+
actions_selection_counter: ClassVar[bool]
180182
model: type[_ModelT]
181183
opts: Options[_ModelT]
182184
admin_site: AdminSite
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
from typing import Any
1+
from typing import Any, ClassVar
22

33
from django.contrib import admin
44
from django.contrib.flatpages.models import FlatPage
55

66
class FlatPageAdmin(admin.ModelAdmin[FlatPage]):
77
form: Any
8-
fieldsets: Any
8+
fieldsets: ClassVar[Any]
99
list_display: Any
10-
list_filter: Any
11-
search_fields: Any
10+
list_filter: ClassVar[Any]
11+
search_fields: ClassVar[Any]
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from typing import Any
1+
from typing import Any, ClassVar
22

33
from django.contrib import admin
44

55
class RedirectAdmin(admin.ModelAdmin):
66
list_display: Any
7-
list_filter: Any
8-
search_fields: Any
9-
radio_fields: Any
7+
list_filter: ClassVar[Any]
8+
search_fields: ClassVar[Any]
9+
radio_fields: ClassVar[Any]
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from typing import Any
1+
from typing import Any, ClassVar
22

33
from django.contrib import admin
44

55
class SiteAdmin(admin.ModelAdmin):
66
list_display: Any
7-
search_fields: Any
7+
search_fields: ClassVar[Any]

0 commit comments

Comments
 (0)