Skip to content

Commit eff8175

Browse files
Add typing for data parameter in get from RequestFactory, test Client and urlencode (#1297)
Co-authored-by: Nils VAN ZUIJLEN <[email protected]>
1 parent 57b604e commit eff8175

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

django-stubs/test/client.pyi

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from collections.abc import Awaitable, Callable, Iterable, Iterator
1+
from collections.abc import Awaitable, Callable, Iterable, Iterator, Mapping
22
from io import BytesIO
33
from json import JSONEncoder
44
from re import Pattern
@@ -16,6 +16,7 @@ from django.http.response import HttpResponseBase
1616
from django.template.base import Template
1717
from django.test.utils import ContextList
1818
from django.urls import ResolverMatch
19+
from typing_extensions import TypeAlias
1920

2021
BOUNDARY: str
2122
MULTIPART_CONTENT: str
@@ -58,14 +59,18 @@ class AsyncClientHandler(BaseHandler):
5859
def encode_multipart(boundary: str, data: dict[str, Any]) -> bytes: ...
5960
def encode_file(boundary: str, key: str, file: Any) -> list[bytes]: ...
6061

62+
_GetDataType: TypeAlias = (
63+
Mapping[str, str | bytes | Iterable[str | bytes]] | Iterable[tuple[str, str | bytes | Iterable[str | bytes]]] | None
64+
)
65+
6166
class _RequestFactory(Generic[_T]):
6267
json_encoder: type[JSONEncoder]
6368
defaults: dict[str, str]
6469
cookies: SimpleCookie
6570
errors: BytesIO
6671
def __init__(self, *, json_encoder: type[JSONEncoder] = ..., **defaults: Any) -> None: ...
6772
def request(self, **request: Any) -> _T: ...
68-
def get(self, path: str, data: Any = ..., secure: bool = ..., **extra: Any) -> _T: ...
73+
def get(self, path: str, data: _GetDataType = ..., secure: bool = ..., **extra: Any) -> _T: ...
6974
def post(self, path: str, data: Any = ..., content_type: str = ..., secure: bool = ..., **extra: Any) -> _T: ...
7075
def head(self, path: str, data: Any = ..., secure: bool = ..., **extra: Any) -> _T: ...
7176
def trace(self, path: str, secure: bool = ..., **extra: Any) -> _T: ...
@@ -129,7 +134,7 @@ class Client(ClientMixin, _RequestFactory[_MonkeyPatchedWSGIResponse]):
129134
# Silence type warnings, since this class overrides arguments and return types in an unsafe manner.
130135
def request(self, **request: Any) -> _MonkeyPatchedWSGIResponse: ...
131136
def get( # type: ignore
132-
self, path: str, data: Any = ..., follow: bool = ..., secure: bool = ..., **extra: Any
137+
self, path: str, data: _GetDataType = ..., follow: bool = ..., secure: bool = ..., **extra: Any
133138
) -> _MonkeyPatchedWSGIResponse: ...
134139
def post( # type: ignore
135140
self, path: str, data: Any = ..., content_type: str = ..., follow: bool = ..., secure: bool = ..., **extra: Any

django-stubs/utils/http.pyi

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from collections.abc import Iterable
1+
from collections.abc import Iterable, Mapping
22
from re import Pattern
3-
from typing import Any
43

54
ETAG_MATCH: Pattern[str]
65
MONTHS: list[str]
@@ -14,7 +13,12 @@ def urlquote(url: str, safe: str = ...) -> str: ...
1413
def urlquote_plus(url: str, safe: str = ...) -> str: ...
1514
def urlunquote(quoted_url: str) -> str: ...
1615
def urlunquote_plus(quoted_url: str) -> str: ...
17-
def urlencode(query: Any, doseq: bool = ...) -> str: ...
16+
def urlencode(
17+
query: Mapping[str, str | bytes | Iterable[str | bytes]]
18+
| Iterable[tuple[str, str | bytes | Iterable[str | bytes]]]
19+
| None,
20+
doseq: bool = ...,
21+
) -> str: ...
1822
def http_date(epoch_seconds: float | None = ...) -> str: ...
1923
def parse_http_date(date: str) -> int: ...
2024
def parse_http_date_safe(date: str) -> int | None: ...

0 commit comments

Comments
 (0)