From 3d3f09382ec97598891d262200f12ba7cd4693fb Mon Sep 17 00:00:00 2001 From: Luke Petre Date: Mon, 9 Sep 2024 03:31:34 +0100 Subject: [PATCH] Use Query typedef from yarl for params (#8564) ## What do these changes do? The type signature for params does not match the functionality provided by yarl. Update the type signature to expose the full feature set of yarl. ## Are there changes in behavior for the user? No changes to the user, expect that type checking will now match the runtime functionality. ## Is it a substantial burden for the maintainers to support this? The only burden I see is that the type signature in yarl is not exported in the package, which means these two files should be kept in sync. I would recommend yarl be updated to export the type signature and aiohttp could use that directly. ## Related issue number Fixes #8563 ## Checklist - [x] I think the code is well written - [ ] Unit tests for the changes exist - [ ] Documentation reflects the changes - [ ] If you provide code modification, please add yourself to `CONTRIBUTORS.txt` * The format is <Name> <Surname>. * Please keep alphabetical order, the file is sorted by names. - [ ] Add a new news fragment into the `CHANGES/` folder * name it `..rst` (e.g. `588.bugfix.rst`) * if you don't have an issue number, change it to the pull request number after creating the PR * `.bugfix`: A bug fix for something the maintainers deemed an improper undesired behavior that got corrected to match pre-agreed expectations. * `.feature`: A new behavior, public APIs. That sort of stuff. * `.deprecation`: A declaration of future API removals and breaking changes in behavior. * `.breaking`: When something public is removed in a breaking way. Could be deprecated in an earlier release. * `.doc`: Notable updates to the documentation structure or build process. * `.packaging`: Notes for downstreams about unobvious side effects and tooling. Changes in the test invocation considerations and runtime assumptions. * `.contrib`: Stuff that affects the contributor experience. e.g. Running tests, building the docs, setting up the development environment. * `.misc`: Changes that are hard to assign to any of the above categories. * Make sure to use full sentences with correct case and punctuation, for example: ```rst Fixed issue with non-ascii contents in doctest text files -- by :user:`contributor-gh-handle`. ``` Use the past tense or the present tense a non-imperative mood, referring to what's changed compared to the last released version of this project. --------- Co-authored-by: J. Nick Koston Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sam Bull (cherry picked from commit b543677e67424df5f1ca2144c462107a2f594556) --- CHANGES/8564.feature.rst | 1 + aiohttp/client.py | 12 ++++++------ aiohttp/client_reqrep.py | 3 ++- aiohttp/typedefs.py | 13 +++++++++++++ 4 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 CHANGES/8564.feature.rst 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