Skip to content

Commit

Permalink
Type get_response with a callback protocol.
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
PIG208 committed Aug 10, 2022
1 parent 99a6cef commit 3d71af0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion django-stubs/utils/deprecation.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class DeprecationInstanceCheck(type):
def __instancecheck__(self, instance: Any): ...

class GetResponseCallable(Protocol):
def __call__(request: HttpRequest) -> HttpResponse: ...
def __call__(self, __request: HttpRequest) -> HttpResponse: ...

class MiddlewareMixin:
get_response: GetResponseCallable = ...
Expand Down
5 changes: 3 additions & 2 deletions mypy_django_plugin/transformers/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ def resolve_str_promise_attribute(ctx: AttributeContext) -> MypyType:
return AnyType(TypeOfAny.from_error)

if isinstance(method.type, CallableType):
# The proxied str methods are only meant to be used as instance methods.
if method.type.arg_names[0] != "self":
return method.type
# The proxied str methods are meant to be used as instance methods.
# We need to drop the first `self` argument in them.
assert method.type.arg_names[0] == "self"
return method.type.copy_modified(
arg_kinds=method.type.arg_kinds[1:],
arg_names=method.type.arg_names[1:],
Expand Down

0 comments on commit 3d71af0

Please sign in to comment.