Make OAuth2Session picklable (oauthlib #849) - #570
Open
apoorvdarshan wants to merge 1 commit into
Open
Conversation
OAuth2Session.__init__ set the no-op auth handler as a lambda (`self.auth = lambda r: r`), which cannot be pickled, so a session could not be used in a multiprocessing context (`Can't pickle local object ...<lambda>`). Move the no-op handler to a module-level function (_no_op_auth) so instances are picklable. Behavior is unchanged: it still returns the request unchanged, disabling requests' automatic auth. Adds a test.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes oauthlib/oauthlib#849.
Problem
OAuth2Sessioncannot be pickled, so it can't be sent across processes (e.g.multiprocessing,ProcessPoolExecutor):The cause is a lambda assigned in
__init__:Lambdas defined inside a method aren't picklable.
Fix
Move the no-op auth handler to a module-level function so instances remain picklable. Behavior is identical — it still returns the request unchanged, which is what disables requests' automatic auth (e.g. from
.netrc, per #278).The maintainer asked for a PR with a test in oauthlib/oauthlib#849.
Testing
OAuth2SessionTest.test_pickle: every client type pickles/unpickles, and the restored session'sauthstill returns the request unchanged. It raisesPicklingErroronmainand passes with this change.36 passed). Two unrelated items in this environment are pre-existing and not touched by this change:tests/test_compliance_fixes.pyfails to collect without the optionalrequests_mockdependency, andtests/test_core.py::OAuth1Test::testCanPostBinaryDatafails onmainas well (OAuth1, binary body).ruffis clean on the changed files. Added aHISTORY.rstentry.Disclosure: this change was prepared with the assistance of an AI tool (Claude Code). I reproduced the issue, implemented and verified the fix and test, ran the suite and linter, and take responsibility for the contribution and will respond to review feedback personally.