Skip to content

Commit

Permalink
Reflect the deprecation of get_response being None. (#1086)
Browse files Browse the repository at this point in the history
* Reflect the deprecation of get_response being None.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>

* 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 <p359101898@gmail.com>

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
  • Loading branch information
PIG208 authored Sep 3, 2022
1 parent 12e8009 commit c0414c7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion django-stubs/middleware/cache.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions django-stubs/utils/deprecation.pyi
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -27,9 +27,10 @@ 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: 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: ...

0 comments on commit c0414c7

Please sign in to comment.