X Tutup
Skip to content

gh-145651: Add http.client.{HTTPConnection,HTTPResponse}.__repr__#145653

Open
JackDanger wants to merge 5 commits intopython:mainfrom
JackDanger:http-client-repr
Open

gh-145651: Add http.client.{HTTPConnection,HTTPResponse}.__repr__#145653
JackDanger wants to merge 5 commits intopython:mainfrom
JackDanger:http-client-repr

Conversation

@JackDanger
Copy link

@JackDanger JackDanger commented Mar 9, 2026

Add informative __repr__ implementations to http.client.HTTPConnection and http.client.HTTPResponse.

  • HTTPConnection shows host and port: <HTTPConnection example.com:80>
  • HTTPResponse shows status and reason: <HTTPResponse [200 OK]>
  • HTTPSConnection inherits the repr and shows port 443
  • HTTPResponse shows just <HTTPResponse> before headers are read

Closes #145651

@python-cla-bot
Copy link

python-cla-bot bot commented Mar 9, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

@picnixz picnixz changed the title gh-145651: Add __repr__ to http.client.HTTPConnection and HTTPResponse gh-145651: Add http.client.{HTTPConnection,HTTPResponse}.__repr__ Mar 9, 2026
picnixz
picnixz previously requested changes Mar 9, 2026
self._create_connection = socket.create_connection

def __repr__(self):
return '<%s %s:%s>' % (self.__class__.__name__, self.host, self.port if self.port is not None else self.default_port)
Copy link
Member

Choose a reason for hiding this comment

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

Please use an if block if self.port is None. You can also use f-strings here as it's new code.

Comment on lines +2377 to +2378
if not hasattr(client, 'HTTPSConnection'):
self.skipTest('ssl support required')
Copy link
Member

Choose a reason for hiding this comment

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

Use a decorator instead.

Copy link
Author

Choose a reason for hiding this comment

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

can do 🙇

@bedevere-app
Copy link

bedevere-app bot commented Mar 9, 2026

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@JackDanger
Copy link
Author

I have made the requested changes; please review again.

@bedevere-app
Copy link

bedevere-app bot commented Mar 9, 2026

Thanks for making the requested changes!

@picnixz: please review the changes made to this pull request.

@bedevere-app bedevere-app bot requested a review from picnixz March 9, 2026 01:03
def test_http_response_repr_before_read(self):
sock = FakeSocket(b'HTTP/1.1 200 OK\r\n\r\n')
resp = client.HTTPResponse(sock)
self.assertEqual(repr(resp), '<HTTPResponse>')
Copy link
Member

Choose a reason for hiding this comment

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

Would this representation really make sense? it looks a bit... invalid. Maybe <HTTPResponse (notset)> or something like this ("notset" is not the best word though; what does requests and httpx do for such cases?)

sock = FakeSocket(body)
resp = client.HTTPResponse(sock)
resp.begin()
self.assertEqual(repr(resp), '<HTTPResponse [200 OK]>')
Copy link
Member

Choose a reason for hiding this comment

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

Is the representation actually the same as for httpx/requests?

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

http.client.HTTPConnection and HTTPResponse are missing __repr__

2 participants

X Tutup