Fix pickling of exception classes with kw_only attributes (#734)#1534
Closed
r266-tech wants to merge 2 commits intopython-attrs:mainfrom
Closed
Fix pickling of exception classes with kw_only attributes (#734)#1534r266-tech wants to merge 2 commits intopython-attrs:mainfrom
r266-tech wants to merge 2 commits intopython-attrs:mainfrom
Conversation
BaseException.__reduce__ returns (cls, self.args, state), passing self.args as positional arguments to cls() on unpickle. When kw_only=True, __init__ only accepts keyword-only arguments, causing unpickling to fail with: TypeError: __init__() takes 1 positional argument but 2 were given This fix adds a custom __reduce__ for exception classes that have kw_only attrs. It uses a _rebuild_exc helper that creates the instance via __new__ (bypassing __init__) and restores attributes directly, then calls BaseException.__init__ to properly set self.args. Fixes GH#734
for more information, see https://pre-commit.ci
Member
|
AI slop |
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.
Problem
Exception classes decorated with
auto_exc=Trueandkw_only=Truecannot be pickled.BaseException.__reduce__returns(cls, self.args, state), passingself.argsas positional arguments tocls()on unpickle. Whenkw_only=True,__init__only accepts keyword-only arguments, causing:Reproducer:
Fix
Added a custom
__reduce__for exception classes that have anykw_onlyattrs. It uses a_rebuild_exchelper that:cls.__new__(cls)— bypassing__init__object.__setattr__BaseException.__init__to properly restoreself.argsThe fix is minimal (only applied when the class is an exception AND has kw_only attrs) and does not affect non-exception classes or exceptions without kw_only.
Tests
Added
test_auto_exc_kw_only_pickle(parametrized over slots, frozen, pickle protocol) andtest_auto_exc_mixed_kw_only_pickle(mixed positional + kw_only attrs).Closes #734