Improve the format of http.Request.__repr__ to include eg class and object identity
|Reported by:||Richard Wall||Owned by:||Jean-Paul Calderone|
branch-diff, diff-cov, branch-cov, buildbot
In #6120 radix commented that Request.repr should include the ClassName and memory address.
19:24 < radix> rwall: hmm 19:25 < radix> rwall: can you please include the class name and instance ID in that repr as well 19:25 < radix> I really hate it when classes __repr__ don't include the basic info 19:26 < radix> if you want me to put a comment on the ticket I can do that 19:26 < radix> oh, you're reviewing, not authoring. 19:26 < rwall> radix: Yes. That sounds like a good idea. Is it ok to change the repr without warning? 19:27 < radix> I see. and it's not even changing the contents 19:27 < rwall> Yep, just adding a missing test. 19:27 < radix> rwall: yes, as tomprince said in an earlier comment on the ticket, we don't want users relying on the content of the __repr__ 19:27 < radix> but I see the content isn't even being changed in this branch, so it's probably not the appropriate branch to change it in. 19:29 < rwall> radix: Ok, but I'll raise another ticket. What's the ideal repr? What should be the standard? 19:30 < rwall> radix: and without wanting to dwell too long on such a small ticket...do you think __repr__ should have full docstring? 19:32 < radix> huh, standards, that's an idea. we don't have any 19:32 < radix> rwall: I don't think __repr__'s docstring needs to document the format absolutely. but I don't really think it matters either way. 19:33 < rwall> ok 19:34 < radix> Personally, off the cuff, I would suggest something like <ClassName at 0xffffffff ...> or something, where "..." could be "GET /" or maybe even "method=GET path=/" etc
Here's an interesting discussion about ideal repr output.
If at all possible, this (repr) should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment).
If this is not possible, a string of the form <...some useful description...> should be returned.
The return value must be a string object. If a class defines repr() but not str(), then repr() is also used when an “informal” string representation of instances of that class is required.
This is typically used for debugging, so it is important that the representation is information-rich and unambiguous
The current repr outputs method='GET' etc but in #6120, exarkun commented that he'd rather just see method=GET. I think his reasoning was that ultimately, the method and clientproto should be supplied and stored as twisted.python.constants instead of byte strings. And similarly, the URI should also probably be supplied and stored as a URI object of some sort. This would enforce the use of byte strings for these attribute values. (see related ticket:6168).
So here's a proposed format...
<twisted.web.http.Request at 0xffffffff method=GET uri=http://www.example.com/foo/bar clientproto=HTTP/1.1>