Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make HttpStatusError and RequestError pickleable #3108

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

hoodmane
Copy link

If an error has keyword-only arguments, it needs this extra __reduce__ method to ensure that unpickling doesn't raise
TypeError: missing (n) required keyword-only argument(s).

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

@hoodmane hoodmane force-pushed the pickle-status-error branch 3 times, most recently from 0eef52c to 5f81a85 Compare February 22, 2024 07:01
If an error has keyword-only arguments, it needs this extra `__reduce__` method
to ensure that unpickling doesn't raise
`TypeError: missing (n) required keyword-only argument(s)`
Comment on lines +74 to +80
def __reduce__(
self,
) -> typing.Tuple[
typing.Callable[..., Exception], typing.Tuple[typing.Any], dict[str, typing.Any]
]:
return (Exception.__new__, (type(self),) + self.args, self.__dict__)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we able to be consistent with how pickle is supported on Request/Response here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the key word only arguments of Request have default values so that's why it's pickleable. Our choices here are:

  1. Give all kwonly arguments defaults. Classes where all kwonly arguments have defaults mostly have the correct pickle behavior without any special support.
  2. Do something like what I have here.

As an aside, this is a bit of a Python wart, ideally simple classes with required kwonly constructor args should be pickleable out of the box. I wonder if there is any discussion on discuss.python.org about it...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay sure thing.
Thanks for the fix. ☺️

@tomchristie
Copy link
Member

Worth adding a CHANGELOG entry for this.

@hoodmane
Copy link
Author

Okay updated changelog.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants