Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add full request typing signatures for session HTTP methods #8463

Merged
merged 10 commits into from
Jul 6, 2024
1 change: 1 addition & 0 deletions CHANGES/8463.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added a 3.11-specific overloads to ``ClientSession`` -- by :user:`max-muoto`.
68 changes: 68 additions & 0 deletions aiohttp/client.py
Dreamsorcerer marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
Set,
Tuple,
Type,
TypedDict,
TypeVar,
Union,
final,
overload,
)

from multidict import CIMultiDict, MultiDict, MultiDictProxy, istr
Expand Down Expand Up @@ -150,6 +152,34 @@
SSLContext = None


class _RequestOptions(TypedDict, total=False):
Dreamsorcerer marked this conversation as resolved.
Show resolved Hide resolved
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
Dreamsorcerer marked this conversation as resolved.
Show resolved Hide resolved
max_redirects: int
compress: Union[str, None]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This contradicts the documentation for compress as bool:

:param bool compress: Set to ``True`` if request has to be compressed
with deflate encoding. If `compress` can not be combined
with a *Content-Encoding* and *Content-Length* headers.
``None`` by default (optional).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's copied from _request(), not something related to this PR specifically. Feel free to open a PR to fix it. Looking at the code, it's the docs that are inaccurate, though we could expand the typing to allow bool:

elif self.compress:
if not isinstance(self.compress, str):
self.compress = "deflate"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, basically:
None/False/"" = Disable compression
str = use this compression method
Non-str (e.g. True) = use deflate

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely clear how the str is used though, I think maybe only deflate and gzip are valid..

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
Expand Down Expand Up @@ -187,6 +217,44 @@
class ClientSession:
"""First-class interface for making HTTP requests."""

if sys.version_info >= (3, 11):
Dreamsorcerer marked this conversation as resolved.
Show resolved Hide resolved
from typing import Unpack

@overload
def get(
self,
url: StrOrURL,
**kwargs: Unpack["_RequestOptions"],
) -> "_RequestContextManager": ...
Fixed Show fixed Hide fixed

@overload
def put(
self,
url: StrOrURL,
**kwargs: Unpack["_RequestOptions"],
) -> "_RequestContextManager": ...
Fixed Show fixed Hide fixed

@overload
def post(
self,
url: StrOrURL,
**kwargs: Unpack["_RequestOptions"],
) -> "_RequestContextManager": ...
Fixed Show fixed Hide fixed

@overload
def delete(
self,
url: StrOrURL,
**kwargs: Unpack["_RequestOptions"],
) -> "_RequestContextManager": ...
Fixed Show fixed Hide fixed

@overload
def head(
self,
url: StrOrURL,
**kwargs: Unpack["_RequestOptions"],
) -> "_RequestContextManager": ...
Fixed Show fixed Hide fixed

__slots__ = (
"_base_url",
"_source_traceback",
Expand Down
Loading