Skip to content

ClientResponse.raise_for_status() raises an AssertionError for empty reason #3532

Closed
@rhwlo

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 of ClientError) if it’s called on a ClientResponse with a reason = None, regardless of the HTTP status code, if this can only happen when raise_for_status() has been called before start() has been called; or
  • convert a ClientResponse.reason of None into "" if None is a valid value for ClientResponse.reason

Actual behaviour

  • ClientResponse.raise_for_status() raises an AssertionError if it’s invoked on a ClientResponse when a status >= 400 and bool(reason) is False, which includes reason == ""
  • ClientResponse.raise_for_status() raises a ClientResponseError if it’s invoked on a ClientResponse with a status >= 400 if the reason is not falsey
  • ClientResponse.raise_for_status() raises no error if it’s invoked on a ClientResponse with a status < 400 and a reason = 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.

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions