Skip to content

[3.9] bpo-45520: Backport __getstate__, __setstate__ methods for dataclasses from 3.10 into 3.9 (GH-25786) #29147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,10 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen):
raise TypeError(f'Cannot overwrite attribute {fn.__name__} '
f'in class {cls.__name__}')

# Need this for pickling frozen classes.
cls.__getstate__ = _dataclass_getstate
cls.__setstate__ = _dataclass_setstate

# Decide if/how we're going to create a hash function.
hash_action = _hash_action[bool(unsafe_hash),
bool(eq),
Expand All @@ -995,6 +999,21 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen):
return cls


# _dataclass_getstate and _dataclass_setstate are needed for pickling frozen
# classes (maybe even with slots).
# These could be slightly more performant if we generated
# the code instead of iterating over fields. But that can be a project for
# another day, if performance becomes an issue.
def _dataclass_getstate(self):
return [getattr(self, f.name) for f in fields(self)]


def _dataclass_setstate(self, state):
for field, value in zip(fields(self), state):
# use setattr because dataclass may be frozen
object.__setattr__(self, field.name, value)


def dataclass(cls=None, /, *, init=True, repr=True, eq=True, order=False,
unsafe_hash=False, frozen=False):
"""Returns the same class as was passed in, with dunder methods
Expand Down
29 changes: 29 additions & 0 deletions Lib/test/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2775,6 +2775,35 @@ class Derived(Base):
# We can add a new field to the derived instance.
d.z = 10

# Can't be local to test_frozen_pickle.
@dataclass(frozen=True)
class FrozenSlotsClass:
__slots__ = ('foo', 'bar')
foo: str
bar: int

@dataclass(frozen=True)
class FrozenWithoutSlotsClass:
foo: str
bar: int

def test_frozen_pickle(self):
# bpo-43999
# bpo-45520

self.assertEqual(self.FrozenSlotsClass.__slots__, ("foo", "bar"))
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
with self.subTest(proto=proto):
obj = self.FrozenSlotsClass("a", 1)
p = pickle.loads(pickle.dumps(obj, protocol=proto))
self.assertIsNot(obj, p)
self.assertEqual(obj, p)

obj = self.FrozenWithoutSlotsClass("a", 1)
p = pickle.loads(pickle.dumps(obj, protocol=proto))
self.assertIsNot(obj, p)
self.assertEqual(obj, p)

class TestDescriptors(unittest.TestCase):
def test_set_name(self):
# See bpo-33141.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix ``__getstate__`` and ``__setstate__`` methods for frozen
``dataclasses``. It basically backports a part of 3.10's code to support
``pickle`` and ``deepcopy``.