Skip to content
Snippets Groups Projects
Commit c4dad17f authored by vlorentz's avatar vlorentz
Browse files

Allow passing an ImmutableDict as argument to ImmutableDict's constructor.

It allows easy conversion of Union[ImmutableDict, Dict] to ImmutableDict.
parent 9e475a70
No related branches found
No related tags found
No related merge requests found
......@@ -13,9 +13,16 @@ VT = TypeVar("VT")
class ImmutableDict(Mapping, Generic[KT, VT]):
data: Tuple[Tuple[KT, VT], ...]
def __init__(self, data: Union[Iterable[Tuple[KT, VT]], Dict[KT, VT]] = {}):
def __init__(
self,
data: Union[
Iterable[Tuple[KT, VT]], "ImmutableDict[KT, VT]", Dict[KT, VT]
] = {},
):
if isinstance(data, dict):
self.data = tuple(item for item in data.items())
elif isinstance(data, ImmutableDict):
self.data = data.data
else:
self.data = tuple(data)
......
......@@ -32,6 +32,22 @@ def test_immutabledict_one_item():
assert list(d.items()) == [("foo", "bar")]
def test_immutabledict_from_iterable():
d1 = ImmutableDict()
d2 = ImmutableDict({"foo": "bar"})
assert ImmutableDict([]) == d1
assert ImmutableDict([("foo", "bar")]) == d2
def test_immutabledict_from_immutabledict():
d1 = ImmutableDict()
d2 = ImmutableDict({"foo": "bar"})
assert ImmutableDict(d1) == d1
assert ImmutableDict(d2) == d2
def test_immutabledict_immutable():
d = ImmutableDict({"foo": "bar"})
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment