Skip to content

Commit

Permalink
Customize pickling of frozenlist
Browse files Browse the repository at this point in the history
Like frozendict, implement __reduce__ and __setstate__ to handle
pickling. Otherwise, __setstate__ will be called to restore the
frozenlist, causing a RuntimeError because frozenlist is not mutable.

Closes dagster-io#2719
  • Loading branch information
kinghuang committed Sep 9, 2020
1 parent d53f5b2 commit 0c2e877
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions python_modules/dagster/dagster/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ class frozenlist(list):
def __readonly__(self, *args, **kwargs):
raise RuntimeError("Cannot modify ReadOnlyList")

# https://docs.python.org/3/library/pickle.html#object.__reduce__
#
# Like frozendict, implement __reduce__ and __setstate__ to handle pickling.
# Otherwise, __setstate__ will be called to restore the frozenlist, causing
# a RuntimeError because frozenlist is not mutable.

def __reduce__(self):
return (frozenlist, (), list(self))

def __setstate__(self, state):
self.__init__(state)

__setitem__ = __readonly__
__delitem__ = __readonly__
append = __readonly__
Expand Down

0 comments on commit 0c2e877

Please sign in to comment.