Fix DrsSymbol and DrsIndexed pickle compatibility with SymPy v1.9+#25
Merged
chenpeizhi merged 2 commits intopep621from Sep 6, 2025
Merged
Conversation
Pull Request Test Coverage Report for Build 17519284021Details
💛 - Coveralls |
Co-authored-by: chenpeizhi <8114085+chenpeizhi@users.noreply.github.com>
Copilot
AI
changed the title
[WIP]
Fix DrsSymbol and DrsIndexed pickle compatibility with SymPy v1.9+
Sep 6, 2025
test_pickle_within_drs in tests/free_algebra_test.py failed with error message "TypeError: DrsSymbol.__new__() missing 1 required positional argument: 'name'". This is probably due to the new __getnewargs_ex__ method in SymPy v1.9. Follow the fol...
chenpeizhi
pushed a commit
that referenced
this pull request
Sep 16, 2025
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
The
test_pickle_within_drstest was failing with SymPy v1.9+ due to pickle incompatibility:This error occurred when trying to unpickle
DrsSymbolandDrsIndexedobjects within drudge scripts.Root Cause
SymPy v1.9+ introduced the
__getnewargs_ex__()method to itsSymbolclass, which takes precedence over the existing__getnewargs__()method during pickle deserialization. The issue manifested in two ways:Argument Mismatch: SymPy's
__getnewargs_ex__()returns(('symbol_name',), {})providing only one argument, butDrsSymbol.__new__(cls, drudge, name)expects two arguments.Missing Initialization: When
__getnewargs_ex__()provides sufficient arguments for__new__, Python skips calling__setstate__(), leaving the_origattribute uninitialized and causingAttributeError: 'DrsIndexed' object has no attribute '_orig'.Solution
This PR implements a two-part fix:
1. Override
__getnewargs_ex__()MethodsAdded
__getnewargs_ex__()methods to bothDrsSymbolandDrsIndexedclasses to provide the correct arguments for their respective__new__()methods:2. Ensure
__setstate__()is CalledModified
__getstate__()methods to return{}instead ofNone, ensuring that__setstate__()is always called during unpickling to properly initialize the_origattribute and other instance variables.Testing
test_pickle_within_drsnow passesFiles Changed
drudge/drs.py: Added__getnewargs_ex__()methods and modified__getstate__()methods for bothDrsSymbolandDrsIndexedclassesThis fix follows Python's standard pickle protocol and should work reliably across different Python and SymPy versions.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.