From c80f62423e4ba812d693d93d7e8f8895443efecd Mon Sep 17 00:00:00 2001 From: savanto Date: Mon, 8 Jul 2024 17:33:57 -0500 Subject: [PATCH 1/2] Update `admin.sites._ViewType` bound to allow `StreamingHttpResponse` From the [Django documentation](https://docs.djangoproject.com/en/5.0/ref/request-response/#streaminghttpresponse-objects): > The `StreamingHttpResponse` is not a subclass of `HttpResponse` ... Because of this, creating an admin view that returned a `StreamingHttpResponse` was causing the following Mypy error: ``` testing/admin.py:14: error: Value of type variable "_ViewType" of "admin_view" of "AdminSite" cannot be "Callable[[HttpRequest], StreamingHttpResponse]" [type-var] ``` This updates the admin `_ViewType` type-var bound to allow a Callable that can return a union of `HttpResponse | StreamingHttpResponse`. After this update to the stubs file, the Mypy error is no longer reproducible. --- django-stubs/contrib/admin/sites.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/django-stubs/contrib/admin/sites.pyi b/django-stubs/contrib/admin/sites.pyi index e58996f24..a47b42a6c 100644 --- a/django-stubs/contrib/admin/sites.pyi +++ b/django-stubs/contrib/admin/sites.pyi @@ -10,7 +10,7 @@ from django.core.checks import CheckMessage from django.db.models.base import Model from django.db.models.query import QuerySet from django.http.request import HttpRequest -from django.http.response import HttpResponse +from django.http.response import HttpResponse, StreamingHttpResponse from django.template.response import TemplateResponse from django.urls import URLPattern, URLResolver from django.utils.functional import LazyObject, _StrOrPromise @@ -25,7 +25,7 @@ else: all_sites: MutableSet[AdminSite] -_ViewType = TypeVar("_ViewType", bound=Callable[..., HttpResponse]) +_ViewType = TypeVar("_ViewType", bound=Callable[..., HttpResponse | StreamingHttpResponse]) _ModelT = TypeVar("_ModelT", bound=Model) _ActionCallback: TypeAlias = Callable[[ModelAdmin, HttpRequest, QuerySet], TemplateResponse | None] From fc65173c1fc9fa882d7bed87b0fe6c5cb21e0c09 Mon Sep 17 00:00:00 2001 From: savanto Date: Tue, 9 Jul 2024 07:46:24 -0500 Subject: [PATCH 2/2] Use `HttpResponseBase` common parent instead --- django-stubs/contrib/admin/sites.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/django-stubs/contrib/admin/sites.pyi b/django-stubs/contrib/admin/sites.pyi index a47b42a6c..cb7b1963d 100644 --- a/django-stubs/contrib/admin/sites.pyi +++ b/django-stubs/contrib/admin/sites.pyi @@ -10,7 +10,7 @@ from django.core.checks import CheckMessage from django.db.models.base import Model from django.db.models.query import QuerySet from django.http.request import HttpRequest -from django.http.response import HttpResponse, StreamingHttpResponse +from django.http.response import HttpResponse, HttpResponseBase from django.template.response import TemplateResponse from django.urls import URLPattern, URLResolver from django.utils.functional import LazyObject, _StrOrPromise @@ -25,7 +25,7 @@ else: all_sites: MutableSet[AdminSite] -_ViewType = TypeVar("_ViewType", bound=Callable[..., HttpResponse | StreamingHttpResponse]) +_ViewType = TypeVar("_ViewType", bound=Callable[..., HttpResponseBase]) _ModelT = TypeVar("_ModelT", bound=Model) _ActionCallback: TypeAlias = Callable[[ModelAdmin, HttpRequest, QuerySet], TemplateResponse | None]