diff --git a/rest_framework-stubs/test.pyi b/rest_framework-stubs/test.pyi index 955e89f2f..46aec070d 100644 --- a/rest_framework-stubs/test.pyi +++ b/rest_framework-stubs/test.pyi @@ -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: ... @@ -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): ... diff --git a/test-data/plugins.ini b/test-data/plugins.ini index ea6372bbf..defbf6782 100644 --- a/test-data/plugins.ini +++ b/test-data/plugins.ini @@ -1,5 +1,6 @@ [mypy] ignore_missing_imports = True +check_untyped_defs = True plugins = mypy_django_plugin.main, mypy_drf_plugin.main diff --git a/test-data/typecheck/test_client.test b/test-data/typecheck/test_client.test new file mode 100644 index 000000000..e566e1269 --- /dev/null +++ b/test-data/typecheck/test_client.test @@ -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] \ No newline at end of file