Fix pickling of exceptions with kw_only attributes#1510
Open
Fridayai700 wants to merge 1 commit intopython-attrs:mainfrom
Open
Fix pickling of exceptions with kw_only attributes#1510Fridayai700 wants to merge 1 commit intopython-attrs:mainfrom
kw_only attributes#1510Fridayai700 wants to merge 1 commit intopython-attrs:mainfrom
Conversation
BaseException.__reduce__ passes all attribute values as positional args, which fails when some attrs are keyword-only. Add a custom __reduce__ to exception classes that passes all init arguments as keyword arguments via a helper function. Fixes python-attrs#734 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Summary
Fixes the
TypeErrorthat occurs when pickling attrs exception classes that usekw_only=True.BaseException.__reduce__passes all attribute values as positional args, which fails when some attrs are keyword-only.The fix adds a custom
__reduce__to all attrs exception classes (auto_exc=True) that uses a helper function_reconstruct_excto pass all init arguments as keyword arguments. This handles:kw_only=Trueon the class)Fixes #734
Test plan
test_auto_exc_kw_only_pickle: verifies pickle roundtrip for all-kw_only exceptionstest_auto_exc_mixed_kw_only_pickle: verifies pickle roundtrip for mixed positional + kw_only exceptions