Skip to content

Commit c0414c7

Browse files
authored
Reflect the deprecation of get_response being None. (#1086)
* Reflect the deprecation of get_response being None. Signed-off-by: Zixuan James Li <[email protected]> * 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 python/mypy#5485. Signed-off-by: Zixuan James Li <[email protected]> Signed-off-by: Zixuan James Li <[email protected]>
1 parent 12e8009 commit c0414c7

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

django-stubs/middleware/cache.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class CacheMiddleware(UpdateCacheMiddleware, FetchFromCacheMiddleware):
2727
cache: BaseCache = ...
2828
def __init__(
2929
self,
30-
get_response: Optional[Callable] = ...,
30+
get_response: Callable = ...,
3131
cache_timeout: Optional[float] = ...,
3232
page_timeout: Optional[float] = ...,
3333
**kwargs: Any

django-stubs/utils/deprecation.pyi

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Callable, Optional, Type
1+
from typing import Any, Callable, Optional, Protocol, Type
22

33
from django.http.request import HttpRequest
44
from django.http.response import HttpResponse
@@ -27,9 +27,10 @@ class DeprecationInstanceCheck(type):
2727
deprecation_warning: Type[Warning]
2828
def __instancecheck__(self, instance: Any): ...
2929

30-
GetResponseCallable = Callable[[HttpRequest], HttpResponse]
30+
class GetResponseCallable(Protocol):
31+
def __call__(self, __request: HttpRequest) -> HttpResponse: ...
3132

3233
class MiddlewareMixin:
33-
get_response: Optional[GetResponseCallable] = ...
34-
def __init__(self, get_response: Optional[GetResponseCallable] = ...) -> None: ...
34+
get_response: GetResponseCallable = ...
35+
def __init__(self, get_response: GetResponseCallable = ...) -> None: ...
3536
def __call__(self, request: HttpRequest) -> HttpResponse: ...

0 commit comments

Comments
 (0)