Skip to content

Commit 76f7722

Browse files
authored
[PR #9692/e85db24 backport][3.11] Change RequestInfo to be a NamedTuple to improve performances (#9708)
1 parent 0babc34 commit 76f7722

File tree

5 files changed

+16
-16
lines changed

5 files changed

+16
-16
lines changed

CHANGES/9692.breaking.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Changed ``ClientRequest.request_info`` to be a `NamedTuple` to improve client performance -- by :user:`bdraco`.

aiohttp/client_reqrep.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,11 @@ class ContentDisposition:
105105
filename: Optional[str]
106106

107107

108-
@attr.s(auto_attribs=True, frozen=True, slots=True)
109-
class RequestInfo:
108+
class RequestInfo(NamedTuple):
110109
url: URL
111110
method: str
112111
headers: "CIMultiDictProxy[str]"
113-
real_url: URL = attr.ib()
114-
115-
@real_url.default
116-
def real_url_default(self) -> URL:
117-
return self.url
112+
real_url: URL
118113

119114

120115
class Fingerprint:
@@ -401,7 +396,11 @@ def port(self) -> Optional[int]:
401396
@property
402397
def request_info(self) -> RequestInfo:
403398
headers: CIMultiDictProxy[str] = CIMultiDictProxy(self.headers)
404-
return RequestInfo(self.url, self.method, headers, self.original_url)
399+
# These are created on every request, so we use a NamedTuple
400+
# for performance reasons.
401+
return tuple.__new__(
402+
RequestInfo, (self.url, self.method, headers, self.original_url)
403+
)
405404

406405
def update_host(self, url: URL) -> None:
407406
"""Update destination host, port and connection type (ssl)."""

docs/client_reference.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,7 @@ Response object
14831483

14841484
.. attribute:: request_info
14851485

1486-
A namedtuple with request URL and headers from :class:`~aiohttp.ClientRequest`
1486+
A :class:`typing.NamedTuple` with request URL and headers from :class:`~aiohttp.ClientRequest`
14871487
object, :class:`aiohttp.RequestInfo` instance.
14881488

14891489
.. method:: get_encoding()
@@ -1842,7 +1842,7 @@ Utilities
18421842

18431843
.. class:: RequestInfo()
18441844

1845-
A data class with request URL and headers from :class:`~aiohttp.ClientRequest`
1845+
A :class:`typing.NamedTuple` with request URL and headers from :class:`~aiohttp.ClientRequest`
18461846
object, available as :attr:`ClientResponse.request_info` attribute.
18471847

18481848
.. attribute:: url

tests/test_client_request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def test_version_default(make_request) -> None:
121121
def test_request_info(make_request) -> None:
122122
req = make_request("get", "http://python.org/")
123123
assert req.request_info == aiohttp.RequestInfo(
124-
URL("http://python.org/"), "GET", req.headers
124+
URL("http://python.org/"), "GET", req.headers, URL("http://python.org/")
125125
)
126126

127127

tests/test_client_response.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ def test_response_request_info() -> None:
987987
response = ClientResponse(
988988
"get",
989989
URL(url),
990-
request_info=RequestInfo(url, "get", headers),
990+
request_info=RequestInfo(url, "get", headers, url),
991991
writer=WriterMock(),
992992
continue100=None,
993993
timer=TimerNoop(),
@@ -1006,7 +1006,7 @@ def test_request_info_in_exception() -> None:
10061006
response = ClientResponse(
10071007
"get",
10081008
URL(url),
1009-
request_info=RequestInfo(url, "get", headers),
1009+
request_info=RequestInfo(url, "get", headers, url),
10101010
writer=WriterMock(),
10111011
continue100=None,
10121012
timer=TimerNoop(),
@@ -1027,7 +1027,7 @@ def test_no_redirect_history_in_exception() -> None:
10271027
response = ClientResponse(
10281028
"get",
10291029
URL(url),
1030-
request_info=RequestInfo(url, "get", headers),
1030+
request_info=RequestInfo(url, "get", headers, url),
10311031
writer=WriterMock(),
10321032
continue100=None,
10331033
timer=TimerNoop(),
@@ -1050,7 +1050,7 @@ def test_redirect_history_in_exception() -> None:
10501050
response = ClientResponse(
10511051
"get",
10521052
URL(url),
1053-
request_info=RequestInfo(url, "get", headers),
1053+
request_info=RequestInfo(url, "get", headers, url),
10541054
writer=WriterMock(),
10551055
continue100=None,
10561056
timer=TimerNoop(),
@@ -1064,7 +1064,7 @@ def test_redirect_history_in_exception() -> None:
10641064
hist_response = ClientResponse(
10651065
"get",
10661066
URL(hist_url),
1067-
request_info=RequestInfo(url, "get", headers),
1067+
request_info=RequestInfo(url, "get", headers, url),
10681068
writer=WriterMock(),
10691069
continue100=None,
10701070
timer=TimerNoop(),

0 commit comments

Comments
 (0)