From a3739f75128ddef6894f0b941a99c5945575a0ba Mon Sep 17 00:00:00 2001 From: Zixuan James Li Date: Tue, 5 Jul 2022 21:27:42 -0400 Subject: [PATCH 1/2] Reflect the deprecation of get_response being None. Signed-off-by: Zixuan James Li --- django-stubs/middleware/cache.pyi | 2 +- django-stubs/utils/deprecation.pyi | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/django-stubs/middleware/cache.pyi b/django-stubs/middleware/cache.pyi index 520ac39f5..7923a936e 100644 --- a/django-stubs/middleware/cache.pyi +++ b/django-stubs/middleware/cache.pyi @@ -27,7 +27,7 @@ class CacheMiddleware(UpdateCacheMiddleware, FetchFromCacheMiddleware): cache: BaseCache = ... def __init__( self, - get_response: Optional[Callable] = ..., + get_response: Callable = ..., cache_timeout: Optional[float] = ..., page_timeout: Optional[float] = ..., **kwargs: Any diff --git a/django-stubs/utils/deprecation.pyi b/django-stubs/utils/deprecation.pyi index e1f1467e3..091c664ad 100644 --- a/django-stubs/utils/deprecation.pyi +++ b/django-stubs/utils/deprecation.pyi @@ -30,6 +30,6 @@ class DeprecationInstanceCheck(type): GetResponseCallable = Callable[[HttpRequest], HttpResponse] class MiddlewareMixin: - get_response: Optional[GetResponseCallable] = ... - def __init__(self, get_response: Optional[GetResponseCallable] = ...) -> None: ... + get_response: GetResponseCallable = ... + def __init__(self, get_response: GetResponseCallable = ...) -> None: ... def __call__(self, request: HttpRequest) -> HttpResponse: ... From 755081867edbfcfabc26968cf241d7447fe0252a Mon Sep 17 00:00:00 2001 From: Zixuan James Li Date: Fri, 5 Aug 2022 13:10:16 -0400 Subject: [PATCH 2/2] Type get_response with a callback protocol. Otherwise, calling `self.get_response(request)` in a subclass of `MiddlewareMixin` runs into `Invalid self argument` error. This is a workaround for https://github.com/python/mypy/issues/5485. Signed-off-by: Zixuan James Li --- django-stubs/utils/deprecation.pyi | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/django-stubs/utils/deprecation.pyi b/django-stubs/utils/deprecation.pyi index 091c664ad..77e61a880 100644 --- a/django-stubs/utils/deprecation.pyi +++ b/django-stubs/utils/deprecation.pyi @@ -1,4 +1,4 @@ -from typing import Any, Callable, Optional, Type +from typing import Any, Callable, Optional, Protocol, Type from django.http.request import HttpRequest from django.http.response import HttpResponse @@ -27,7 +27,8 @@ class DeprecationInstanceCheck(type): deprecation_warning: Type[Warning] def __instancecheck__(self, instance: Any): ... -GetResponseCallable = Callable[[HttpRequest], HttpResponse] +class GetResponseCallable(Protocol): + def __call__(self, __request: HttpRequest) -> HttpResponse: ... class MiddlewareMixin: get_response: GetResponseCallable = ...