make BaseModelAdmin generic to properly type methods dealing with models (#504)

* make BaseModelAdmin generic to properly type the `obj` argument of ModelAdmin.delete_model
closes #482

* turn BaseModelAdmin into bound generic, run black

* add test for generic ModelAdmin
This commit is contained in:
proxy
2020-10-27 18:02:43 -04:00
committed by GitHub
parent 40c8bfa510
commit ffb6551eb4
2 changed files with 70 additions and 43 deletions

View File

@@ -14,7 +14,10 @@
def an_action(modeladmin: admin.ModelAdmin, request: HttpRequest, queryset: QuerySet) -> None:
pass
class A(admin.ModelAdmin):
class TestModel(models.Model):
pass
class A(admin.ModelAdmin[TestModel]):
# BaseModelAdmin
autocomplete_fields = ("strs",)
raw_id_fields = ["strs"]
@@ -71,6 +74,11 @@
actions_selection_counter = True
admin_site = AdminSite()
# test generic ModelAdmin
# https://github.com/typeddjango/django-stubs/pull/504
# this will fail if `model` has a type other than the generic specified in the class declaration
model = TestModel
def a_method_action(self, request, queryset):
pass
@@ -127,4 +135,4 @@
pass
class A(admin.ModelAdmin):
actions = [an_action] # E: List item 0 has incompatible type "Callable[[None], None]"; expected "Union[Callable[[ModelAdmin, HttpRequest, QuerySet[Any]], None], str]"
actions = [an_action] # E: List item 0 has incompatible type "Callable[[None], None]"; expected "Union[Callable[[ModelAdmin[Any], HttpRequest, QuerySet[Any]], None], str]"