Repository navigation
Usage of HTTPResponse.url #86228
Description
Activity
Hello all,
While testing some static analysis tools on HTTP/client.py, Pylint pointed
me to HTTPResponse.geturl() method with a "no-member" error for theurl
attribute. I tried invoking thegeturlmethod and reading the
HTTPResponse.urlattribute using a sample code from the official docs:import http.client conn = http.client.HTTPSConnection("www.python.org") conn.request("GET", "/") r1 = conn.getresponse() print(r1.status, r1.reason) r1.geturl() r1.urlimport http.client conn = http.client.HTTPSConnection("www.python.org") conn.request("GET", "/") r1 = conn.getresponse() data1 = r1.read() conn.request("GET", "/") r1 = conn.getresponse() while chunk := r1.read(200): print(repr(chunk)) r1.geturl() r1.urlBoth of those examples will raise an
AttributeError: 'HTTPResponse' object has no attribute 'url'.I tried searching through this module's history from when this line originally appeared,
6c5e28c but
I wasn't able to find this attribute being set internally by the class, even
though there is anurlattribute at init.So, I wonder if this attribute was intended to be set externally as in
r1.url = 'something'
or if it is just a bug- addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Oct 17, 2020 There is a comment in the HTTPResponse class regarding these methods:
# For compatibility with old-style urllib responses.
They were there for the "urlopen" API in "urllib.request", not for the "http.client" module on its own. I expect the "url" attribute is set by the "urlopen" code.
However more recently (bpo-12707) the "url" attribute and "geturl" method were documented in the HTTPResponse documentation, which is awkward.
This seems to still be broken in 3.12:
$ docker run --rm python:3.12 python -c \ 'import http.client as httplib; conn = httplib.HTTPConnection("google.com"); conn.request("GET", "/"); print(conn.getresponse().geturl())' Traceback (most recent call last): File "<string>", line 1, in <module> File "/usr/local/lib/python3.12/http/client.py", line 790, in geturl return self.url ^^^^^^^^ AttributeError: 'HTTPResponse' object has no attribute 'url'
HTTPResponse.urldoesn't exist, andHTTPResponse.geturl()is broken. I tried 3.12, 3.11, 3.10, 3.9, and 3.8 (prior to #11447), it doesn't look like this has ever worked.FYI @merwok and @matrixise who approved #11447.
Tangent: I now wish the original PR had also updated the docstrings on the deprecated methods (it only added a code comment).
Reacted by Michael DiamondAnother problem with this documentation is it suggests HTTPResponse.headers is an EmailMessage instance, while HTTPResponse.msg remains only a (subclassed) instance of email.message.Message. And addinfourl.info and addinfourl.headers are now supposed to return an EmailMessage, although .info was previously (indirectly) documented to return an email.message.Message.
I tested Python 3.10.2 and it seems HTTPResponse.headers has not changed because the object didn’t have one of the methods unique to EmailMessage:
>>> u = urlopen('http://google.com') >>> u.headers.is_attachment Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'HTTPMessage' object has no attribute 'is_attachment'
Reacted by Michael Diamond- added a commit that references this issue
on Jan 24, 2025 Still present on Python 3.13.3 in 26 may 2025
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsTodo
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: