Skip to content

Commit

Permalink
fix test client stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurnikov committed Mar 10, 2019
1 parent 13f884d commit d2fa65d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
38 changes: 36 additions & 2 deletions rest_framework-stubs/test.pyi
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from typing import Any, Optional, Sequence, Tuple
from typing import Any, Optional, Sequence, Tuple, Union, Dict

from django.db.models import Model
from django.http import HttpRequest, HttpResponse
from django.test import testcases
from django.test.client import Client as DjangoClient, ClientHandler, RequestFactory as DjangoRequestFactory
from rest_framework.compat import requests
from rest_framework.response import Response

def force_authenticate(request: HttpRequest, user: Optional[Model] = ..., token: Optional[Any] = ...) -> None: ...

Expand All @@ -22,9 +23,42 @@ class ForceAuthClientHandler(ClientHandler):
def __init__(self, *args: Any, **kwargs: Any): ...
def get_response(self, request: HttpRequest) -> HttpResponse: ...

class APIClient(APIRequestFactory, DjangoClient): # type: ignore
class APIClient(DjangoClient):
def credentials(self, **kwargs: Any): ...
def force_authenticate(self, user: Optional[Model] = ..., token: Optional[Any] = ...) -> None: ...
def request(self, **request: Any) -> Response: ...
def get(self, path: str, data: Any = ..., secure: bool = ..., **extra: Any) -> Response: ...
def post(
self, path: str, data: Any = ..., content_type: str = ..., secure: bool = ..., **extra: Any
) -> Response: ...
def head(self, path: str, data: Any = ..., secure: bool = ..., **extra: Any) -> Response: ...
def trace(self, path: str, secure: bool = ..., **extra: Any) -> Response: ...
def options(
self,
path: str,
data: Union[Dict[str, str], str] = ...,
content_type: str = ...,
secure: bool = ...,
**extra: Any
) -> Response: ...
def put(
self, path: str, data: Any = ..., content_type: str = ..., secure: bool = ..., **extra: Any
) -> Response: ...
def patch(
self, path: str, data: Any = ..., content_type: str = ..., secure: bool = ..., **extra: Any
) -> Response: ...
def delete(
self, path: str, data: Any = ..., content_type: str = ..., secure: bool = ..., **extra: Any
) -> Response: ...
def generic(
self,
method: str,
path: str,
data: Any = ...,
content_type: Optional[str] = ...,
secure: bool = ...,
**extra: Any
) -> Response: ...

class APITransactionTestCase(testcases.TransactionTestCase): ...
class APITestCase(testcases.TestCase): ...
Expand Down
1 change: 1 addition & 0 deletions test-data/plugins.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[mypy]
ignore_missing_imports = True
check_untyped_defs = True
plugins =
mypy_django_plugin.main,
mypy_drf_plugin.main
10 changes: 10 additions & 0 deletions test-data/typecheck/test_client.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[CASE test_client_methods_return_response_objects]
from rest_framework import test

class MyTestCase(test.APITestCase):
client: test.APIClient

def test_my_code(self):
client = test.APIClient()
reveal_type(client.post('http://google.com')) # E: Revealed type is 'rest_framework.response.Response'
[/CASE]

0 comments on commit d2fa65d

Please sign in to comment.