FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.11] gh-104035: Do not ignore user-defined `__{get,set}state__` in slotted frozen dataclasses (GH-104041) by miss-islington · Pull Request #104044 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
6 changes: 4 additions & 2 deletions Lib/dataclasses.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -1191,8 +1191,10 @@ def _add_slots(cls, is_frozen, weakref_slot):

if is_frozen:
# Need this for pickling frozen classes with slots.
cls.__getstate__ = _dataclass_getstate
cls.__setstate__ = _dataclass_setstate
if '__getstate__' not in cls_dict:
cls.__getstate__ = _dataclass_getstate
if '__setstate__' not in cls_dict:
cls.__setstate__ = _dataclass_setstate

return cls

Expand Down
68 changes: 68 additions & 0 deletions Lib/test/test_dataclasses.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -3068,6 +3068,74 @@ def test_frozen_pickle(self):
self.assertIsNot(obj, p)
self.assertEqual(obj, p)

@dataclass(frozen=True, slots=True)
class FrozenSlotsGetStateClass:
foo: str
bar: int

getstate_called: bool = field(default=False, compare=False)

def __getstate__(self):
object.__setattr__(self, 'getstate_called', True)
return [self.foo, self.bar]

@dataclass(frozen=True, slots=True)
class FrozenSlotsSetStateClass:
foo: str
bar: int

setstate_called: bool = field(default=False, compare=False)

def __setstate__(self, state):
object.__setattr__(self, 'setstate_called', True)
object.__setattr__(self, 'foo', state[0])
object.__setattr__(self, 'bar', state[1])

@dataclass(frozen=True, slots=True)
class FrozenSlotsAllStateClass:
foo: str
bar: int

getstate_called: bool = field(default=False, compare=False)
setstate_called: bool = field(default=False, compare=False)

def __getstate__(self):
object.__setattr__(self, 'getstate_called', True)
return [self.foo, self.bar]

def __setstate__(self, state):
object.__setattr__(self, 'setstate_called', True)
object.__setattr__(self, 'foo', state[0])
object.__setattr__(self, 'bar', state[1])

def test_frozen_slots_pickle_custom_state(self):
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
with self.subTest(proto=proto):
obj = self.FrozenSlotsGetStateClass('a', 1)
dumped = pickle.dumps(obj, protocol=proto)

self.assertTrue(obj.getstate_called)
self.assertEqual(obj, pickle.loads(dumped))

for proto in range(pickle.HIGHEST_PROTOCOL + 1):
with self.subTest(proto=proto):
obj = self.FrozenSlotsSetStateClass('a', 1)
obj2 = pickle.loads(pickle.dumps(obj, protocol=proto))

self.assertTrue(obj2.setstate_called)
self.assertEqual(obj, obj2)

for proto in range(pickle.HIGHEST_PROTOCOL + 1):
with self.subTest(proto=proto):
obj = self.FrozenSlotsAllStateClass('a', 1)
dumped = pickle.dumps(obj, protocol=proto)

self.assertTrue(obj.getstate_called)

obj2 = pickle.loads(dumped)
self.assertTrue(obj2.setstate_called)
self.assertEqual(obj, obj2)

def test_slots_with_default_no_init(self):
# Originally reported in bpo-44649.
@dataclass(slots=True)
Expand Down
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Do not ignore user-defined ``__getstate__`` and ``__setstate__`` methods for
slotted frozen dataclasses.

Back | FazBrowse Home | New Git URL