Skip to content

Commit d05b474

Browse files
Support older serialization protocol
Closes #1643
1 parent 0057103 commit d05b474

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ See [0Ver](https://0ver.org/).
1111
### Bugfixes
1212

1313
- Fixed HKT `to_str` example in documentation
14+
- Fixed backward deserialization compatibility for BaseContainer
1415

1516

1617
## 0.20.0

returns/primitives/container.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from abc import ABCMeta
2-
from typing import Any, TypeVar
2+
from typing import Any, TypeVar, Union
33

44
from typing_extensions import TypedDict
55

@@ -56,11 +56,15 @@ def __getstate__(self) -> _PickleState:
5656
"""That's how this object will be pickled."""
5757
return {'container_value': self._inner_value} # type: ignore
5858

59-
def __setstate__(self, state: _PickleState) -> None:
59+
def __setstate__(self, state: Union[_PickleState, Any]) -> None:
6060
"""Loading state from pickled data."""
61-
object.__setattr__( # noqa: WPS609
62-
self, '_inner_value', state['container_value'],
63-
)
61+
if isinstance(state, dict) and 'container_value' in state:
62+
object.__setattr__( # noqa: WPS609
63+
self, '_inner_value', state['container_value'],
64+
)
65+
else:
66+
# backward compatibility with 0.19.0 and earlier
67+
object.__setattr__(self, '_inner_value', state) # noqa: WPS609
6468

6569

6670
def container_equality(

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ per-file-ignores =
8787
returns/contrib/hypothesis/*.py: WPS437, WPS609
8888
# TODO: remove after mypy@0.800
8989
returns/contrib/mypy/_typeops/visitor.py: S101, WPS232
90+
# Allow class attributes literals for slots and setattr:
91+
returns/primitives/container.py: WPS226
9092
# There are multiple assert's in tests:
9193
tests/*.py: S101, WPS204, WPS218, WPS226, WPS432, WPS436
9294
# Some examples don't have any docs on purpose:

tests/test_io/test_io_container/test_io_pickle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ def test_io_pickle():
99
def test_io_pickle_restore():
1010
"""Ensures that object can be restored."""
1111
container = IO(2)
12-
container.__setstate__({'container_value': 1}) # type: ignore # noqa: WPS609, E501
12+
container.__setstate__({'container_value': 1}) # noqa: WPS609, E501
1313
assert container == IO(1)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pickle # noqa: S403
2+
3+
from returns.primitives.container import BaseContainer
4+
5+
6+
def test_pickle_backward_deserialization():
7+
"""Test that BaseContainer can be deserialized from 0.19.0 and earlier."""
8+
# BaseContainer(1) serialized as of 0.19.0
9+
serialized_container = (
10+
b'\x80\x04\x958\x00\x00\x00\x00\x00\x00\x00\x8c\x1c' +
11+
b'returns.primitives.container\x94\x8c\rBaseContainer' +
12+
b'\x94\x93\x94)\x81\x94K\x01b.'
13+
)
14+
assert pickle.loads(serialized_container) == BaseContainer(1) # noqa: S301

0 commit comments

Comments
 (0)