|
code, msg, hdrs = response.code, response.msg, response.info() |
Per the docs for urllib.response.addinfourl and http.client.HTTPResponse, response.code and response.info() are deprecated as of Python 3.9. This is an issue when writing handlers that generate their own response objects—these objects must continue to provide the deprecated members.
I admit I don’t know how common or advisable this use case is, but it happened to me while I was implementing HTTP caching. When serving a response from the cache, I didn’t have a “real” response object to modify, and I wasn’t confident that I could instantiate the right thing from the standard library (the docs call it “a http.client.HTTPResponse object slightly modified”).
Instead, I made my own class that implemented just enough of the interface to get by. I thought that headers, status, url, and I/O read operations would suffice… but then I realised that HTTPErrorProcessor runs after all other handlers (including mine). So I would also need to add the deprecated members in order to be compatible.
See also #86203, where (if I’m reading correctly) it’s request objects that need to continue to provide deprecated members in order to work with the standard library.
Linked PRs
cpython/Lib/urllib/request.py
Line 625 in 0181aa2
Per the docs for
urllib.response.addinfourlandhttp.client.HTTPResponse,response.codeandresponse.info()are deprecated as of Python 3.9. This is an issue when writing handlers that generate their own response objects—these objects must continue to provide the deprecated members.I admit I don’t know how common or advisable this use case is, but it happened to me while I was implementing HTTP caching. When serving a response from the cache, I didn’t have a “real” response object to modify, and I wasn’t confident that I could instantiate the right thing from the standard library (the docs call it “a
http.client.HTTPResponseobject slightly modified”).Instead, I made my own class that implemented just enough of the interface to get by. I thought that
headers,status,url, and I/O read operations would suffice… but then I realised thatHTTPErrorProcessorruns after all other handlers (including mine). So I would also need to add the deprecated members in order to be compatible.See also #86203, where (if I’m reading correctly) it’s request objects that need to continue to provide deprecated members in order to work with the standard library.
Linked PRs
urllib/request#123509urllib/request#123541addinfourlandHTTPResponsedeprecated attributes with stable ones #132644