From 680d4f53ea87f0a6a75ddb257035bfc859deed24 Mon Sep 17 00:00:00 2001 From: Maxwell Muoto <41130755+max-muoto@users.noreply.github.com> Date: Sun, 30 Jun 2024 14:48:36 -0500 Subject: [PATCH 1/7] Add overloads --- aiohttp/client.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/aiohttp/client.py b/aiohttp/client.py index 7a4db0db476..4afe99f1144 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -32,6 +32,8 @@ TypeVar, Union, final, + TypedDict, + overload, ) from multidict import CIMultiDict, MultiDict, MultiDictProxy, istr @@ -150,6 +152,34 @@ SSLContext = None +class _RequestOptions(TypedDict, total=False): + params: Union[Mapping[str, str], None] + data: Any + json: Any + cookies: Union[LooseCookies, None] + headers: Union[LooseHeaders, None] + skip_auto_headers: Union[Iterable[str], None] + auth: Union[BasicAuth, None] + allow_redirects: bool + max_redirects: int + compress: Union[str, None] + chunked: Union[bool, None] + expect100: bool + raise_for_status: Union[None, bool, Callable[[ClientResponse], Awaitable[None]]] + read_until_eof: bool + proxy: Union[StrOrURL, None] + proxy_auth: Union[BasicAuth, None] + timeout: "Union[ClientTimeout, _SENTINEL, None]" + ssl: Union[SSLContext, bool, Fingerprint] + server_hostname: Union[str, None] + proxy_headers: Union[LooseHeaders, None] + trace_request_ctx: Union[SimpleNamespace, None] + read_bufsize: Union[int, None] + auto_decompress: Union[bool, None] + max_line_size: Union[int, None] + max_field_size: Union[int, None] + + @dataclasses.dataclass(frozen=True) class ClientTimeout: total: Optional[float] = None @@ -187,6 +217,44 @@ class ClientTimeout: class ClientSession: """First-class interface for making HTTP requests.""" + if sys.version_info >= (3, 11): + from typing import Unpack + + @overload + def get( + self, + url: StrOrURL, + **kwargs: Unpack["_RequestOptions"], + ) -> "_RequestContextManager": ... + + @overload + def put( + self, + url: StrOrURL, + **kwargs: Unpack["_RequestOptions"], + ) -> "_RequestContextManager": ... + + @overload + def post( + self, + url: StrOrURL, + **kwargs: Unpack["_RequestOptions"], + ) -> "_RequestContextManager": ... + + @overload + def delete( + self, + url: StrOrURL, + **kwargs: Unpack["_RequestOptions"], + ) -> "_RequestContextManager": ... + + @overload + def head( + self, + url: StrOrURL, + **kwargs: Unpack["_RequestOptions"], + ) -> "_RequestContextManager": ... + __slots__ = ( "_base_url", "_source_traceback", From 921261762d156a109ca84e894daa617ab662277f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 30 Jun 2024 19:49:50 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- aiohttp/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aiohttp/client.py b/aiohttp/client.py index 4afe99f1144..91bdfaef36b 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -29,10 +29,10 @@ Set, Tuple, Type, + TypedDict, TypeVar, Union, final, - TypedDict, overload, ) From 2df4510ef4c8237f6ed8771b234ec8df95cbb8c7 Mon Sep 17 00:00:00 2001 From: Maxwell Muoto <41130755+max-muoto@users.noreply.github.com> Date: Sun, 30 Jun 2024 14:55:17 -0500 Subject: [PATCH 3/7] Add changes --- CHANGES/8463.misc.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 CHANGES/8463.misc.rst diff --git a/CHANGES/8463.misc.rst b/CHANGES/8463.misc.rst new file mode 100644 index 00000000000..1d42136ebd7 --- /dev/null +++ b/CHANGES/8463.misc.rst @@ -0,0 +1 @@ +Added a 3.11-specific overloads to ``ClientSession`` -- by :user:`max-muoto`. From c8368cf59d672febb7f88b2b017a1997c28491ef Mon Sep 17 00:00:00 2001 From: Maxwell Muoto <41130755+max-muoto@users.noreply.github.com> Date: Fri, 5 Jul 2024 17:36:34 -0500 Subject: [PATCH 4/7] Avoid overloading --- aiohttp/client.py | 183 +++++++++++++++++++++++++--------------------- 1 file changed, 98 insertions(+), 85 deletions(-) diff --git a/aiohttp/client.py b/aiohttp/client.py index 91bdfaef36b..9fc6b0fb1da 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -217,44 +217,6 @@ class ClientTimeout: class ClientSession: """First-class interface for making HTTP requests.""" - if sys.version_info >= (3, 11): - from typing import Unpack - - @overload - def get( - self, - url: StrOrURL, - **kwargs: Unpack["_RequestOptions"], - ) -> "_RequestContextManager": ... - - @overload - def put( - self, - url: StrOrURL, - **kwargs: Unpack["_RequestOptions"], - ) -> "_RequestContextManager": ... - - @overload - def post( - self, - url: StrOrURL, - **kwargs: Unpack["_RequestOptions"], - ) -> "_RequestContextManager": ... - - @overload - def delete( - self, - url: StrOrURL, - **kwargs: Unpack["_RequestOptions"], - ) -> "_RequestContextManager": ... - - @overload - def head( - self, - url: StrOrURL, - **kwargs: Unpack["_RequestOptions"], - ) -> "_RequestContextManager": ... - __slots__ = ( "_base_url", "_source_traceback", @@ -1054,61 +1016,112 @@ def _prepare_headers(self, headers: Optional[LooseHeaders]) -> "CIMultiDict[str] added_names.add(key) return result - def get( - self, url: StrOrURL, *, allow_redirects: bool = True, **kwargs: Any - ) -> "_RequestContextManager": - """Perform HTTP GET request.""" - return _RequestContextManager( - self._request(hdrs.METH_GET, url, allow_redirects=allow_redirects, **kwargs) - ) + if sys.version_info >= (3, 11) and TYPE_CHECKING: + import typing - def options( - self, url: StrOrURL, *, allow_redirects: bool = True, **kwargs: Any - ) -> "_RequestContextManager": - """Perform HTTP OPTIONS request.""" - return _RequestContextManager( - self._request( - hdrs.METH_OPTIONS, url, allow_redirects=allow_redirects, **kwargs + def get( + self, + url: StrOrURL, + **kwargs: typing.Unpack[_RequestOptions], + ) -> "_RequestContextManager": ... + + def options( + self, + url: StrOrURL, + **kwargs: typing.Unpack[_RequestOptions], + ) -> "_RequestContextManager": ... + + def head( + self, + url: StrOrURL, + **kwargs: typing.Unpack[_RequestOptions], + ) -> "_RequestContextManager": ... + + def post( + self, + url: StrOrURL, + **kwargs: typing.Unpack[_RequestOptions], + ) -> "_RequestContextManager": ... + + def put( + self, + url: StrOrURL, + **kwargs: typing.Unpack[_RequestOptions], + ) -> "_RequestContextManager": ... + + def patch( + self, + url: StrOrURL, + **kwargs: typing.Unpack[_RequestOptions], + ) -> "_RequestContextManager": ... + + def delete( + self, + url: StrOrURL, + **kwargs: typing.Unpack[_RequestOptions], + ) -> "_RequestContextManager": ... + + else: + + def get( + self, url: StrOrURL, *, allow_redirects: bool = True, **kwargs: Any + ) -> "_RequestContextManager": + """Perform HTTP GET request.""" + return _RequestContextManager( + self._request( + hdrs.METH_GET, url, allow_redirects=allow_redirects, **kwargs + ) ) - ) - def head( - self, url: StrOrURL, *, allow_redirects: bool = False, **kwargs: Any - ) -> "_RequestContextManager": - """Perform HTTP HEAD request.""" - return _RequestContextManager( - self._request( - hdrs.METH_HEAD, url, allow_redirects=allow_redirects, **kwargs + def options( + self, url: StrOrURL, *, allow_redirects: bool = True, **kwargs: Any + ) -> "_RequestContextManager": + """Perform HTTP OPTIONS request.""" + return _RequestContextManager( + self._request( + hdrs.METH_OPTIONS, url, allow_redirects=allow_redirects, **kwargs + ) ) - ) - def post( - self, url: StrOrURL, *, data: Any = None, **kwargs: Any - ) -> "_RequestContextManager": - """Perform HTTP POST request.""" - return _RequestContextManager( - self._request(hdrs.METH_POST, url, data=data, **kwargs) - ) + def head( + self, url: StrOrURL, *, allow_redirects: bool = False, **kwargs: Any + ) -> "_RequestContextManager": + """Perform HTTP HEAD request.""" + return _RequestContextManager( + self._request( + hdrs.METH_HEAD, url, allow_redirects=allow_redirects, **kwargs + ) + ) - def put( - self, url: StrOrURL, *, data: Any = None, **kwargs: Any - ) -> "_RequestContextManager": - """Perform HTTP PUT request.""" - return _RequestContextManager( - self._request(hdrs.METH_PUT, url, data=data, **kwargs) - ) + def post( + self, url: StrOrURL, *, data: Any = None, **kwargs: Any + ) -> "_RequestContextManager": + """Perform HTTP POST request.""" + return _RequestContextManager( + self._request(hdrs.METH_POST, url, data=data, **kwargs) + ) - def patch( - self, url: StrOrURL, *, data: Any = None, **kwargs: Any - ) -> "_RequestContextManager": - """Perform HTTP PATCH request.""" - return _RequestContextManager( - self._request(hdrs.METH_PATCH, url, data=data, **kwargs) - ) + def put( + self, url: StrOrURL, *, data: Any = None, **kwargs: Any + ) -> "_RequestContextManager": + """Perform HTTP PUT request.""" + return _RequestContextManager( + self._request(hdrs.METH_PUT, url, data=data, **kwargs) + ) - def delete(self, url: StrOrURL, **kwargs: Any) -> "_RequestContextManager": - """Perform HTTP DELETE request.""" - return _RequestContextManager(self._request(hdrs.METH_DELETE, url, **kwargs)) + def patch( + self, url: StrOrURL, *, data: Any = None, **kwargs: Any + ) -> "_RequestContextManager": + """Perform HTTP PATCH request.""" + return _RequestContextManager( + self._request(hdrs.METH_PATCH, url, data=data, **kwargs) + ) + + def delete(self, url: StrOrURL, **kwargs: Any) -> "_RequestContextManager": + """Perform HTTP DELETE request.""" + return _RequestContextManager( + self._request(hdrs.METH_DELETE, url, **kwargs) + ) async def close(self) -> None: """Close underlying connector. From ac10ab0d65526dabfc4c469c3eee87c42764c695 Mon Sep 17 00:00:00 2001 From: Maxwell Muoto <41130755+max-muoto@users.noreply.github.com> Date: Fri, 5 Jul 2024 17:39:05 -0500 Subject: [PATCH 5/7] Remove unused import --- aiohttp/client.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/aiohttp/client.py b/aiohttp/client.py index 9fc6b0fb1da..caf8d6d0ac7 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -33,7 +33,6 @@ TypeVar, Union, final, - overload, ) from multidict import CIMultiDict, MultiDict, MultiDictProxy, istr @@ -353,8 +352,9 @@ def __init__( def __init_subclass__(cls: Type["ClientSession"]) -> None: raise TypeError( - "Inheritance class {} from ClientSession " - "is forbidden".format(cls.__name__) + "Inheritance class {} from ClientSession " "is forbidden".format( + cls.__name__ + ) ) def __del__(self, _warnings: Any = warnings) -> None: From 3d4155fd818e0d21647a822ba8c97779ad89cfdf Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 5 Jul 2024 22:39:36 +0000 Subject: [PATCH 6/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- aiohttp/client.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/aiohttp/client.py b/aiohttp/client.py index caf8d6d0ac7..b3ee8648dca 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -352,9 +352,8 @@ def __init__( def __init_subclass__(cls: Type["ClientSession"]) -> None: raise TypeError( - "Inheritance class {} from ClientSession " "is forbidden".format( - cls.__name__ - ) + "Inheritance class {} from ClientSession " + "is forbidden".format(cls.__name__) ) def __del__(self, _warnings: Any = warnings) -> None: From e4af6b13c7e4d7ebc5dcac5a7d38bdf6a13619fc Mon Sep 17 00:00:00 2001 From: Maxwell Muoto <41130755+max-muoto@users.noreply.github.com> Date: Fri, 5 Jul 2024 19:16:40 -0500 Subject: [PATCH 7/7] Address feedback --- aiohttp/client.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/aiohttp/client.py b/aiohttp/client.py index caf8d6d0ac7..0a0f2b12e47 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -150,6 +150,9 @@ else: SSLContext = None +if sys.version_info >= (3, 11) and TYPE_CHECKING: + from typing import Unpack + class _RequestOptions(TypedDict, total=False): params: Union[Mapping[str, str], None] @@ -1017,48 +1020,47 @@ def _prepare_headers(self, headers: Optional[LooseHeaders]) -> "CIMultiDict[str] return result if sys.version_info >= (3, 11) and TYPE_CHECKING: - import typing def get( self, url: StrOrURL, - **kwargs: typing.Unpack[_RequestOptions], + **kwargs: Unpack[_RequestOptions], ) -> "_RequestContextManager": ... def options( self, url: StrOrURL, - **kwargs: typing.Unpack[_RequestOptions], + **kwargs: Unpack[_RequestOptions], ) -> "_RequestContextManager": ... def head( self, url: StrOrURL, - **kwargs: typing.Unpack[_RequestOptions], + **kwargs: Unpack[_RequestOptions], ) -> "_RequestContextManager": ... def post( self, url: StrOrURL, - **kwargs: typing.Unpack[_RequestOptions], + **kwargs: Unpack[_RequestOptions], ) -> "_RequestContextManager": ... def put( self, url: StrOrURL, - **kwargs: typing.Unpack[_RequestOptions], + **kwargs: Unpack[_RequestOptions], ) -> "_RequestContextManager": ... def patch( self, url: StrOrURL, - **kwargs: typing.Unpack[_RequestOptions], + **kwargs: Unpack[_RequestOptions], ) -> "_RequestContextManager": ... def delete( self, url: StrOrURL, - **kwargs: typing.Unpack[_RequestOptions], + **kwargs: Unpack[_RequestOptions], ) -> "_RequestContextManager": ... else: