Skip to content

Commit

Permalink
Add Symbol serializability
Browse files Browse the repository at this point in the history
Symbol was not (un)picklable as Symbol.__getattr__ was getting in the way while looking for __setstate__.
  • Loading branch information
maurosilber committed Jan 18, 2024
1 parent 3b955a1 commit 1a1accc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
4 changes: 3 additions & 1 deletion symbolite/abstract/symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,11 @@ def __getitem__(self, key: Any) -> Self:
return getitem(self, key)

# Emulating attribute
def __getattr__(self, key: Any) -> Self:
def __getattr__(self, key: str) -> Self:
"""Defines behavior for when an item is accessed,
using the notation self.key"""
if key.startswith("__"):
raise AttributeError(key)
return symgetattr(self, key)

# Normal arithmetic operators
Expand Down
13 changes: 13 additions & 0 deletions symbolite/testsuite/test_serialize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pickle

from pytest import mark

from .. import Scalar, Symbol, Vector


@mark.parametrize("cls", [Scalar, Symbol, Vector])
def test_pickle(cls):
obj = cls()
dump = pickle.dumps(obj)
load = pickle.loads(dump)
assert load == obj

0 comments on commit 1a1accc

Please sign in to comment.