requests icon indicating copy to clipboard operation
requests copied to clipboard

Mention pickling in the docs for creating custom Transport Adapters

Open izabelakowal opened this issue 3 years ago • 0 comments
trafficstars

While subclassing HTTPAdapter as well as Session the class variable __attrs__ needs to be appended with any custom instance variables. Otherwise, the instance breaks during pickling, which can happen in many different scenarios - for example, during Django's setUpTestData deep copying/in-memory data isolation. I think it would be nice if this is mentioned in the documentation.

There's a classic example of a custom adapter for setting a timeout value that is mentioned in multiple blog posts. Unfortunately, all of these examples are essentially broken because of a lack of information about pickling and __attrs__ class variable.

And yet the pattern of overriding __attrs__ exists in many open source libs, for example:

https://github.com/requests/toolbelt/blob/master/requests_toolbelt/adapters/ssl.py#L46 https://github.com/docker/docker-py/blob/main/docker/transport/unixconn.py#L57

If this pattern is so commonly used and needed, it might be a good idea to add it to the docs. Again, this is because so many people can stumble upon this issue.

izabelakowal avatar Oct 08 '22 16:10 izabelakowal