[docs] [issue13211] urllib2.HTTPError does not have 'reason' attribute.

Jason R. Coombs report at bugs.python.org
Tue Nov 8 22:39:55 CET 2011


Jason R. Coombs <jaraco at jaraco.com> added the comment:

That was my point. If HTTPError is a subclass of URLError, then an HTTPError _is an_ URLError, and thus should implement the same public interface.

The problem is better illustrated with this request:

try:
  urllib.request.urlopen('http://api.wordnik.com/v4/word.json/foo/examples')
except urllib2.URLError as exc:
  # We caught a URLError, what's the reason?
  print(exc.reason)

This code will fail with an attribute error, but only when the except clause catches an HTTPError (as it does in this case).

The documentation explicitly states that HTTPError is a subclass of URLError and it doesn't say anything about the addinfourl interface. The documentation strongly suggests (though implicitly) that HTTPError.reason will be present as it is with URLError.

If HTTPError does not implement the reason attribute, then there is no value in making it the subclass of URLError, and HTTPError should probably have the same parent class as URLError. However, this change is even more drastic and would almost certainly violate backward compatibility constraints.

The proposal I've made is generally backward compatible, and addresses the underlying bug (that URLError.__init__ is never called), and the symptom that a HTTPError does not implement the documented public interface.

Perhaps there's a better approach, but I believe based on the example code above, the implementation is broken.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue13211>
_______________________________________


More information about the docs mailing list