Skip to content

Refactor ClientRequest #11012

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

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open

Refactor ClientRequest #11012

wants to merge 17 commits into from

Conversation

Dreamsorcerer
Copy link
Member

@Dreamsorcerer Dreamsorcerer commented May 25, 2025

Goals

  • Separate logic between the proxy version and the main version, which have different requirements. This reduces need for asserts etc. where we know an attribute will not be None in one case, but not the other. The proxy code now uses the internal ClientRequestBase, while ClientRequest is for the main request and will be exposed to middlewares.
  • Rename some attributes/methods to better reflect what middleware users should be accessing (and matching what we've actually documented).
  • Ensure body is always Payload (it was only not when None was passed previously), and strip out the redundant code that tries to send an empty body.
  • Make it easier to make additions to ClientRequest in future releases without breaking subclasses.

TODO

If we're happy with this generally, then I still need to fix up the tests, which will take a substantial amount of work.

@Dreamsorcerer Dreamsorcerer requested a review from asvetlov as a code owner May 25, 2025 13:05
@Dreamsorcerer Dreamsorcerer added the backport:skip Skip backport bot label May 25, 2025
@@ -28,8 +29,9 @@
from multidict import CIMultiDict, CIMultiDictProxy, MultiDict, MultiDictProxy
from yarl import URL

from . import hdrs, helpers, http, multipart, payload
from . import hdrs, multipart, payload

Check notice

Code scanning / CodeQL

Cyclic import Note

Import of module
aiohttp.multipart
begins an import cycle.

Copilot Autofix

AI 8 days ago

Copilot could not generate an autofix suggestion

Copilot could not generate an autofix suggestion for this alert. Try pushing a new commit or if the problem persists contact support.

body: Any = b""
auth = None
response = None
class ClientResponse(HeadersMixin):
Copy link
Member Author

Choose a reason for hiding this comment

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

There are 0 changes to ClientResponse, I've just moved it up so I can reference it in ClientRequest.

Copy link
Member

Choose a reason for hiding this comment

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

I was under the impression the design was to be able to GC ClientRequest but hold on to ClientResponse

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't that makes a difference? I've just added a reference to ClientResponse (the class, not an instance) as a class attribute in ClientRequest, so need it defined first.

Copy link

codecov bot commented May 25, 2025

❌ 217 Tests Failed:

Tests completed Failed Passed Skipped
3874 217 3657 40
View the top 3 failed test(s) by shortest run time
tests.test_client_request::test_connection_key_with_proxy
Stack Traces | 0.002s run time
#x1B[0m#x1B[94masync#x1B[39;49;00m #x1B[94mdef#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[92mtest_connection_key_with_proxy#x1B[39;49;00m() -> #x1B[94mNone#x1B[39;49;00m:#x1B[90m#x1B[39;49;00m
    #x1B[90m    #x1B[39;49;00m#x1B[33m"""Verify the proxy headers are included in the ConnectionKey when a proxy is used."""#x1B[39;49;00m#x1B[90m#x1B[39;49;00m
        proxy = URL(#x1B[33m"#x1B[39;49;00m#x1B[33mhttp://proxy.example.com#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m)#x1B[90m#x1B[39;49;00m
>       req = ClientRequest(#x1B[90m#x1B[39;49;00m
            #x1B[33m"#x1B[39;49;00m#x1B[33mGET#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m,#x1B[90m#x1B[39;49;00m
            URL(#x1B[33m"#x1B[39;49;00m#x1B[33mhttp://example.com#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m),#x1B[90m#x1B[39;49;00m
            proxy=proxy,#x1B[90m#x1B[39;49;00m
            proxy_headers={#x1B[33m"#x1B[39;49;00m#x1B[33mX-Proxy#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m: #x1B[33m"#x1B[39;49;00m#x1B[33mtrue#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m},#x1B[90m#x1B[39;49;00m
            loop=asyncio.get_running_loop(),#x1B[90m#x1B[39;49;00m
        )#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[31mE       TypeError: ClientRequest.__init__() missing 18 required keyword-only arguments: 'params', 'headers', 'skip_auto_headers', 'data', 'cookies', 'auth', 'version', 'compress', 'chunked', 'expect100', 'response_class', 'proxy_auth', 'timer', 'session', 'ssl', 'traces', 'trust_env', and 'server_hostname'#x1B[0m

proxy      = URL('http://proxy.example.com')

#x1B[1m#x1B[31mtests/test_client_request.py#x1B[0m:1524: TypeError
tests.test_client_request::test_get_with_data[pyloop]
Stack Traces | 0.002s run time
loop = <_UnixSelectorEventLoop running=False closed=False debug=False>

    #x1B[0m#x1B[94masync#x1B[39;49;00m #x1B[94mdef#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[92mtest_get_with_data#x1B[39;49;00m(loop: asyncio.AbstractEventLoop) -> #x1B[94mNone#x1B[39;49;00m:#x1B[90m#x1B[39;49;00m
        #x1B[94mfor#x1B[39;49;00m meth #x1B[95min#x1B[39;49;00m ClientRequest.GET_METHODS:#x1B[90m#x1B[39;49;00m
>           req = ClientRequest(#x1B[90m#x1B[39;49;00m
                meth, URL(#x1B[33m"#x1B[39;49;00m#x1B[33mhttp://python.org/#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m), data={#x1B[33m"#x1B[39;49;00m#x1B[33mlife#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m: #x1B[33m"#x1B[39;49;00m#x1B[33m42#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m}, loop=loop#x1B[90m#x1B[39;49;00m
            )#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[31mE           TypeError: ClientRequest.__init__() missing 19 required keyword-only arguments: 'params', 'headers', 'skip_auto_headers', 'cookies', 'auth', 'version', 'compress', 'chunked', 'expect100', 'response_class', 'proxy', 'proxy_auth', 'timer', 'session', 'ssl', 'proxy_headers', 'traces', 'trust_env', and 'server_hostname'#x1B[0m

loop       = <_UnixSelectorEventLoop running=False closed=False debug=False>
meth       = 'OPTIONS'

#x1B[1m#x1B[31mtests/test_client_request.py#x1B[0m:810: TypeError
tests.test_client_request::test_connection_key_without_proxy
Stack Traces | 0.003s run time
#x1B[0m#x1B[94masync#x1B[39;49;00m #x1B[94mdef#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[92mtest_connection_key_without_proxy#x1B[39;49;00m() -> #x1B[94mNone#x1B[39;49;00m:#x1B[90m#x1B[39;49;00m
    #x1B[90m    #x1B[39;49;00m#x1B[33m"""Verify the proxy headers are not included in the ConnectionKey when a proxy is used."""#x1B[39;49;00m#x1B[90m#x1B[39;49;00m
        #x1B[90m# If proxy is unspecified, proxy_headers should be ignored#x1B[39;49;00m#x1B[90m#x1B[39;49;00m
>       req = ClientRequest(#x1B[90m#x1B[39;49;00m
            #x1B[33m"#x1B[39;49;00m#x1B[33mGET#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m,#x1B[90m#x1B[39;49;00m
            URL(#x1B[33m"#x1B[39;49;00m#x1B[33mhttp://example.com#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m),#x1B[90m#x1B[39;49;00m
            proxy_headers={#x1B[33m"#x1B[39;49;00m#x1B[33mX-Proxy#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m: #x1B[33m"#x1B[39;49;00m#x1B[33mtrue#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m},#x1B[90m#x1B[39;49;00m
            loop=asyncio.get_running_loop(),#x1B[90m#x1B[39;49;00m
        )#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[31mE       TypeError: ClientRequest.__init__() missing 19 required keyword-only arguments: 'params', 'headers', 'skip_auto_headers', 'data', 'cookies', 'auth', 'version', 'compress', 'chunked', 'expect100', 'response_class', 'proxy', 'proxy_auth', 'timer', 'session', 'ssl', 'traces', 'trust_env', and 'server_hostname'#x1B[0m


#x1B[1m#x1B[31mtests/test_client_request.py#x1B[0m:1538: TypeError

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

@@ -190,623 +193,352 @@ class ConnectionKey(NamedTuple):
proxy_headers_hash: Optional[int] # hash(CIMultiDict)


class ClientRequest:
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can back-port a limited version of this to keep back-ports more manageable. Right now automatic back-porting fails far too often, and we aren't any closer to releasing 4.x so we are going to have to deal with that for a long time which makes the risk of back-port errors higher.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe. Will have to move backportable parts to another PR and merge, then update this one.

Copy link

codspeed-hq bot commented May 25, 2025

CodSpeed Performance Report

Merging #11012 will degrade performances by 9.34%

Comparing clientrequest-refactor (0cdf184) with master (4824648)

Summary

❌ 1 regressions
✅ 58 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
test_send_client_request_one_hundred[pyloop] 2.5 ms 2.8 ms -9.34%

@bdraco
Copy link
Member

bdraco commented May 26, 2025

Sorry about the conflicts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip Skip backport bot
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants