diff --git a/CHANGES/8564.feature.rst b/CHANGES/8564.feature.rst new file mode 100644 index 00000000000..1eac9d12217 --- /dev/null +++ b/CHANGES/8564.feature.rst @@ -0,0 +1 @@ +Improved type on ``params`` to match the underlying type allowed by ``yarl`` -- by :user:`lpetre`. diff --git a/aiohttp/client.py b/aiohttp/client.py index 3c4a0f97c04..1e5c1448ce5 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -101,7 +101,7 @@ from .http_websocket import WSHandshakeError, WSMessage, ws_ext_gen, ws_ext_parse from .streams import FlowControlDataQueue from .tracing import Trace, TraceConfig -from .typedefs import JSONEncoder, LooseCookies, LooseHeaders, StrOrURL +from .typedefs import JSONEncoder, LooseCookies, LooseHeaders, Query, StrOrURL __all__ = ( # client_exceptions @@ -161,7 +161,7 @@ class _RequestOptions(TypedDict, total=False): - params: Union[Mapping[str, Union[str, int]], str, None] + params: Query data: Any json: Any cookies: Union[LooseCookies, None] @@ -455,7 +455,7 @@ async def _request( method: str, str_or_url: StrOrURL, *, - params: Optional[Mapping[str, str]] = None, + params: Query = None, data: Any = None, json: Any = None, cookies: Optional[LooseCookies] = None, @@ -835,7 +835,7 @@ def ws_connect( heartbeat: Optional[float] = None, auth: Optional[BasicAuth] = None, origin: Optional[str] = None, - params: Optional[Mapping[str, str]] = None, + params: Query = None, headers: Optional[LooseHeaders] = None, proxy: Optional[StrOrURL] = None, proxy_auth: Optional[BasicAuth] = None, @@ -887,7 +887,7 @@ async def _ws_connect( heartbeat: Optional[float] = None, auth: Optional[BasicAuth] = None, origin: Optional[str] = None, - params: Optional[Mapping[str, str]] = None, + params: Query = None, headers: Optional[LooseHeaders] = None, proxy: Optional[StrOrURL] = None, proxy_auth: Optional[BasicAuth] = None, @@ -1452,7 +1452,7 @@ def request( method: str, url: StrOrURL, *, - params: Optional[Mapping[str, str]] = None, + params: Query = None, data: Any = None, json: Any = None, headers: Optional[LooseHeaders] = None, diff --git a/aiohttp/client_reqrep.py b/aiohttp/client_reqrep.py index e0232a40c4c..79073cb895b 100644 --- a/aiohttp/client_reqrep.py +++ b/aiohttp/client_reqrep.py @@ -67,6 +67,7 @@ JSONDecoder, LooseCookies, LooseHeaders, + Query, RawHeaders, ) @@ -262,7 +263,7 @@ def __init__( method: str, url: URL, *, - params: Optional[Mapping[str, str]] = None, + params: Query = None, headers: Optional[LooseHeaders] = None, skip_auto_headers: Optional[Iterable[str]] = None, data: Any = None, diff --git a/aiohttp/typedefs.py b/aiohttp/typedefs.py index 9fb21c15f83..2e285fa2561 100644 --- a/aiohttp/typedefs.py +++ b/aiohttp/typedefs.py @@ -8,6 +8,7 @@ Iterable, Mapping, Protocol, + Sequence, Tuple, Union, ) @@ -15,6 +16,18 @@ from multidict import CIMultiDict, CIMultiDictProxy, MultiDict, MultiDictProxy, istr from yarl import URL +try: + # Available in yarl>=1.10.0 + from yarl import Query as _Query +except ImportError: # pragma: no cover + SimpleQuery = Union[str, int, float] # pragma: no cover + QueryVariable = Union[SimpleQuery, "Sequence[SimpleQuery]"] # pragma: no cover + _Query = Union[ # type: ignore[misc] # pragma: no cover + None, str, "Mapping[str, QueryVariable]", "Sequence[Tuple[str, QueryVariable]]" + ] + +Query = _Query + DEFAULT_JSON_ENCODER = json.dumps DEFAULT_JSON_DECODER = json.loads