Open
Description
Describe the bug
Perhaps not strictly a bug, but a counterintuitive behavior that doesn't match how other parts are handled (like headers).
If I define a ClientSession
with middlewares
and use request()
with another set of middlewares
, I expect the request-level middleware list to extend the session-level list, similar to how request headers are merged with session headers. Instead, request middleware replaces session middleware completely.
To Reproduce
import asyncio
import aiohttp
def middleware_session(
req: aiohttp.ClientRequest, handle: aiohttp.ClientHandlerType
) -> aiohttp.ClientResponse:
print("session middleware")
print(req.headers)
return handle(req)
def middleware_request(
req: aiohttp.ClientRequest, handle: aiohttp.ClientHandlerType
) -> aiohttp.ClientResponse:
print("request middleware")
print(req.headers)
return handle(req)
async def main_():
session = aiohttp.ClientSession(
middlewares=[middleware_session], headers={"User-Agent": "test"}
)
await session.get(
"http://google.com",
middlewares=[middleware_request],
headers={"X-My-Header": "blah"},
)
if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main_())
Expected behavior
In logs:
request middleware
session middleware
<CIMultiDict('Host': 'google.com', 'User-Agent': 'test', 'X-My-Header': 'blah', 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate, br')>
Logs/tracebacks
request middleware
<CIMultiDict('Host': 'google.com', 'User-Agent': 'test', 'X-My-Header': 'blah', 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate, br')>
Python Version
Python 3.13.3
aiohttp Version
3.12.11
multidict Version
6.4.4
propcache Version
0.3.1
yarl Version
1.20.0
OS
Mac OS 15.5
Related component
Client
Additional context
No response
Code of Conduct
- I agree to follow the aio-libs Code of Conduct