Skip to content

Abstract the public-facing API away from ClientResponse#13152

Open
Moist-Cat wants to merge 7 commits into
aio-libs:masterfrom
Moist-Cat:response-refactor
Open

Abstract the public-facing API away from ClientResponse#13152
Moist-Cat wants to merge 7 commits into
aio-libs:masterfrom
Moist-Cat:response-refactor

Conversation

@Moist-Cat

@Moist-Cat Moist-Cat commented Jul 16, 2026

Copy link
Copy Markdown

It's important to notice that the __init__ method varies between implementations and there are some extra fields (e.g, the _in_context field) that are required by some methods used outside the class.
The base class is basically an interface that implements the public API. Fields can't be stored in the base class because they may or may not be available during __init__ so I'm thinking of removing the constructor altogether.

Related PR

#13039

Moist-Cat and others added 2 commits July 15, 2026 23:39
    It's important to notice that the __init__ method varies between
    implementations and there are some leftovers (e.g, the _in_context field).
    The base class is basically an interface.
Comment thread aiohttp/http_base.py Fixed
Comment thread aiohttp/http_base.py Fixed
Comment thread aiohttp/http_base.py Fixed
Comment thread aiohttp/http_base.py Fixed
Comment thread aiohttp/http_base.py Fixed
@Moist-Cat Moist-Cat changed the title Abstract user-facing public API for ClientResponse Abstract the public-facing API away from ClientResponse Jul 16, 2026
@codspeed-hq

codspeed-hq Bot commented Jul 16, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 83 untouched benchmarks
⏩ 83 skipped benchmarks1


Comparing Moist-Cat:response-refactor (423b102) with master (cba121d)2

Open in CodSpeed

Footnotes

  1. 83 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

  2. No successful run was found on master (6ec1d11) during the generation of this report, so cba121d was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@Moist-Cat
Moist-Cat marked this pull request as ready for review July 16, 2026 04:46
@Moist-Cat
Moist-Cat requested a review from asvetlov as a code owner July 16, 2026 04:46
@codecov

codecov Bot commented Jul 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.08911% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 98.95%. Comparing base (cba121d) to head (423b102).
⚠️ Report is 2 commits behind head on master.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
aiohttp/http_base.py 90.62% 9 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #13152      +/-   ##
==========================================
- Coverage   98.97%   98.95%   -0.02%     
==========================================
  Files         131      132       +1     
  Lines       48824    48857      +33     
  Branches     2546     2545       -1     
==========================================
+ Hits        48324    48348      +24     
- Misses        376      385       +9     
  Partials      124      124              
Flag Coverage Δ
Autobahn 22.14% <41.48%> (+0.02%) ⬆️
CI-GHA 98.88% <91.08%> (-0.02%) ⬇️
OS-Linux 98.65% <91.08%> (-0.02%) ⬇️
OS-Windows 97.01% <91.08%> (-0.02%) ⬇️
OS-macOS 97.91% <91.08%> (-0.03%) ⬇️
Py-3.10 98.10% <91.08%> (-0.02%) ⬇️
Py-3.11 98.36% <91.08%> (-0.02%) ⬇️
Py-3.12 98.45% <91.08%> (-0.02%) ⬇️
Py-3.13 98.42% <91.08%> (-0.03%) ⬇️
Py-3.14 98.44% <90.42%> (-0.03%) ⬇️
Py-3.14t 97.54% <90.42%> (-0.02%) ⬇️
Py-pypy-3.11 97.42% <91.08%> (-0.02%) ⬇️
VM-macos 97.91% <91.08%> (-0.03%) ⬇️
VM-ubuntu 98.65% <91.08%> (-0.02%) ⬇️
VM-windows 97.01% <91.08%> (-0.02%) ⬇️
cython-coverage 37.91% <79.20%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

Comment thread aiohttp/http_base.py
import contextlib
import json
from http.cookies import SimpleCookie
from typing import Any, Callable, Optional, Tuple

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Callable, Optional, and Tuple are all deprecated.

Comment thread aiohttp/http_base.py
Comment on lines +25 to +27
self._in_context = False
self._released: bool = False
self._resolve_charset: Callable[[Any, bytes], str] = lambda *_: "utf-8"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The subclass doesn't call this. These could just be class defaults anyway, so don't need the init to define them.

Comment thread aiohttp/http_base.py
self._resolve_charset: Callable[[Any, bytes], str] = lambda *_: "utf-8"

# ----------------------------------------------------------------
# Abstract / overridable protocol methods

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We probably want to ensure these are defined, right? In which case this should probably be an abstract class and these methods can all be defined as abstract.

Comment thread aiohttp/http_base.py
Comment on lines +50 to +54
def headers(self) -> Any:
return self._headers

@property
def history(self) -> Tuple[Any, ...]:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seem to have lost typing information that we had before?

Comment thread aiohttp/client_reqrep.py


class ClientResponse(HeadersMixin):
class ClientResponse(BaseResponse):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So this is the HTTP/1 class, right?

I still think, atleast in master/v4, that we probably want to rename this and have the base class be called ClientResponse. That means that middlewares etc. which reference ClientResponse shouldn't need any changes to work in future with the HTTP/2 class.

With these current changes, we'd need to changes lots of references from ClientResponse -> BaseResponse, both in our code and in user's code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants