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

Make creating RequestInfo backwards compatible with 3.10 #9873

Merged
merged 6 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/9873.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added a backward compatibility layer to `~aiohttp.RequestInfo` to allow creating these objects without a `real_url` -- by :user:`bdraco`.
25 changes: 23 additions & 2 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from .formdata import FormData
from .hdrs import CONTENT_TYPE
from .helpers import (
_SENTINEL,
BaseTimerContext,
BasicAuth,
HeadersMixin,
Expand Down Expand Up @@ -104,13 +105,31 @@ class ContentDisposition:
filename: Optional[str]


class RequestInfo(NamedTuple):
class _RequestInfo(NamedTuple):
url: URL
method: str
headers: "CIMultiDictProxy[str]"
real_url: URL


class RequestInfo(_RequestInfo):

def __new__(
cls,
url: URL,
method: str,
headers: "CIMultiDictProxy[str]",
real_url: URL = _SENTINEL, # type: ignore[assignment]
) -> "RequestInfo":
"""Create a new RequestInfo instance.

For backwards compatibility, the real_url parameter is optional.
"""
return tuple.__new__(
cls, (url, method, headers, url if real_url is _SENTINEL else real_url)
)


class Fingerprint:
HASHFUNC_BY_DIGESTLEN = {
16: md5,
Expand Down Expand Up @@ -326,7 +345,9 @@ def port(self) -> Optional[int]:
def request_info(self) -> RequestInfo:
headers: CIMultiDictProxy[str] = CIMultiDictProxy(self.headers)
# These are created on every request, so we use a NamedTuple
# for performance reasons.
# for performance reasons. We don't use the RequestInfo.__new__
# method because it has a different signature which is provided
# for backwards compatibility only.
return tuple.__new__(
RequestInfo, (self.url, self.method, headers, self.original_url)
)
Expand Down
43 changes: 43 additions & 0 deletions tests/test_client_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -1544,3 +1544,46 @@ async def test_connection_key_without_proxy() -> None:
)
assert req.connection_key.proxy_headers_hash is None
await req.close()


def test_request_info_back_compat() -> None:
"""Test RequestInfo can be created without real_url."""
url = URL("http://example.com")
other_url = URL("http://example.org")
assert (
aiohttp.RequestInfo(
url=url, method="GET", headers=CIMultiDictProxy(CIMultiDict())
).real_url
is url
)
assert (
aiohttp.RequestInfo(url, "GET", CIMultiDictProxy(CIMultiDict())).real_url is url
)
assert (
aiohttp.RequestInfo(
url, "GET", CIMultiDictProxy(CIMultiDict()), real_url=url
).real_url
is url
)
assert (
aiohttp.RequestInfo(
url, "GET", CIMultiDictProxy(CIMultiDict()), real_url=other_url
).real_url
is other_url
)


def test_request_info_tuple_new() -> None:
"""Test RequestInfo must be created with real_url using tuple.__new__."""
url = URL("http://example.com")
with pytest.raises(IndexError):
tuple.__new__(
aiohttp.RequestInfo, (url, "GET", CIMultiDictProxy(CIMultiDict()))
).real_url

assert (
tuple.__new__(
aiohttp.RequestInfo, (url, "GET", CIMultiDictProxy(CIMultiDict()), url)
).real_url
is url
)
Loading