Skip to content

Commit a038db8

Browse files
Fix type of data parameter in get test requests (#267)
Fix type of data parameter in get test requests. Lists of two-tuples of strings are allowed Co-authored-by: Marti Raudsepp <[email protected]>
1 parent 6a972bb commit a038db8

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

rest_framework-stubs/test.pyi

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from collections.abc import Iterable, Mapping
12
from typing import Any
23

34
import coreapi
@@ -13,6 +14,13 @@ from django.test.client import RequestFactory as DjangoRequestFactory
1314
from rest_framework.authtoken.models import Token
1415
from rest_framework.request import Request
1516
from rest_framework.response import _MonkeyPatchedResponse
17+
from typing_extensions import TypeAlias
18+
19+
_GetDataType: TypeAlias = (
20+
Mapping[str, str | bytes | int | Iterable[str | bytes | int]]
21+
| Iterable[tuple[str, str | bytes | int | Iterable[str | bytes | int]]]
22+
| None
23+
)
1624

1725
def force_authenticate(
1826
request: HttpRequest, user: AnonymousUser | AbstractBaseUser | None = ..., token: Token | None = ...
@@ -50,7 +58,7 @@ class APIRequestFactory(DjangoRequestFactory):
5058
renderer_classes: Any
5159
def __init__(self, enforce_csrf_checks: bool = ..., **defaults: Any) -> None: ...
5260
def request(self, **kwargs: Any) -> Request: ... # type: ignore[override]
53-
def get(self, path: str, data: dict[str, Any] | str | None = ..., follow: bool = ..., **extra: Any) -> Request: ... # type: ignore[override]
61+
def get(self, path: str, data: _GetDataType = ..., follow: bool = ..., **extra: Any) -> Request: ... # type: ignore[override]
5462
def post(self, path: str, data: Any | None = ..., format: str | None = ..., content_type: str | None = ..., follow: bool = ..., **extra: Any) -> Request: ... # type: ignore[override]
5563
def put(self, path: str, data: Any | None = ..., format: str | None = ..., content_type: str | None = ..., follow: bool = ..., **extra: Any) -> Request: ... # type: ignore[override]
5664
def patch(self, path: str, data: Any | None = ..., format: str | None = ..., content_type: str | None = ..., follow: bool = ..., **extra: Any) -> Request: ... # type: ignore[override]
@@ -68,7 +76,7 @@ class APIClient(APIRequestFactory, DjangoClient):
6876
def credentials(self, **kwargs: Any): ...
6977
def force_authenticate(self, user: AnonymousUser | AbstractBaseUser = ..., token: Token | None = ...) -> None: ...
7078
def request(self, **kwargs: Any) -> _MonkeyPatchedResponse: ... # type: ignore[override]
71-
def get(self, path: str, data: dict[str, Any] | str | None = ..., follow: bool = ..., **extra: Any) -> _MonkeyPatchedResponse: ... # type: ignore[override]
79+
def get(self, path: str, data: _GetDataType = ..., follow: bool = ..., **extra: Any) -> _MonkeyPatchedResponse: ... # type: ignore[override]
7280
def post(self, path: str, data: Any | None = ..., format: str | None = ..., content_type: str | None = ..., follow: bool = ..., **extra: Any) -> _MonkeyPatchedResponse: ... # type: ignore[override]
7381
def put(self, path: str, data: Any | None = ..., format: str | None = ..., content_type: str | None = ..., follow: bool = ..., **extra: Any) -> _MonkeyPatchedResponse: ... # type: ignore[override]
7482
def patch(self, path: str, data: Any | None = ..., format: str | None = ..., content_type: str | None = ..., follow: bool = ..., **extra: Any) -> _MonkeyPatchedResponse: ... # type: ignore[override]

scripts/typecheck_tests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
'expected "Iterable[Any]"',
150150
'Value of type variable "_MT" of "paginate_queryset" of "CursorPagination" cannot be "object"',
151151
'Value of type "Union[object, Any]" is not indexable',
152+
'Argument 2 to "get" of "APIRequestFactory" has incompatible type "Dict[str, object]"',
152153
],
153154
"test_parsers.py": ['"object" has no attribute', 'Argument 1 to "isnan" has incompatible type'],
154155
"test_permissions.py": [
@@ -243,6 +244,11 @@
243244
],
244245
"test_viewsets.py": [
245246
'(expression has type "None", variable has type "Request")',
247+
'Argument 2 to "get" of "APIRequestFactory" has incompatible type "str"',
248+
"note: Following member(s) of ",
249+
"note: Expected:",
250+
"note: Got:",
251+
"note: def ",
246252
],
247253
"utils.py": ['Invalid signature "Callable[[BadType], Any]"'],
248254
}

0 commit comments

Comments
 (0)