Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco-Sulla committed Mar 31, 2024
2 parents c6c6d2a + 956fbe0 commit 6c40246
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ Pipfile
test/core.*
.eggs/
.idea/
.vs/
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Same as `key(index)`, but it returns a tuple with (key, value) at the given inde
The `frozendict` _module_ has also these static methods:

### `frozendict.deepfreeze(o, custom_converters = None, custom_inverse_converters = None)`
Converts the object and all the objects nested in it in its immutable
Converts the object and all the objects nested in it, into their immutable
counterparts.

The conversion map is in `getFreezeConversionMap()`.
Expand Down Expand Up @@ -275,7 +275,7 @@ o = {"x": [
a
]}

cool.deepfreeze(a)
cool.deepfreeze(o)
# frozendict(x = (
# 5,
# frozendict(y = frozenset({5, "b", memoryview(b"b")})),
Expand Down
11 changes: 8 additions & 3 deletions src/frozendict/cool.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,15 @@ def deepfreeze(
custom_inverse_converters = custom_inverse_converters
)

if frozen_type:
return type_o(o)
try:
freeze = freeze_conversion_map[base_type_o]
except KeyError:
if frozen_type:
freeze = type_o
else:
raise

return freeze_conversion_map[base_type_o](o)
return freeze(o)


__all__ = (
Expand Down
44 changes: 44 additions & 0 deletions test/test_freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import frozendict as cool
from frozendict import frozendict
from collections import OrderedDict
from collections.abc import MutableSequence, Sequence
from array import array
from types import MappingProxyType
from frozendict import FreezeError, FreezeWarning
Expand All @@ -12,6 +13,38 @@ def __init__(self, x):
self.x = x


class BaseSeq(Sequence):

def __init__(self, seq):
self._items = list(seq)

def __getitem__(self, i):
return self._items[i]

def __len__(self):
return len(self._items)


class FrozenSeqA(BaseSeq):
pass


class FrozenSeqB(BaseSeq):
pass


class MutableSeq(BaseSeq, MutableSequence):

def __delitem__(self, i):
del self._items[i]

def __setitem__(self, i, v):
self._items[i] = v

def insert(self, index, value):
self._items.insert(index, value)


def custom_a_converter(a):
return frozendict(**a.__dict__, y="mbuti")

Expand Down Expand Up @@ -138,3 +171,14 @@ def test_register_inverse(before_cure_inverse, after_cure_inverse):
)

assert cool.deepfreeze(before_cure_inverse) == after_cure_inverse


def test_prefer_forward():
assert isinstance(
cool.deepfreeze(
[FrozenSeqA((0, 1, 2))],
custom_converters={FrozenSeqA: FrozenSeqB},
custom_inverse_converters={FrozenSeqA: MutableSeq}
)[0],
FrozenSeqB
)

0 comments on commit 6c40246

Please sign in to comment.