Python requests hook returns a value to cause exception

The Documentation for python requests module says for hooks that “If the callback function returns a value, it is assumed that it is to replace the data that was passed in. If the function doesn’t return anything, nothing else is effected”

Now i am trying to return a value(int in my case) from my hook function & it throws an exception. This will be valid in all the cases when the return value is an object that DOESNOT have the raw() method defined for it.

Here is some code

And here is the exception:

The code in sessions.py @line 446 is trying to extract cookies after the dispatch_hook..From source

Either the documentation needs to alter or the handling needs to be re-worked. What is the best way to handle this ?

[update]

Based on the comments I tried to return the base response object. Turns out it cannot be used in that manner moreover since some of its fields are initialized to None.

Newer code:

Exception thrown now:

What seems is that i will have to implement a full pseudo response?

If the callback function returns a value, it is assumed that it is to replace the data that was passed in. If the function doesn’t return anything, nothing else is effected.

This means that whatever you return is expected to take the place of the response object you were passed.

Nothing in the documentation states that you can return just anything. What did you expect to happen instead?

If you wanted to return a response that has different data, return something that acts like a response still. This means that either you need to subclass the requests response object, or implement something that provides the same API:

You may want to copy over some of the other attributes from the original response; check the documentation on what attributes Response objects have.