ClientResponse.raise_for_status() raises an AssertionError for empty reason #3532
Closed
Description
Long story short
ClientResponse.raise_for_status() raises an AssertionError for a >= 400 status code if its reason is an empty string.
Expected behaviour
I expect ClientResponse.raise_for_status() to raise a ClientResponseError if it’s invoked on a ClientResponse with a status >= 400, and to either:
- raise a
ClientError(or some subclass ofClientError) if it’s called on aClientResponsewith areason = None, regardless of the HTTP status code, if this can only happen whenraise_for_status()has been called beforestart()has been called; or - convert a
ClientResponse.reasonofNoneinto""ifNoneis a valid value forClientResponse.reason
Actual behaviour
ClientResponse.raise_for_status()raises an AssertionError if it’s invoked on aClientResponsewhen a status >= 400 andbool(reason)isFalse, which includesreason == ""ClientResponse.raise_for_status()raises a ClientResponseError if it’s invoked on aClientResponsewith a status >= 400 if thereasonis not falseyClientResponse.raise_for_status()raises no error if it’s invoked on aClientResponsewith a status < 400 and areason = None
Steps to reproduce
A colleague and I uncovered this using aioresponses, which currently doesn’t set a reason on the ClientResponse at all – see pnuckowski/aioresponses#114.
You can reproduce this with the example client and server in this gist: https://gist.github.com/rhwlo/5ff3cb6bae8deb6abb4a1e527ad9583e
Your environment
I’m using the latest aiohttp from the repository, but this problem shows up only in v3.5.0 and newer.