From 973b7076450c7efd3d9f58fc7f343e1fdf3c84e2 Mon Sep 17 00:00:00 2001 From: Nils VAN ZUIJLEN Date: Tue, 18 Oct 2022 14:53:32 +0200 Subject: [PATCH 1/3] Fix type of data parameter in get test requests Lists of two-tuples of strings are allowed Co-authored-by: Marti Raudsepp --- rest_framework-stubs/test.pyi | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/rest_framework-stubs/test.pyi b/rest_framework-stubs/test.pyi index 56f30ed95..b91cbb685 100644 --- a/rest_framework-stubs/test.pyi +++ b/rest_framework-stubs/test.pyi @@ -1,4 +1,6 @@ +from collections.abc import Iterable, Mapping from typing import Any +from typing_extensions import TypeAlias import coreapi import requests @@ -14,6 +16,12 @@ from rest_framework.authtoken.models import Token from rest_framework.request import Request from rest_framework.response import _MonkeyPatchedResponse +_GetDataType: TypeAlias = ( + Mapping[str, str | bytes | int | Iterable[str | bytes | int]] + | Iterable[tuple[str, str | bytes | int | Iterable[str | bytes | int]]] + | None +) + def force_authenticate( request: HttpRequest, user: AnonymousUser | AbstractBaseUser | None = ..., token: Token | None = ... ) -> None: ... @@ -50,7 +58,7 @@ class APIRequestFactory(DjangoRequestFactory): renderer_classes: Any def __init__(self, enforce_csrf_checks: bool = ..., **defaults: Any) -> None: ... def request(self, **kwargs: Any) -> Request: ... # type: ignore[override] - def get(self, path: str, data: dict[str, Any] | str | None = ..., follow: bool = ..., **extra: Any): ... # type: ignore[override] + def get(self, path: str, data: _GetDataType = ..., follow: bool = ..., **extra: Any): ... # type: ignore[override] def post(self, path: str, data: Any | None = ..., format: str | None = ..., content_type: str | None = ..., follow: bool = ..., **extra: Any) -> Request: ... # type: ignore[override] def put(self, path: str, data: Any | None = ..., format: str | None = ..., content_type: str | None = ..., follow: bool = ..., **extra: Any) -> Request: ... # type: ignore[override] 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): def credentials(self, **kwargs: Any): ... def force_authenticate(self, user: AnonymousUser | AbstractBaseUser = ..., token: Token | None = ...) -> None: ... def request(self, **kwargs: Any) -> _MonkeyPatchedResponse: ... # type: ignore[override] - def get(self, path: str, data: dict[str, Any] | str | None = ..., follow: bool = ..., **extra: Any): ... # type: ignore[override] + def get(self, path: str, data: _GetDataType = ..., follow: bool = ..., **extra: Any): ... # type: ignore[override] def post(self, path: str, data: Any | None = ..., format: str | None = ..., content_type: str | None = ..., follow: bool = ..., **extra: Any) -> _MonkeyPatchedResponse: ... # type: ignore[override] def put(self, path: str, data: Any | None = ..., format: str | None = ..., content_type: str | None = ..., follow: bool = ..., **extra: Any) -> _MonkeyPatchedResponse: ... # type: ignore[override] def patch(self, path: str, data: Any | None = ..., format: str | None = ..., content_type: str | None = ..., follow: bool = ..., **extra: Any) -> _MonkeyPatchedResponse: ... # type: ignore[override] From 12bb19ce872f77aca7d6626c7010aa933ac8e048 Mon Sep 17 00:00:00 2001 From: Marti Raudsepp Date: Wed, 22 Feb 2023 12:06:45 +0200 Subject: [PATCH 2/3] isort & typecheck ignores --- rest_framework-stubs/test.pyi | 2 +- scripts/typecheck_tests.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/rest_framework-stubs/test.pyi b/rest_framework-stubs/test.pyi index 9ffd861cd..62cc38b3d 100644 --- a/rest_framework-stubs/test.pyi +++ b/rest_framework-stubs/test.pyi @@ -1,6 +1,5 @@ from collections.abc import Iterable, Mapping from typing import Any -from typing_extensions import TypeAlias import coreapi import requests @@ -15,6 +14,7 @@ from django.test.client import RequestFactory as DjangoRequestFactory from rest_framework.authtoken.models import Token from rest_framework.request import Request from rest_framework.response import _MonkeyPatchedResponse +from typing_extensions import TypeAlias _GetDataType: TypeAlias = ( Mapping[str, str | bytes | int | Iterable[str | bytes | int]] diff --git a/scripts/typecheck_tests.py b/scripts/typecheck_tests.py index 1b215d3ed..eebe8175a 100644 --- a/scripts/typecheck_tests.py +++ b/scripts/typecheck_tests.py @@ -57,6 +57,7 @@ '"_MonkeyPatchedWSGIResponse" has no attribute "data"', '" defined here', '" has no attribute "id"', + 'Argument 2 to "get" of "APIRequestFactory" has incompatible type "Dict[str, object]"', ], "authentication": [ 'Argument 1 to "post" of "APIClient" has incompatible type "None"; expected "str', @@ -243,6 +244,10 @@ ], "test_viewsets.py": [ '(expression has type "None", variable has type "Request")', + "note: Following member(s) of ", + "note: Expected:", + "note: Got:", + "note: def ", ], "utils.py": ['Invalid signature "Callable[[BadType], Any]"'], } From 0734f203192da93a8d76f3de6bc66eb15b63f43c Mon Sep 17 00:00:00 2001 From: Marti Raudsepp Date: Wed, 22 Feb 2023 12:09:46 +0200 Subject: [PATCH 3/3] Fix typecheck ignores again --- scripts/typecheck_tests.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/typecheck_tests.py b/scripts/typecheck_tests.py index eebe8175a..5d9f18e32 100644 --- a/scripts/typecheck_tests.py +++ b/scripts/typecheck_tests.py @@ -57,7 +57,6 @@ '"_MonkeyPatchedWSGIResponse" has no attribute "data"', '" defined here', '" has no attribute "id"', - 'Argument 2 to "get" of "APIRequestFactory" has incompatible type "Dict[str, object]"', ], "authentication": [ 'Argument 1 to "post" of "APIClient" has incompatible type "None"; expected "str', @@ -150,6 +149,7 @@ 'expected "Iterable[Any]"', 'Value of type variable "_MT" of "paginate_queryset" of "CursorPagination" cannot be "object"', 'Value of type "Union[object, Any]" is not indexable', + 'Argument 2 to "get" of "APIRequestFactory" has incompatible type "Dict[str, object]"', ], "test_parsers.py": ['"object" has no attribute', 'Argument 1 to "isnan" has incompatible type'], "test_permissions.py": [ @@ -244,6 +244,7 @@ ], "test_viewsets.py": [ '(expression has type "None", variable has type "Request")', + 'Argument 2 to "get" of "APIRequestFactory" has incompatible type "str"', "note: Following member(s) of ", "note: Expected:", "note: Got:",